Elasticsearch排除不符合条件的文档

Elasticsearch排除不符合条件的文档

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

我需要找到具有与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"
                    }
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

最佳答案

你近了这对我有用(请注意negated语句中的must_not文本搜索):

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

关于elasticsearch - Elasticsearch排除不符合条件的文档,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44173960/

10-10 07:17