简体   繁体   English

Elasticsearch 排除多个术语的关联

[英]Elasticsearch exclude association of multiple terms

I need to exclude from my search an association of different terms.我需要从我的搜索中排除不同术语的关联。 Is there any clever way to do this kind of multi-term-exclusion?有没有什么聪明的方法来做这种多词排除?

Example: say I want to get all users Except the ones whose (last name is Doe ) and (first name is John or Annie or without any first name value).示例:假设我想获取所有用户,除了那些(姓氏为Doe(名字为JohnAnnie没有任何名字值的用户)。

Expected results example:预期结果示例:

first_name | last_name | Search result
--------------------------------------
Bob        | Doe       | appears
--------------------------------------
Annie      | Doe       | excluded 
--------------------------------------
(null)     | Doe       | excluded 
--------------------------------------
John       | Foo       | appears 
--------------------------------------
(null)     | Foo       | appears 

So far the best I did was the following request, but it does not work: in our example this request will exclude everyone whose last name is Doe, whatever the first name… and I cannot understand why?到目前为止,我所做的最好的是以下请求,但它不起作用:在我们的示例中,此请求将排除姓氏为 Doe 的每个人,无论名字如何......我不明白为什么?

GET user/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "bool": {
            "must": [
              {
                "term": {
                  "last_name": "Doe"
                }
              }
            ]
          }
        },
        {
          "bool": {
            "should": [
              {
                "bool": {
                  "must": [
                    {
                      "terms": {
                        "first_name": [
                          "John",
                          "Annie"
                        ]
                      }
                    }
                  ]
                }
              },
              {
                "bool": {
                  "must_not": {
                    "exists": {
                      "field": "first_name"
                    }
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

Any help of an Elastic-sharp-eye on this will be much appreciated! Elastic-sharp-eye 对此的任何帮助将不胜感激!

In must_not array document must not match any of the clause.在 must_not 数组文档中不得匹配任何子句。 Your query basically translates to don't consider a document if last_name is Doe OR (first_name is annie or john or doesn't exists)如果 last_name 是 Doe OR(first_name 是 annie 或 john 或不存在),您的查询基本上会转化为不考虑文档

To work as AND place your query in bool用作并将您的查询放在 bool 中

{
  "query": {
    "bool": {
      "must_not": [
        {
          "bool": {
            "must": [
              {
                "match": {
                  "last_name": "Doe"
                }
              },
              {
                "bool": {
                  "should": [
                    {
                      "terms": {
                        "first_name.keyword": [
                          "John",
                          "Annie"
                        ]
                      }
                    },
                    {
                      "bool": {
                        "must_not": {
                          "exists": {
                            "field": "first_name"
                          }
                        }
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM