valid be true while explanation has errors? – 说明有错误时有效是真的吗?

huangapple go评论66阅读模式
英文:

How could valid be true while explanation has errors?

问题

我正在运行 ES 7.17.10,我有一个具有以下映射的简单索引:

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\"]\")"
    }
  ]
}

(Note: I've provided the translations for the code portions as requested.)

英文:

I'm running ES 7.17.10 and I have a simple index with the following mapping:

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" }
                }
            }
        }
    }
}'

When I try to validate a query string, the valid part is true, but the explanation contains an error, is that normal?

# 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\"]\")"
    }
  ]
}

答案1

得分: 1

validation: 验证一个潜在昂贵的查询,而不执行它。

explanation: 返回有关特定文档与查询匹配(或不匹配)的原因信息。

您的查询是有效的,但查询结果为空集。解释信息告诉您为什么会得到空集,因为您用字符串参数查询整数字段。而且 Elasticsearch 可以处理这种情况,不会抛出异常。我认为 Elasticsearch 不是强类型的数据库引擎。

{{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": []
}
}

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

{{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
}

英文:

validation: Validates a potentially expensive query without executing it.

explaination: Returns information about why a specific document matches (or doesn’t match) a query.

Your query is vaild, and the query result is empty set. And explaination info tells you why you get empty set because you query an integer field by a string param. And ES can handle the situation and will not throw an exception. I think ES is not strongly-type database engine.

{{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": []
    }
}

On the other hand, when you index wrong type data, ES will throw an exception.

{{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
}

huangapple
  • 本文由 发表于 2023年5月17日 09:54:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76268105.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定