如何合并两个DSL查询以进行聚合和过滤

问题描述

  • 我需要搜索研究或会计业务领域,这是字段(OR)语句的数组
  • 我需要搜索“角色是开发人员还是测试人员”,条件是这是字段(或)语句的数组
  • 我想获取所有名称的BusinessArea的masterid数,designationNames,Role
  • 名称过滤器为“ Group1”

下面是字典

test= [ { 'masterid': '1','name': 'Group1','BusinessArea': [ 'Accounting','Research'],'Designation': [ 'L1' 'L2' ] },{ 'masterid': '2','BusinessArea': ['Research','Accounting' ],'Role': [ { 'id': '5032','name': 'Tester' },{ 'id': '5033','name': 'Developer' } ],'Designation': [ 'L1' 'L2' ]},{ 'masterid': '3','BusinessArea': [ 'Engineering' ],'name': 'Developer' },'name': 'Developer','parentname': '' } ],'Designation': [ 'L1' 'L2' ]}]

下面是聚合函数


    {
      "size": 0,"aggs": {
        "countNames": {
          "terms": {
            "field": "BusinessArea.keyword"
          }
        },"designationNames": {
          "terms": {
            "field": "Designation.keyword"
          }
        },"Role": {
          "terms": {
            "field": "Role.name.keyword"
          }
        }
    
      }
    }

下面是过滤查询


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

"filter": [
   "term": {
    "name.keyword": "Group1"}]

我需要将查询输出都合并到一起

解决方法

好开始!!!现在,您可以像这样简单地组合所有这些片段:

{
  "size": 0,"query": {
    "bool": {
      "filter": [
        {
          "term": {
            "name.keyword": "Group1"
          }
        },{
          "terms": {
            "BusinessArea.keyword": [
              "Research","Accounting"
            ]
          }
        },{
          "terms": {
            "Role.name.keyword": [
              "Developer","Tester"
            ]
          }
        }
      ]
    }
  },"aggs": {
    "countNames": {
      "terms": {
        "field": "BusinessArea.keyword"
      }
    },"designationNames": {
      "terms": {
        "field": "Designation.keyword"
      }
    },"Role": {
      "terms": {
        "field": "Role.name.keyword"
      }
    }
  }
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...