stackoverflowsucks
stackoverflowsucks

Reputation: 247

Elasticsearch highlighting using multi_match

I have used highlighting with ES with simple match query:

GET /_search
{
   "query": {
      "match": {
         "Text": "key words here"
      }
   },
   "highlight": {
      "pre_tags" : ["<span class='highlighter'>"],
      "post_tags" : ["</span>"],
      "fields": {
         "Text": {
            "fragment_size": 400,
            "number_of_fragments": 1,
            "no_match_size" : 20
         }
      }
   }
}

this works nicely and highlight Text is returned in the result with the specified tags.

I would like to use highlighting on a multi_match query like this:

GET /_search
{
   "query": {
      "multi_match": {
         "query": "GB RAM",
         "operator": "AND",
         "fields": "_all"
      }
   },
   "highlight": {
         "pre_tags": [
            "<span class='highlighter'>"
         ],
         "post_tags": [
            "</span>"
         ],
         "fields": {
            "Text": {
               "fragment_size": 400,
               "number_of_fragments": 1,
               "no_match_size": 20
            }
         }
      }
}

this doesn't quite work, the highlight Text returned is 20 chars long (the no_match_size), like this:

 "highlight": {
    "Text": ["        DVD-RAM"]
  }

What am I doing incorrectly here?

Upvotes: 4

Views: 7470

Answers (2)

stackoverflowsucks
stackoverflowsucks

Reputation: 247

Thanks to user3775217 for answering. This is the query that finally worked for me

GET /_search
{
   "_source": {
        "exclude": [ "Text" ]
   },
   "query": {
        "multi_match": {
            "query": "DICTIONARY",
            "operator": "AND",
            "fields": "_all"
        }
    },
   "highlight": {
      "pre_tags": [
         "<span class='highlighter'>"
      ],
      "post_tags": [
         "</span>"
      ],
      "fields": {
         "Text": {
            "require_field_match": false,
            "fragment_size": 400,
            "number_of_fragments": 1,
            "no_match_size": 20
         }
      }
   },
   "size": 100
}

http://kempe.net/blog/2015/02/25/elasticsearch-query-full-text-search.html

Upvotes: 2

user3775217
user3775217

Reputation: 4803

You will have to modify the mappings first to enable store:true in the mappings. Since highlight need exact string value for it to work as _all field doesn't include itself in source.

Change mappings to set store:true for _all

PUT highlight_index_2

{
  "mappings": {
    "type_1" : {
     "_all": {
        "enabled": true,
        "store": true
      }
    }
  }
}

Next you will need to tweak your query a bit. You are getting highlights for only Text fields as your query specify the lucene highlighter to highlight only for Text field. You can modify you query like follows

{
    "query": {
        "query_string": {
            "fields": [
                "_all"
            ],
            "query": "this is the"
        }
    },
    "highlight": {
        "pre_tags": [
            "<span class='highlighter'>"
        ],
        "post_tags": [
            "</span>"
        ],
        "fields": {
            "_all": {
                "fragment_size": 400,
                "number_of_fragments": 2,
                "no_match_size": 20
            }
        }
    }
}

Make sure to tune number of fragments for highlighting multiple fields.

Upvotes: 2

Related Questions