【问题标题】:How to enable document routing in Transport Client or Node Client如何在传输客户端或节点客户端中启用文档路由
【发布时间】:2016-02-27 03:19:10
【问题描述】:

我想在 Elastic-Search 中使用 routing-field

但我找不到任何 Java API 来启用它。

我经历过link 1link 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 的路由也会有很大帮助。

【问题讨论】:

    标签: elasticsearch apache-storm


    【解决方案1】:

    我相信您在 ES 中混淆了两个不同的“路由”概念。一个是document routing,另一个是index allocation routing(或“过滤”)。

    _routing 字段允许您指定索引每个文档时要使用的值,以确定文档将在哪个分片上建立索引。您提供的其他两个链接指的是 index-level(相对于 document-level)设置,该设置确定索引的分片如何分配给你的集群。

    听起来您正在尝试进行文档路由。这可以使用IndexRequestBuilder 类和setRouting(String) 方法在Java API 中完成。看看source code on GitHub

    还有一些good code examples here在索引时指定路由字段。

    【讨论】:

      【解决方案2】:

      几乎!

      你可以只替换一行代码

      来自

          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();
      

      然后您的文档将存储在与您提供的路由值相对应的特定分片中。在搜索时,您可以提供相同的路由值,以便您的搜索请求只命中一个特定的分片。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-27
        • 2019-04-15
        • 1970-01-01
        • 2017-07-05
        • 2015-12-06
        • 2014-07-21
        • 2016-09-28
        • 1970-01-01
        相关资源
        最近更新 更多