如何忽略 elasticsearch 查询中的 number_format_exception 错误

how to ignore number_format_exception error in elasticsearch query

Hii 如何忽略以下查询中的数据类型错误,因为当为具有非数字(长)数据类型的字段提供字符串值时它会抛出错误。我知道 lenient 参数,但它不适用于术语查询。

GET employee/_search
{
  "query": {
    "bool": {
        "must": [
          {
            "bool": {
              "should": [
                {
                  "terms": {
                    "employee_id": [
                      "abcdef"
                    ]
                  }
                },
                {
                  "terms": {
                    "employee_name": [
                      "abcdef"
                    ]
                  }
                }
              ]
            }
          }
        ]
      }
  }
}

错误信息

"caused_by": {
            "type": "number_format_exception",
            "reason": "For input string: \"abcdef\""
          }

Elasticsearch 详细信息

"version" : {
    "number" : "7.1.1",
    "build_flavor" : "oss",
    "build_type" : "tar",
    "build_hash" : "Unknown",
    "build_date" : "2020-11-03T08:48:42.499923Z",
    "build_snapshot" : false,
    "lucene_version" : "8.0.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  }

所以预期的行为是数据类型错误应该被忽略并且查询的其余部分运行并产生结果,因为它处于应该条件,如果有必须条件则不给出结果

索引映射

{
  "employee" : {
    "mappings" : {
      "dynamic" : "true",
      "properties" : {
        "employee_id" : {
          "type" : "long"
        }
      }
    }
  }
}

您可以使用 query_string_query 代替:

{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "query_string": {
                  "query": "employee_id:abcdef"   <---
                }
              },
              {
                "terms": {
                  "employee_name": [
                    "abcdef"
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }

您的原始查询是 terms 查询,相当于逻辑 OR。因此,您可以 adapt the query string 成为:

"employee_id:(abcdef OR xyz OR 123)"

值类型不起作用的地方。