How to query multiple indexes with elasticsearch in Spring boot(Java App)
As we all know elasticsearch has indexes, that you can query to get data. That is simple with spring data configured or rest template. So, I came across a complex query I need to perform. So let see.
The Query I was to do on two user indexes on different types, having some similar data. So I did some r&d and got to know about multi-search queries.
Multi-search queries enable us to query two or more indexes, with queries for each index. Check out at the link below
https://www.elastic.co/guide/en/elasticsearch/client/java-rest/master/java-rest-high-multi-search.html
My Solution
The solution multi-search API provided was good, but complex for my case, as I had different indexes but with the same fields in both like userId, id, name.
So I do use multi indexes but with a single search and single query. Let see.
Steps
1.Create a method and initialize High-Level Rest Client(You can use directly in your main method too, I just prefer keeping separate code)
private RestHighLevelClient restHighLevelClient() {
log.debug("RestHighLevelClient initialization");
String esHost = // host of elasticsearch
Integer esPort = // port of elasticsearch
return new RestHighLevelClient(RestClient.builder(new HttpHost(esHost, esPort)));
}
2.Search method with a query
search(keyword: string) {
// Create a Bool query
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
boolQuery.must(query);
// Create a search request
// pass your indexes in place of indexA, indexB
SearchRequest searchRequest = new SearchRequest("indexA", "indexB");
// CReate a search Source
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(boolQuery);
searchRequest.source(searchSourceBuilder);
// Create object to get Response
SearchResponse searchResponse = restHighLevelClient().search(searchRequest, RequestOptions.DEFAULT);
// Parsing response
SearchHit[] searchHits = searchResponse.getHits().getHits();
}
3.Parsing data from Search hit by looping
Just pass the fields and cast data type to get the expected response
for(SearchHit hit: searchHits) {
Map<String, Object> sourceAsMap = hit.getSourceAsMap();
String field = (String) sourceAsMap.get("fieldName");
Integer field2 = (Integer) sourceAsMap.get("fieldName");
}
4.This is how you can do the multi-search. Just replace your query, indexes as required, and parse data
Note: You must be thinking about why we did not use any DTO instead. This is because, we require just 3 fields for now, which can be handled easily. We using JSONObject for this. You can create any class or DTO for parsing.
I hope it will help you, thanks for reading.
Also, checkout Tekraze.com for more articles.
Originally published at Medium.com
Leave How to query multiple indexes with elasticsearch in Spring boot(Java App) to:
Read more #elasticsearch posts
Best Posts From Balvinder Singh
We have not curated any of balvinder294's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From Balvinder Singh
- My Dcity Progress | Student Debt | Powering up
- Purchased Games Witcher 3, Star Wars, Need for speed Unbound on Steam Sale
- My Dcity Progress and Staking SIM more power to 1M | March 4
- My Golem And Terracore Updates | 17th Feb
- My Daily activity for 16th Feb | More HP from Hive Engine lease
- AC Mirage Jailbreak, A Delicate Alliance PC gameplay part 3
- Unveiling the Secrets of Assassin's Creed Mirage: Initial Mission in Baghdad PC Gameplay Part 2
- Assassin's Creed Mirage Gameplay 1 No commentary 1080 PC - Getting to know Baghdad and Basim
- Starfield Gameplay No Commentary Part 5- First Rescue mission With team and exploration of planets
- I Upgraded my Old desktop PC to Modern RGB Gaming with Ryzen 5