Elasticsearch排除与条件不匹配的文档

时间:2017-05-25 06:42:19

标签: 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": "971550000322@email.com"
          }
        }

我需要找到使用country_iso = AE而不是其他任何内容进行分支映射的用户

必须和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"
                    }
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

1 个答案:

答案 0 :(得分:1)

你很亲密。这个对我有用(注意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"
              }
            }
          }
        }
      ]
    }
  }
}
相关问题