python - 通过elasticsearch_dsl使用 “must_not exists”

标签 python elasticsearch elasticsearch-dsl

对于我的项目之一,我需要从ES索引确定缺少字段的所有记录。
请在下面查看存储在我的ES索引中的数据示例:

{
  "schema": "https://sample.org/schemas/user_v0.0.1.json",
  "barcode": "210000001",
  "birth_date": "1961-11-24", 
  "first_name": "John",
  "last_name": "Doe",
  "subscriptions": [
    {
      "end_date": "2021-03-30",
      "start_date": "2020-03-30"
    }
  ]
}, {
  "schema": "https://sample.org/schemas/user_v0.0.1.json",
  "barcode": "210000002",
  "birth_date": "1980-03-17", 
  "first_name": "Bob",
  "last_name": "Smith",
  "subscriptions": []
}, {
  "schema": "https://sample.org/schemas/user_v0.0.1.json",
  "barcode": "210000003",
  "birth_date": "1980-03-17", 
  "first_name": "Patty",
  "last_name": "Smith"
}


我想确定我的哪些用户没有任何订阅。在我的示例中,应返回“鲍勃·史密斯”和“帕蒂·史密斯”。我需要使用Python ElasticSearch DSL查询来做到这一点。

目前,我可以过滤搜索以仅检索用户,但是尽管进行了多次尝试,但我仍未找到仅获得用户“must_not” +“exists”订阅的方法。

results = Search()\
          .filter('term', schema='https://sample.org/schemas/user_v0.0.1.json')
          # complete filter with : "Must not exists subscription"
          .source('barcode')
          .scan()

谢谢你的帮助

最佳答案

我对Python DSL不熟悉,但是用于查找那些没有任何订阅的用户的REST查询是:

    {
     "query": {
    "bool": {
      "must_not": [
        {
          "nested": {
            "path": "subscriptions",
            "query": {
              "exists": {
                "field": "subscriptions"
              }
            }
          }
        }
      ]
    }
  }

关于python - 通过elasticsearch_dsl使用 “must_not exists”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60971169/

相关文章:

elasticsearch - Logstash 5 Alpha4 到 elasticsearch5 Alpha4 通信错误

elasticsearch - 查看Vega + Kibana中不是 `doc_count`的所有内容

elasticsearch - 如何在Elasticsearch中获得嵌套字段的不同值?

elasticsearch - 仅返回具有产品的类别(Elasticsearch)

python - 从字符串中提取完整的国家/地区名称并将其作为数据框列

python - 如何从 CSV 检查用户名和密码。文件

Python Django/with Microsoft Graphs - 我不断收到值错误 "state missing from auth_code_flow"

python - 如何动态训练 LSTM 模型?

elasticsearch - Elasticsearch错误的距离计算和过滤器

python-2.7 - 带有条件和范围的elasticsearch-dsl-py查询过滤器