DEV Community

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

Posted on • Originally published at Medium

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)));
    }
Enter fullscreen mode Exit fullscreen mode

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();
}
Enter fullscreen mode Exit fullscreen mode

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");
}
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)