我想在Elastic-Search中使用routing-field。
但是我找不到能够启用它的Java API。
我已经遍历了link 1和link 2,但是似乎都没有解决这个问题。
我的代码:
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,因为似乎没有一种将Node-Client与Storm一起使用的好方法,但是这个问题无关紧要。如果可以使用Node-Client进行路由,请建议否则,否则TransportClient的路由也会有很大帮助。
最佳答案
我相信您在ES中混淆了两个不同的“路由”概念。一个是document routing,另一个是index allocation routing(或“过滤”)。_routing
字段允许您指定在为每个文档建立索引时使用的值,以确定将在哪个分片上建立索引。您提供的其他两个链接引用索引级别(而不是文档级别)设置,该设置确定如何将索引的分片分配给集群中的各个节点。
听起来您正在尝试进行文档路由。这可以在Java API中使用IndexRequestBuilder
类和setRouting(String)
方法来完成。看看source code on GitHub。
还有一些good code examples here在索引期间指定路由字段。
关于elasticsearch - 如何在传输客户端或节点客户端中启用文档路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35665268/