elasticsearch 如何在传输客户端或节点客户端中启用文档路由
我想在弹性搜索中使用 但是我找不到任何JavaAPI来支持同样的功能 我经历过,但似乎没有人解决这个问题 我的代码:elasticsearch 如何在传输客户端或节点客户端中启用文档路由,elasticsearch,apache-storm,elasticsearch,Apache Storm,我想在弹性搜索中使用 但是我找不到任何JavaAPI来支持同样的功能 我经历过,但似乎没有人解决这个问题 我的代码: public void prepare(Map stormConf, TopologyContext context, OutputCollector collector) { this.collector = collector; Settings settings = ImmutableSettings.settingsBuilder()
public void prepare(Map stormConf, TopologyContext context, OutputCollector collector) {
this.collector = collector;
Settings settings = ImmutableSettings.settingsBuilder()
.put("cluster.name", elasticSearchCluster).build();
this.client = new TransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(esHost, esPort));
}
public void execute(Tuple tuple) {
try {
String document = tuple.toString();
byte[] byteBuffer = document.getBytes();
IndexResponse response = this.client.prepareIndex(indexName, type, id)
.setSource(byteBuffer).execute().actionGet();
} catch (Exception e) {
e.printStackTrace();
}
collector.ack(tuple);
}
请注意,我在这里使用TransportClient,因为在storm中使用Node Client似乎不是一个好方法,但问题是与此无关。如果有一种将节点客户端与路由一起使用的方法,请务必建议,否则TransportClient的路由也会有很大帮助。我相信您混淆了ES中两个不同的“路由”概念。一个是,另一个是(或“过滤”)
\u routing
字段允许您指定为每个文档编制索引时要使用的值,以确定文档将在哪个碎片上编制索引。您提供的其他两个链接是指索引级别(与文档级别相反)设置,该设置确定如何将索引的碎片分配给集群中的各个节点
听起来好像您正在尝试执行文档传送。这可以在Java API中使用IndexRequestBuilder
类和setRouting(String)
方法实现。看一看这张照片
还有一些在索引期间指定路由字段。几乎
您可以只替换一行代码
从
到
然后,您的文档将存储在与您提供的路由值相对应的特定分片中。在搜索时,您可以提供相同的路由值,以便您的搜索请求只命中一个特定碎片
IndexResponse response = this.client.prepareIndex(indexName, type, id)
.setSource(byteBuffer).execute().actionGet();
String routingValue = "ANY_ROUTING_VALUE_YOU_WANT";
IndexResponse response = this.client.prepareIndex(indexName, type, id) .setSource(byteBuffer).setRouting(routingValue).execute().actionGet();