4

I'd like check if a field exists in my custom scoring script when searching in elasticsearch.

This search works:

{
  "query": {
    "custom_score": {
      "script": "((doc['region'].value == '1') ? 1 : 0)",
      "query": {
        "constant_score": {
          "filter": {
            "and": [
              {
                "term": {
                  "id": "100"
                }
              }
            ]
          }
        }
      }
    }
  },
  "size": 1
}

It will score the result as 1 if the region field value is 1.

However this query will not work: it will not score the document as 1 if the region field does not exist in the document:

{
  "query": {
    "custom_score": {
      "script": "((doc['region'].value == null) ? 1 : 0)",
      "query": {
        "constant_score": {
          "filter": {
            "and": [
              {
                "term": {
                  "id": "100"
                }
              }
            ]
          }
        }
      }
    }
  },
  "size": 1
}

How do I check for missing values in a script?

4

1 回答 1

14

如果此字段具有数字类型,其中包括索引为 long 的日期,则缺失值由 0 表示。

于 2012-10-26T20:37:06.460 回答