如何在ES API Java中创建mutilple索引

时间:2017-06-22 14:15:27

标签: java elasticsearch

我的索引是:https://URL/bpa_stag_v3_2016*,bpa_stag_v3_2017/orders_suppliers_stats_ag/_search 我使用api java进行弹性搜索,但它没有用。

  String indexForTwoYear = ConfiguratorDao.CURRENT_ENV.getAliasPrefix() + String.valueOf(previousYear) + "*,"
                    + ConfiguratorDao.CURRENT_ENV.getAliasPrefix() + String.valueOf(currentYear) + "*";



            SearchRequestBuilder requestBuilder = client.prepareSearch(indexForTwoYear)
                    .setTypes(IndexTypeEnum.SUPPLIERS_AGGREGATION.getValue()).setQuery(queryBuilder).setSize(1);

2 个答案:

答案 0 :(得分:1)

要使用Java API搜索多个索引,您应该将索引名称作为多个参数传递(而不是作为单个逗号分隔的字符串...)

像这样:client.prepareSearch("bpa_stag_v3_2016*", "bpa_stag_v3_2017")

请参阅此处示例https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-search.html

答案 1 :(得分:1)

根据AbstractClient Elasticsearch类(由NodeClientTransportClient扩展)的文档,您可以在prepareSearch中将可变数量的索引作为var-args传递}方法(javadoc here):

public SearchRequestBuilder prepareSearch(String... indices)
  

使用查询搜索一个或多个索引和一个或多个类型。

所以,在你的情况下,它将是:

client.prepareSearch("pa_stag_v3_2016*","bpa_stag_v3_2017")