【问题标题】:How to merge two DSL query for aggregation and filter如何合并两个 DSL 查询以进行聚合和过滤
【发布时间】:2021-02-17 23:19:51
【问题描述】:
  • 我需要搜索 BusinessArea,即 Research 或 Accounting,这是字段数组 (OR) 语句
  • 我需要搜索角色是开发人员或测试人员条件这是字段数组(OR)语句
  • 我想获取 BusinessArea、designationNames、Role 的 masterid 计数,即所有名称
  • 名称过滤器为“Group1”

下面是字典

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"}]

我需要合并两者的查询和输出

【问题讨论】:

  • 我尝试合并 RequestError: RequestError(400, 'parsing_exception', 'Expected [START_OBJECT] but found [START_ARRAY]')
  • search=es.search(index="4", body=quer)

标签: python elasticsearch elasticsearch-aggregation elasticsearch-query


【解决方案1】:

美好的开始!!!现在您可以像这样简单地组合所有这些 sn-ps:

{
  "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"
      }
    }
  }
}

【讨论】:

  • 当我接受这个时,我得到了 RequestError: RequestError(400, 'parsing_exception', 'Expected [START_OBJECT] but found [START_ARRAY]')
  • 不介意我问,但您确定您正确复制/粘贴了查询吗?你能分享完整的错误吗?你能展示你的python代码组装查询吗,这可能是罪魁祸首?
  • 我的两个查询都在工作 quer = {'query': {'bool': {'must': [{'terms': {'BusinessArea.keyword': ['Silver', 'Gold']}}], 'filter': [{'term': {'name.keyword': 'Group1'}}]}}} q = { "size": 0, "aggs": { "BusinessArea": { "terms": { "field": "BusinessArea.keyword" } }, "Designation": { "terms": { "field": "Designation.keyword" } }, "Role": { "terms": { "field": "Role.name.keyword" } } } }
  • 请显示您的代码(更新您的问题),错误一定在某处
  • 是的,顺序无关紧要。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-19
  • 2017-05-24
  • 2019-10-19
  • 2019-05-06
  • 2015-08-30
  • 1970-01-01
  • 2020-12-24
相关资源
最近更新 更多