简体   繁体   中英

How to highlight date fields in Elasticsearch?

My mapping:

"mappings": {
     "my_type": {
        "properties": {
           "birthDate": {
              "type": "date",
              "format": "dateOptionalTime"
           },
           "name": {
              "type": "string"
           }
        }
     }
}

My search query:

GET my_index/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "name": "babken"
          }
        },
        {
          "term": {
            "birthDate": {
              "value": "1999-01-01"
            }
          }
        }
      ]
    }
  },
  "highlight": {
    "fields": {
      "*": {}
    }
  }
}

However in the response body, only the name field is highlighted, even though the birthDate field has matched as well:

"hits": [
         {
            "_index": "my_index",
            "_type": "my_type",
            "_id": "1a82fbb4-1268-42b9-9999-ef932f67a114",
            "_score": 12.507131,
            "_source": {
               "name": "babken",
               "birthDate": "1999-01-01",
            },
            "highlight": {
               "name": [
                  "<em>babken</em>"
               ]
            }
         }
         ...

How can I make the birthDate field appear in "highlight" results as well if it has matched?

I'm using Elasticsearch 1.6

You would need to change the the type to string to enable highlighting.

Bare minimum requirement for a field to be enabled for highlighting is that it should be string type.

The following issue has little more discussion about it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM