Cover image for How to query multiple indexes with elasticsearch in Spring boot(Java App)

How to query multiple indexes with elasticsearch in Spring boot(Java App)

balvinder294 profile image Balvinder Singh Originally published at Medium ・2 min read

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

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.


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();
// 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();
// 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.

Posted on by:

balvinder294 profile

Balvinder Singh


Full Stack Developer and DevOps working remotely in Dehaze.io. Founder and Blogger at Tekraze.com, here to share my journey of code and experiences to help out the coders to give back to dev communiry


Editor guide