Cannot get highlight in using highlighting

Hi, when using "highlight" query. It seems nothing be highlight on return result. As the following, i want to highlight "text" in the content.

PUT http://192.168.196.248:9200/_ingest/pipeline/attachment
{
"description" : "Extract attachment information",
"processors" : [
{
"attachment" : {
"field" : "data"
}
}
]
}

PUT http://192.168.196.248:9200/my_index/my_type/2?pipeline=attachment
{
"filename" : "ipsum2.txt",
"data" : "dGhpcyBpcwpqdXN0IHNvbWUgdGV4dAo="
}

POST http://192.168.196.248:9200/my_index/my_type/_search
{
"query" : {
"match": { "attachment.content": "text" }
},
"highlight" : {
"require_field_match": false,
"fields": {
"_all" : { "pre_tags" : [""], "post_tags" : [""] }
}
}
}

Hi @takleung,

You were almost there. The issue here is that _all field need to be prepared for highlighting.

Furthermore _all field is deprecated in 6.X so maybe its better to use "attachment.content" and it will work as your are expecting:

PUT /_ingest/pipeline/attachment
{
  "description": "Extract attachment information",
  "processors": [
    {
      "attachment": {
        "field": "data"
      }
    }
  ]
}

PUT /my_index/my_type/2?pipeline=attachment
{
  "filename": "ipsum2.txt",
  "data": "dGhpcyBpcwpqdXN0IHNvbWUgdGV4dAo="
}

POST /my_index/my_type/_search
{
  "query": {
    "match": {
      "attachment.content": "text"
    }
  },
  "highlight": {
    "require_field_match": false,
    "fields": {
      "attachment.content": {}
    }
  }
}

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "my_index",
        "_type": "my_type",
        "_id": "2",
        "_score": 0.2876821,
        "_source": {
          "filename": "ipsum2.txt",
          "data": "dGhpcyBpcwpqdXN0IHNvbWUgdGV4dAo=",
          "attachment": {
            "content_type": "text/plain; charset=ISO-8859-1",
            "language": "en",
            "content": "this is\njust some text",
            "content_length": 24
          }
        },
        "highlight": {
          "attachment.content": [
            "this is\njust some <em>text</em>"
          ]
        }
      }
    ]
  }
}

Cheers,
LG

1 Like

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