【问题标题】:How to put or condition in two different keys in DSL query如何在 DSL 查询中放置或设置两个不同的键
【发布时间】:2021-10-23 19:45:41
【问题描述】:

我有 dsl 查询需要返回

  • Country Owner 是字典列表
  • Territory Owner 是字典列表
  • 需要检查 id 是该字典的一部分
  • 我的查询低于哪个返回是'and',但我需要OR 操作
a = {'from': 0, 'size': 200,'query': {
    'bool': {
      'must': [
        {
          'terms': {
            'Country Owner.id.keyword': [
              'ABC101'
            ]
          }
        },
        {
          'terms': {
            'Territory Owner.id.keyword': [
              'ABC101'
            ]
          }
        }
      ]
    }
  }
}

【问题讨论】:

标签: elasticsearch dsl


【解决方案1】:

只需将must 更改为should 就可以了

a = {'from': 0, 'size': 200,'query': {
    'bool': {
      'minimum_should_match': 1,                <--- change this
      'should': [                               <--- change this
        {
          'terms': {
            'Country Owner.id.keyword': [
              'ABC101'
            ]
          }
        },
        {
          'terms': {
            'Territory Owner.id.keyword': [
              'ABC101'
            ]
          }
        }
      ]
    }
  }
}

【讨论】:

  • 为什么是 minimum_should_match,这是必需的吗?
  • 只有当你要添加过滤器或必须的子句时才有必要,但如果你只有应该,你可以放弃它......即使离开它并没有什么坏处
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多