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.





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






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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

How do I collapse sections of code in Visual Studio Code for Windows?