Elasticsearch 是否接受 1 或 0 来搜索布尔字段?

Does Elasticsearch accept 1 or 0 for searching on boolean field?

这是我的 elasticsearch 查询的一部分:

 {
    "match": {
        "value": {
            "query": "1",
        }
    }
}

value 是我的索引中的布尔字段,elasticsearch 在布尔字段的搜索中是否接受 1 为 true 和 0 为 false?

这很容易测试...

首先创建索引:

PUT test 
{
  "mappings": {
    "properties": {
      "bool_field": {
        "type": "boolean"
      }
    }
  }
}

然后索引文档:

PUT test/_doc/1
{
  "bool_field": true
}

尝试使用 0/1 而不是布尔值进行查询

POST test/_search
{
  "query": {
    "term": {
      "bool_field": "1"
    }
  }
}

响应:Can't parse boolean value [1], expected [true] or [false]

{
  "error" : {
    "root_cause" : [
      {
        "type" : "query_shard_exception",
        "reason" : "failed to create query: Can't parse boolean value [1], expected [true] or [false]",
        "index_uuid" : "bZpN3j1kT9KtMBnGkpOmKQ",
        "index" : "test"
      }
    ],
    "type" : "search_phase_execution_exception",
    "reason" : "all shards failed",
    "phase" : "query",
    "grouped" : true,
    "failed_shards" : [
      {
        "shard" : 0,
        "index" : "test",
        "node" : "CyVrqrOtR0CP3RfZtdBTag",
        "reason" : {
          "type" : "query_shard_exception",
          "reason" : "failed to create query: Can't parse boolean value [1], expected [true] or [false]",
          "index_uuid" : "bZpN3j1kT9KtMBnGkpOmKQ",
          "index" : "test",
          "caused_by" : {
            "type" : "illegal_argument_exception",
            "reason" : "Can't parse boolean value [1], expected [true] or [false]"
          }
        }
      }
    ]
  },
  "status" : 400
}

PS:以前最多可达ES 5。从 ES 6 开始,只有 true/false 是布尔字段可接受的值。

按指定 in the doc ES 接受 true/"true"/false/"false" 作为布尔值。

其他值如 0/1 将在最新版本中引发错误

error: failed to create query: Can't parse boolean value 1, expected [true] or [false]

注意:您应该使用 term 查询来过滤布尔字段