【问题标题】:How to put not condition on particular id's not to search in DSL query如何在特定 ID 上设置不条件以不在 DSL 查询中搜索
【发布时间】:2021-08-24 10:00:21
【问题描述】:

这是elasticsearch的字典模式 {'id': 3},{'id': 4},{'id': 5},{'id': 6}

以下是搜索“Country Owner.id.keyword”和“Territory Owner.id.keyword”的查询

  • 我需要设置一个条件,说它不应该使用 id 搜索:[3,4,5] 所以我的output 将只包含来自 6 的值 'ABC101'(如果存在)
a = {'from': 0, 'size': 200,'query': {
    'bool': {
      'should': [                              
        {
          'terms': {
            'Country Owner.id.keyword': [
              'ABC101'
            ]
          }
        },
        {
          'terms': {
            'Territory Owner.id.keyword': [
              'ABC101'
            ]
          }
        }
      ]
    }
  }
}

【问题讨论】:

    标签: elasticsearch dsl


    【解决方案1】:

    您可以在must_not 子句中使用terms query

    {
      "query": {
        "bool": {
          "should": [
            {
              "terms": {
                "Country Owner.id.keyword": [
                  "ABC101"
                ]
              }
            },
            {
              "terms": {
                "Territory Owner.id.keyword": [
                  "ABC101"
                ]
              }
            }
          ],
          "must_not": {
            "terms": {
              "id": [
                3,
                4,
                5
              ]
            }
          }
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 2021-10-23
      • 2013-11-21
      • 1970-01-01
      • 1970-01-01
      • 2014-05-09
      • 1970-01-01
      相关资源
      最近更新 更多