python-3.x - Elasticsearch DSL或查询格式

标签 python-3.x elasticsearch elasticsearch-dsl-py

我有多个文件的索引。这些文档包含以下字段:

  • 名称
  • adhar_number
  • pan_number
  • acc_number

  • 我想创建一个elasticsearch dsl查询。对于此查询,有两个输入可用,例如adhar_number和pan_number。此查询应匹配或与此相关的条件

    示例:如果一个文档仅包含提供的adhar_number,那么我也希望该文档。

    我有一本字典,其中包含以下内容(my_dict):
    {
      "adhar_number": "123456789012",
      "pan_number": "BGPPG4315B"
    }
    

    我尝试如下:
    from elasticsearch import Elasticsearch
    from elasticsearch_dsl import Search
    
    es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
    s = Search(using=es, index="my_index")
    for key, value in my_dict.items():
       s = s.query("match", **{key:value})
    
    print(s.to_dict())
    response = s.execute()
    print(response.to_dict())
    

    它创建以下查询:
    {
      'query': {
        'bool': {
          'must': [
            {
              'match': {
                'adhar_number': '123456789012'
              }
            },
            {
              'match': {
                'pan_number': 'BGPPG4315B'
              }
            }
          ]
        }
      }
    }
    

    上面的代码为我提供了带有 AND条件而不是或Condition 的结果。

    请给我建议包括OR条件的好建议。

    最佳答案

    要修复ES查询本身,您所需要做的就是使用“应该”而不是“必须”:

    {
      'query': {
        'bool': {
          'should': [
            {
              'match': {
                'adhar_number': '123456789012'
              }
            },
            {
              'match': {
                'pan_number': 'BGPPG4315B'
              }
            }
          ]
        }
      }
    }
    

    要在python中实现此目的,请参见the docs中的以下示例。默认逻辑为AND,但是您可以将其替代为OR,如下所示。

    Query combination Query objects can be combined using logical operators:

    Q("match", title='python') | Q("match", title='django')
    # {"bool": {"should": [...]}}
    
    Q("match", title='python') & Q("match", title='django')
    # {"bool": {"must": [...]}}
    
    ~Q("match", title="python")
    # {"bool": {"must_not": [...]}} 
    

    When you call the .query() method multiple times, the & operator will be used internally:

    s = s.query().query() print(s.to_dict())
    # {"query": {"bool": {...}}}
    

    If you want to have precise control over the query form, use the Q shortcut to directly construct the combined query:

    q = Q('bool',
        must=[Q('match', title='python')],
        should=[Q(...), Q(...)],
        minimum_should_match=1 ) s = Search().query(q)
    


    所以你想要像
    q = Q('bool', should=[Q('match', **{key:value})])
    

    关于python-3.x - Elasticsearch DSL或查询格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52401975/

    相关文章:

    python - 如果文本改变了 the 的大小并且 widget 改变了大小

    python-3.x - service_identity 已安装但无法正常工作

    elasticsearch - 通过在另一个索引中找到的时间戳来过滤/聚合时间序列数据的一个elasticsearch索引

    elasticsearch - 如何在Elasticsearch中使用按国家/地区的最大聚合来获取最大值和ID

    python - 在elasticsearch响应中获取@timestamp值

    python - 使用 "django-elasticsearch-dsl"将 ElasticSearch 连接到 Django 导致在尝试创建/重建索引时出现 ConnectionError

    python - 使用elasticsearch遍历查询的所有结果

    python - Python 3 中的 __phello__ 是什么?

    python-3.x - 如何通过谷歌云功能触发云存储中的特定扩展文件

    c# - NEST - 使用 GET 而不是 POST/PUT 进行搜索