Multiple index search java

i want a searchstring to searched in multiple indices. i have written below code:

SearchRequest searchRequest = new SearchRequest("indexA","indexB");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
MultiMatchQueryBuilder operator = QueryBuilders.multiMatchQuery("searchString", "column1_From_IndexA", "column2_From_IndexA","column1_From_IndexB")
.operator(Operator.AND);

i think i am doing something wrong.

please suggest, how the search can be performed

QueryStringQueryBuilder is used to search string where as MultiMatchQueryBuilder/MatchQueryBuilder is used to match the exact value in the specific field, if exact match is there then and only you will get document. Whereas QueryStringQueryBuilder provides search operation you can perform search operation here.

So first decide, do you want exact match or want to search string ?

Could you try this for the search operation ?

SearchRequest searchRequest = new SearchRequest("indexA","indexB");

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

/**
* Multi fields to be searched in
*/
Map<String,Float> fields = new HashMap<String, Float>();
fields.put("column1_From_IndexA", 0.0F);
fields.put("column2_From_IndexA", 0.0F);
fields.put("column1_From_IndexB", 0.0F);

QueryStringQueryBuilder builder = QueryBuilders.queryStringQuery("searchString").fields(fields);
searchSourceBuilder.query(builder);

searchRequest.source(searchSourceBuilder);

SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);

This may helpful, thank you.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.