Highlighting several fields under separate indexes for a query string in elastic search


(Shivang Sarawagi) #1

I went through a StackOverflow question to know how to highlight a response field with elastic search java api but in the code there is only one index "book" being passed in the prepareSearch(index) method

client.prepareSearch("book")
 .setTypes("history")
 .addHighlightedField("title")
 .setQuery(query)
 .setHighlighterFragmentSize(2000)
 .setHighlighterNumOfFragments(1);

the response returns the highlighted field. The problem with my code is I have several indexes, actually an index array being passed to the prepareSearch method with three different indexes. Each index has several fields, I need to highlight all the fields for a particular query string.

    SearchRequestBuilder srb =client.prepareSearch(indexArray);
    srb.addHighlightedField("name");
    srb.addHighlightedField("title"); //field of second index
    srb.addHighlightedField("description"); //field of second index

in the response the highlighted field is only of the first index in the array. The other fields of other indexes are not highlighted.

Can anyone tell me how do I achieve that?

For three index I get three response objects Response for the first index

hits" : [ {
      "_index" : "abcde2016-0125-1607", // 1st Index
      "_type" : "product",
      "_id" : "2542",
      "_score" : 2.6031399,
      "fields" : {
        "name" : [ "Google Cluster F100" ],
        "level" : [ 56 ]
      },
      "highlight" : {
        "name" : [ "<em>Google</em> Cluster F100" ]
      }
    }, 

For other response objects of second & third index I do not receive the highlight tag


(system) #2