【问题标题】:Elastic search ---: MUST_NOT query not working弹性搜索 ---:MUST_NOT 查询不起作用
【发布时间】:2020-01-08 09:04:08
【问题描述】:

我有一个查询,我想在其中添加一个 must_not 子句,该子句将丢弃所有具有某个字段的空白数据的记录。我尝试了很多方法,但都没有奏效。当我使用其他特定字段发出相同的查询(如下所述)时,它可以正常工作。

此查询应获取所有没有“registrationType1”字段为空/空白的记录

query:
{
"size": 20,
"_source": [
"registrationType1"
],
"query": {
"bool": {
"must_not": [
{
"term": {
"registrationType1": ""
}
}
]
}
}
}

下面的结果仍然包含具有空值的“registrationType1”

results:

**"_source": {
"registrationType1": ""}}
, * {
"_index": "oh_animal",
"_type": "animals",
"_id": "3842002",
"_score": 1,
"_source": {
"registrationType1": "A&R"}}
, * {
"_index": "oh_animal",
"_type": "animals",
"_id": "3842033",
"_score": 1,
"_source": {
"registrationType1": "AMHA"}}
, * {
"_index": "oh_animal",
"_type": "animals",
"_id": "3842213",
"_score": 1,
"_source": {
"registrationType1": "AMHA"}}
, * {
"_index": "oh_animal",
"_type": "animals",
"_id": "3842963",
"_score": 1,
"_source": {
"registrationType1": ""}}
, * {
"_index": "oh_animal",
"_type": "animals",
"_id": "3869063",
"_score": 1,
"_source": {
"registrationType1": ""}}**

上述字段的 PFB 映射

"registrationType1": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
}

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    您需要使用keyword 子字段才能执行此操作:

    {
      "size": 20,
      "_source": [
        "registrationType1"
      ],
      "query": {
        "bool": {
          "must_not": [
            {
              "term": {
                "registrationType1.keyword": ""       <-- change this
              }
            }
          ]
        }
      }
    }
    

    【讨论】:

    • 您所做的是查询registrationType1 而不是registrationType1.keyword
    • 其实我都试过了,上面的描述忘记说了
    • 我刚刚在我的本地 ES 上重新创建了您的示例,它与上面查询 registrationType1.keyword 的查询一起使用,没有收到任何带有 "registrationType1": "" 的文档
    • 你用的是哪个版本的ES?
    • 在 6 和 7 上测试过,这并不重要,因为它在两个版本上的工作方式相同。
    【解决方案2】:

    如果没有在文本字段上指定任何文本值,则基本上没有什么可以分析并相应地返回文档。

    类似地,如果您删除must_not 并将其替换为must,它将显示空结果。

    您可以做的是,查看您的映射,在 keyword 字段上查询 must_not。不会分析关键字字段,这样您的查询将按预期返回结果。

    查询

    POST myemptyindex/_search
    {
      "query": {
        "bool": {
          "must_not": [
            {
              "term": {
                "registrationType1.keyword": ""
              }
            }
          ]
        }
      }
    }
    

    希望这会有所帮助!

    【讨论】:

    • 我已经尝试添加“关键字”,但没有成功,抱歉忘记提及了。
    【解决方案3】:

    我使用的是弹性搜索 7.2 版, 我复制了您的数据并提取到我的弹性索引中,并尝试使用和不使用 .keyword 进行查询。

    在字段名称中使用“.keyword”时,我得到了想要的结果。它没有返回具有registrationType1=""的文档。

    注意 - 不使用“.keyword”时查询不起作用

    我在下面添加了我的示例代码,看看是否有帮助。

    from elasticsearch import Elasticsearch
    es = Elasticsearch()
    es.indices.create(index="test", ignore=400, body={
    "mappings": {
        "_doc": {
            "properties": {
                "registrationType1": {
                    "type": "text",
                    "field": {
                        "keyword": {
                            "type": "keyword"
                        }
                    }
                }
            }
        }
    }
    })
    
    data = {
    "registrationType1": ""
    }
    
    es.index(index="test",doc_type="_doc",body=data,id=1)
    search = es.search(index="test", body={
    "size": 20,
    "_source": [
        "registrationType1"
    ],
    "query": {
        "bool": {
        "must_not": [
            {
                "term": {
                    "registrationType1.keyword": ""
                }
            }
        ]
        }
    }
    })
    
    print(search)
    

    执行上述操作不应返回任何结果,因为我们正在为该字段插入空

    【讨论】:

      【解决方案4】:

      映射本身存在一些问题,我删除了索引并用新的映射重新索引它,它现在可以工作了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-12-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-25
        相关资源
        最近更新 更多