elasticsearch 当解释有错误时,有效性怎么可能是真的?

ncecgwcz  于 2023-05-22  发布在  ElasticSearch
关注(0)|答案(1)|浏览(91)

我运行的是ES 7.17.10,我有一个简单的索引,Map如下:

curl -XPUT -H 'Content-Type: application/json' 127.0.0.1:9200/test_index -d '{
    "settings": {
        "number_of_shards": 2,
        "number_of_replicas": 1
    },
    "mappings": {
        "properties": {
            "field1": { "type": "integer" },
            "field2": {
                "type": "nested",
                "properties": {
                    "nested1": { "type": "keyword" },
                    "nested2": { "type": "integer" }
                }
            }
        }
    }
}'

当我尝试验证查询字符串时,valid部分为true,但解释包含错误,这正常吗?

# curl '127.0.0.1:9200/test_index/_validate/query?pretty&explain&q=field1:"test"'
{
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "valid": true,
  "explanations": [
    {
      "index": "test_index",
      "valid": true,
      "explanation": "MatchNoDocsQuery(\"failed [field1] query, caused by number_format_exception:[For input string: \"test\"]\")"
    }
  ]
}
bt1cpqcv

bt1cpqcv1#

validation:验证一个可能开销很大的查询,而不执行它。
说明:返回有关特定文档与查询匹配(或不匹配)的原因的信息。
您的查询有效,查询结果为空集。解释信息告诉你为什么你得到空集,因为你通过一个字符串参数查询一个整数字段。ES可以处理这种情况,不会抛出异常。我认为ES不是强类型的数据库引擎。

{{es}}/vali/_search?pretty&q=field1:"test"

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 0,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    }
}

另一方面,当索引错误类型的数据时,ES将抛出异常。

{{es}}/vali/_doc

{
  "field1":aaa
}

{
    "error": {
        "root_cause": [
            {
                "type": "mapper_parsing_exception",
                "reason": "failed to parse field [field1] of type [integer] in document with id 'mjfPJ4gBNMihXAkDlss_'. Preview of field's value: 'aaa'"
            }
        ],
        "type": "mapper_parsing_exception",
        "reason": "failed to parse field [field1] of type [integer] in document with id 'mjfPJ4gBNMihXAkDlss_'. Preview of field's value: 'aaa'",
        "caused_by": {
            "type": "number_format_exception",
            "reason": "For input string: \"aaa\""
        }
    },
    "status": 400
}

相关问题