Elasticearch highlight: how to get all the text of a field in a Java client

I am new to Elasticsearch. I hope to get the highlighted field in the Java client. If I run the following query at a Windows prompt:

{
    "query": {
        "filtered" : {
            "query" : {
                "term" : {
                    "title" : "western"
                }
            },
            "filter" : {
                "term" : { "year" : 1961 }
            }
        }
    },
    "highlight" : {
        fields" : {
            "title" : {}
            }
        }
}

I get nice selected text as follows:

{
      "_index" : "book",
      "_type" : "history",
      "_id" : "1",
      "_score" : 0.095891505,
      "_source":{ "title": "All Quiet on the Western great Front", "year": 1961}
      "highlight" : {
        "title" : [ "All Quiet on the <em>Western</em> great Front dead" ]
      }
}

Selection

  "highlight" : {
    "title" : [ "All Quiet on the <em>Western</em> great Front dead" ]
  }

can be easily converted to a Java Map object, and the title property has all the text of the matching field, which is really what I want.

However, in the Java client, I get selected fragments that put different segments of the selected text of the same field into an array of text.

Thank you and welcome.

+4
source share
3 answers

API Java 5. , , , .

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

, , . Java- setHighlighterFragmentSize:

SearchResponse sr = client.prepareSearch("book")
                .setTypes("history")
                .addHighlightedField("title")
                .setQuery(query)
                .setHighlighterFragmentSize(2000) //set it larger than the size of the field so that the only one fragment is returned and it contains the entire text of the field.

, , .

.

+1

You can also set the number of fragments to 0, which displays the entire field with highlighting tags. It also ignores the fragment_size parameter.

.setHighlighterNumOfFragments(0)
+1
source

All Articles