【问题标题】:Elasticsearch Exclude documents which does not match a criteriaElasticsearch 排除不符合条件的文档
【发布时间】:2017-05-25 06:42:19
【问题描述】:

user_profile 索引具有与属性 country_iso 的分支映射

{
          "_index": "user_profile",
          "_type": "user",
          "_id": "188",
          "_score": 1.0042034,
          "_source": {
            "user_id": 188,
            "phone": "971550000322",
            "profile_set_date": "Apr 6, 2017",
            "phone_country": "AE",
            "branches": [
              {
                "id": 27,
                "brand_id": 20,
                "country_iso": "KW",
                "city_iso": "KW-02-145162",
                "area_id": 33
              },
              {
                "id": 45,
                "brand_id": 56,
                "country_iso": "AE",
                "city_iso": "AE-01-206944",
                "area_id": 18
              },
              {
                "id": 46,
                "brand_id": 56,
                "country_iso": "AE",
                "city_iso": "AE-01-206944",
                "area_id": 18
              }
            ],
            "prog_id": 13,
            "email": "971550000322@email.com"
          }
        }

我需要找到使用 country_iso=AE 而没有其他任何东西的分支机​​构映射的用户

must 和 must_not 组合对我不起作用

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "prog_id": 13
          }
        }
      ],
      "should": [
        {
          "nested": {
            "path": [
              "branches"
            ],
            "query": {
              "query_string": {
                "fields": [
                  "branches.country_iso"
                ],
                "query": "AE"
              }
            }
          }
        }
      ],
      "minimum_number_should_match": 1,
      "must_not": [
        {
          "bool": {
            "must_not": [
              {
                "nested": {
                  "path": [
                    "branches"
                  ],
                  "query": {
                    "query_string": {
                      "fields": [
                        "branches.country_iso"
                      ],
                      "query": "AE"
                    }
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    你很亲密。这对我有用(注意must_not 语句中的negated 文本搜索):

    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "prog_id": 13
              }
            },
            {
              "nested": {
                "path": [
                  "branches"
                ],
                "query": {
                  "query_string": {
                    "fields": [
                      "branches.country_iso"
                    ],
                    "query": "AE"
                  }
                }
              }
            }
          ],
          "must_not": [
            {
              "nested": {
                "path": [
                  "branches"
                ],
                "query": {
                  "query_string": {
                    "fields": [
                      "branches.country_iso"
                    ],
                    "query": "-AE"
                  }
                }
              }
            }
          ]
        }
      }
    }
    

    【讨论】:

    • 非常感谢。这似乎是完美的。我将通过更多测试来验证结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 2022-11-24
    • 1970-01-01
    • 1970-01-01
    • 2018-06-22
    • 1970-01-01
    • 2021-10-04
    相关资源
    最近更新 更多