DSL查询以从字典数组中查找元素

问题描述

我在弹性搜索中有一个名为professor的索引

  • 如果对于交叉场,我需要“与”条件

  • 对于相同的字段数组,我需要进行条件运算

  1. 我需要搜索BusinessAreaResearch的{​​{1}},这是fields(OR)语句的数组

    AND

  2. 我需要搜索AccountingRole还是Developer的条件,这是fields(OR)语句的数组

    AND

  3. 我需要搜索Tester是否符合Location(&)条件

NY

查询如下,第三个查询到了,如何添加test = [{ 'id': '1','name': 'Group1','BusinessArea': [ { 'id': '14','name': 'Accounting' },{ 'id': '3','name': 'Accounting' } ],'Designation': [ { 'id': '16','name': 'L1' },{ 'id': '20','name': 'L2' },{ 'id': '25',] },{ 'id': '2','name': 'Research' },'Role': [ { 'id': '5032','name': 'Tester' },{ 'id': '5033','name': 'Developer' } ],{ 'id': '1','name': 'Engineering' } ],'name': 'Developer' },'name': 'L2' }] }]

1 and 2

解决方法

我需要搜索“位置”是NY(&)条件

我没有添加此条件,因为示例数据中没有名为Location的字段

尝试以下查询:

搜索查询:

{
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "BusinessArea.name.keyword": [
              "Research","Accounting"
            ]
          }
        },{
          "terms": {
            "Role.name.keyword": [
              "Developer","Tester"
            ]
          }
        }
      ]
    }
  }
}

搜索结果:

"hits": [
      {
        "_index": "stof_64386038","_type": "_doc","_id": "2","_score": 2.0,"_source": {
          "id": "2","name": "Group1","BusinessArea": [
            {
              "id": "14","name": "Research"
            },{
              "id": "3","name": "Accounting"
            }
          ],"Role": [
            {
              "id": "5032","name": "Tester"
            },{
              "id": "5033","name": "Developer"
            }
          ],"Designation": [
            {
              "id": "16","name": "L1"
            },{
              "id": "20","name": "L2"
            },{
              "id": "25","name": "L2"
            }
          ]
        }
      },{
        "_index": "stof_64386038","_id": "3","_source": {
          "id": "1","name": "Engineering"
            }
          ],"name": "Developer"
            },"name": "L2"
            }
          ]
        }
      }
    ]
,

您可以像这样使用布尔查询:

content_search = es.search(index="professor",body={
    "query": {
    "bool": {
      "must": [
           {
          "terms": 
              {
            "BusinessArea.name.keyword": ["Research","Accounting"]
              }
           },{
           "terms": 
                  {
                    "Role.name.keyword": ["Developer","Tested"]
                  }
          }
      ]
    },"filter": [
        {
          "term": {
            "Location.keyword": "NY"
          }
        }
      ]
  }
})