Elasticsearch自定义评分脚本 - 检查字段是否存在?

时间:2012-10-26 18:04:50

标签: elasticsearch

我想在弹性搜索中搜索我的自定义评分脚本中是否存在字段。

此搜索有效:

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

如果区域字段值为1,它会将结果评分为1。

但是此查询不起作用:如果文档中不存在区域字段,它将不会将文档评分为1:

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

如何检查脚本中的缺失值?

1 个答案:

答案 0 :(得分:14)

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

相关问题