ElasticSearch java API to get multiple documents across indexes (using alias) using 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.
From Elasticsearch 5.1, GET API supports querying on documents on an alias too that can point to multiple indexes like this:
GET /my_alias_name/_search/
"query":
"bool":
"filter":
"term":
"_id": "AUwNrOZsm6BwwrmnodbW"
What is the corresponding JAVA API to achieve this (using JestClient...)?
1 Answer
1
1) Client creation:
JestClientFactory factory = new JestClientFactory();
factory.setHttpClientConfig(new HttpClientConfig.Builder("http://localhost:9200")
.multiThreaded(true)
.build());
JestClient jestClient = factory.getObject();
2) Prepare the Search request:
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:
SearchResult result = jestClient.execute(search);
Note: We can add an alias name in place of index name and it works the
same way.
To make it clear, the request you had mentioned in your question is Search API not a Get API.
– Technocrat Sid
Sep 4 '18 at 15:58
Even if you perform a GET request using the alias name like: GET /my_alias_name/my_type/my_id will return you the document.
– Technocrat Sid
Sep 4 '18 at 15:59
Like I mentioned, I am querying on alias that can point to multiple indexes, so there can be multiple documents matching the same document _id. And no, GET /my_alias_name/my_type/my_id errors out like this: "error" : "ElasticsearchIllegalArgumentException[Alias [data] has more than one indices associated with it [[index_1, index_2]], can't execute a single index op]", "status" : 400
– blrguy
Sep 4 '18 at 16:08
Okay got your point. And yes you're right about the GET request failing with error because in your case the alias is pointing to multiple indices. I did not consider that case apologies for that. But your question says "From Elasticsearch 5.1, GET API supports querying on documents on an alias too that can point to multiple indexes like this" --the search query-- "What is the corresponding JAVA API to achieve this (using JestClient...)?"
– Technocrat Sid
Sep 4 '18 at 17:31
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
May be I was not clear on one more thing I have in mind. I was asking specifically about GET API as I was under an assumption that SEARCH requests are costlier than GET requests (any request is on document _id only)! Correct me if I am wrong.
– blrguy
Sep 4 '18 at 15:37