简体   繁体   English

ElasticSearch java API 使用 item id 跨索引(使用别名)获取多个文档

[英]ElasticSearch java API to get multiple documents across indexes (using alias) using item id

ElasticSearch has GET API using which we can query on a single index for a particular document using the document Id. ElasticSearch 有 GET API,使用它我们可以使用文档 ID 查询特定文档的单个索引。 From Elasticsearch 5.1, GET API supports querying on documents on an alias too that can point to multiple indexes like this:从 Elasticsearch 5.1 开始,GET API 也支持查询别名上的文档,该别名可以指向多个索引,如下所示:

GET /my_alias_name/_search/
{
        "query": { 
        "bool": {
         "filter": {
                "term": {
                   "_id": "AUwNrOZsm6BwwrmnodbW"
                }
            }
        }
    }
}

What is the corresponding JAVA API to achieve this (using JestClient...)?实现此目的的相应 JAVA API 是什么(使用 JestClient...)?

1) Client creation: 1)客户端创建:

JestClientFactory factory = new JestClientFactory();
factory.setHttpClientConfig(new HttpClientConfig.Builder("http://localhost:9200")
                        .multiThreaded(true)
                        .build());
JestClient jestClient = factory.getObject();

2) Prepare the Search request: 2) 准备搜索请求:

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.boolQuery().filter(QueryBuilders.termQuery("_id", "AUwNrOZsm6BwwrmnodbW")));

Search search = new Search.Builder(searchSourceBuilder.toString())
                        .addIndex("my_alias_name") -> Add index name or an alias. 
                        .addType("my_type") -> Add index type here. 
                        .build();

3) Execute the search: 3)执行搜索:

SearchResult result = jestClient.execute(search);

Note: We can add an alias name in place of index name and it works the same way.注意:我们可以添加别名来代替索引名,它的工作方式相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM