The6thSense
The6thSense

Reputation: 8335

Elastic Search Highlight API highlight all macthed words

I have indexed document like below in elastic search.

Doc:

{{"ID:1, "Cont": "yes there is a match"},
  {"ID":2, "Cont": "check this for it"} ....}

When I searched the document using highlight.

http://localhost:9200/sample/html/_search
{
    "query": 
        {"bool": {  
            "should": [{"match": {"Content": "check mate"}}]}},
    "highlight" : {
        "fields" : {
            "Content" : {}
        }
    }
}

The output was like below.

"highlight": {
      "Content": ["<em>check</em> blaa", "blaa blaa<em>check</em>"]
}

From the highlight output it looks like check matched on two places but when I open the document and see there was 15 occurrence of check and there was also a match for mate in the document.

  1. Is there a way to get all matched words in the highlight column i.e.) get both the check and mate of this example.

    2.Is there a way to get match occurrence count i.e.) check=15, mate=1

Upvotes: 1

Views: 4039

Answers (1)

Paulin Trognon
Paulin Trognon

Reputation: 813

For your first question:

You can use the number_of_fragments option to get more highlight fragments. By default it is set to 2 (that's why you can see only 2 highlights). You can set it to 100 for example if you want to see more of them.

You can also set number_of_fragments to 0, this will not fragment your Content to show highlights but will give your whole content highlighted (then you will see all highlight occurences).

Documentation: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-highlighting.html#_highlighted_fragments

2nd question:

As far as I know, I don't think that's possible... You would have to count them from the highlight result I'm affraid...

Upvotes: 3

Related Questions