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

How to merge two DSL query for aggregation and filter

下面是词典

test= [ { 'masterid': '1', 'name': 'Group1', 'BusinessArea': [ 'Accounting','Research'], 'Designation': [ 'L1' 'L2' ] }, { 'masterid': '2', 'name': 'Group1', 'BusinessArea': ['Research','Accounting' ], 'Role': [ { 'id': '5032', 'name': 'Tester' }, { 'id': '5033', 'name': 'Developer' } ], 'Designation': [ 'L1' 'L2' ]}, { 'masterid': '3', 'name': 'Group1', 'BusinessArea': [ 'Engineering' ], 'Role': [ { 'id': '5032', 'name': 'Developer' }, { 'id': '5033', '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"
      }
    }
  }
}