【问题标题】:How to filter not in elasticsearch DSL query如何在 elasticsearch DSL 查询中过滤
【发布时间】:2021-08-02 17:56:01
【问题描述】:

DSL查询如下

 {'from': 0, 'size': 10, 'aggs': 
     {'products': {'terms': {'field': 'softwares.name.keyword', 'order': {'_key': 'desc'}}}, 
      'color': {'terms': {'field': 'white.name.keyword', 'order': {'_key': 'desc'}}},
      'types': {'terms': {'field': 'mercedez.name.keyword', 'order': {'_key': 'desc'}}}},
     'query': {'bool': {'must': {'match_all': {}}, 
     'filter': [{'term': {'name.keyword': 'Germany'}}]}}}

  • 在上面的查询中过滤掉名字是德国'filter': [{'term': {'name.keyword': 'Germany'}

我的问题?

  • 如何反其道而行之。 filter not

【问题讨论】:

  • 请在下面查看我的答案。 @sim

标签: elasticsearch dsl


【解决方案1】:

您可以将 must_not 查询与 bool 查询一起使用。

选项 1:使用过滤器

{
  "query": {
    "bool": {
      "must": [
        {
          "match_all": {}
        }
      ],
      "filter": [
        {
          "bool": {
            "must_not": [
              {
                "term": {
                  "name.keyword": {
                    "value": "Germany"
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

选项 2:不带过滤器(但此处德国查询也将考虑用于分数计算)

{
  "query": {
    "bool": {
      "must": [
        {
          "match_all": {}
        }
      ],
      "must_not": [
        {
          "term": {
            "name.keyword": {
              "value": "Germany"
            }
          }
        }
      ]
    }
  }
}

【讨论】:

    猜你喜欢
    • 2021-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多