gpt4 book ai didi

elasticsearch - Elasticsearch 2.3.0 should查询中匹配null

转载 作者:行者123 更新时间:2023-12-02 23:00:05 25 4
gpt4 key购买 nike

我有一个字段gender,它可以有三个值:

  • 女性

gender的映射是:

"gender": {
"type": "string",
"index": "not_analyzed"
}

要匹配null 我关注this .

我使用此查询获取了 gendernull 的文档:

{
"query": {
"bool": {
"must_not": {
"exists": {
"field": "gender"
}
}
}
}
}

我想构建一个 should 查询,以便搜索结果可以返回具有 gender 字段的任何文档:

{
"query": {
"bool": {
"should": {
"bool": {
"should": [
{
"match": {
"gender": {
"query": "MALE",
"type": "boolean"
}
}
},
{
"match": {
"gender": {
"query": "FEMALE",
"type": "boolean"
}
}
}
],
"must_not": {
"exists": {
"field": "gender"
}
}
}
}
}
}
}

上面的查询给了我 0 个匹配。我应该如何为此构造正确的查询?

最佳答案

{
"query": {
"bool": {
"should": [
{
"match": {
"gender": {
"query": "MALE",
"type": "boolean"
}
}
},
{
"match": {
"gender": {
"query": "FEMALE",
"type": "boolean"
}
}
},
{
"bool": {
"must_not": {
"exists": {
"field": "gender"
}
}
}
}
]
}
}
}

或者如果我没有很好地理解这个问题,而事实上你想区分gender文档中完全没有或者字段在那里,但 null,您需要其他东西,这需要稍微更改映射:

    "gender": {
"type": "string",
"index": "not_analyzed",
"null_value": "_null_"
}

在这种情况下,查询是:

  "query": {
"bool": {
"should": [
{
"match": {
"gender": {
"query": "MALE"
}
}
},
{
"match": {
"gender": {
"query": "FEMALE"
}
}
},
{
"term": {
"gender": "_null_"
}
}
]
}
}

在这种情况下,如果你索引:

{"index":{}}
{"gender":"MALE"}
{"index":{}}
{"gender":"FEMALE"}
{"index":{}}
{"gender":null}
{"index":{}}
{"gender2":"MALE"}
{"index":{}}
{"gender":"whatever"}

它会给你返回:

     {
"_score": 0.2826735,
"_source": {
"gender": "MALE"
}
},
{
"_score": 0.2826735,
"_source": {
"gender": null
}
},
{
"_score": 0.021688733,
"_source": {
"gender": "FEMALE"
}
}

关于elasticsearch - Elasticsearch 2.3.0 should查询中匹配null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36640326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com