簡體   English   中英

Elasticsearch 排除多個術語的關聯

[英]Elasticsearch exclude association of multiple terms

我需要從我的搜索中排除不同術語的關聯。 有沒有什么聰明的方法來做這種多詞排除?

示例:假設我想獲取所有用戶,除了那些(姓氏為Doe(名字為JohnAnnie沒有任何名字值的用戶)。

預期結果示例:

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

到目前為止,我所做的最好的是以下請求,但它不起作用:在我們的示例中,此請求將排除姓氏為 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"
                    }
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

Elastic-sharp-eye 對此的任何幫助將不勝感激!

在 must_not 數組文檔中不得匹配任何子句。 如果 last_name 是 Doe OR(first_name 是 annie 或 john 或不存在),您的查詢基本上會轉化為不考慮文檔

用作並將您的查詢放在 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