布尔查询中的elastic平均聚合

ugmeyewa  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(1)|浏览(418)

在elastic中,我尝试对bool查询过滤器进行平均聚合。但是我无法解析名为[query]的BaseAgregationBuilder:找不到解析器。
我的目标:
过滤processname:addcustomer和messagetype:response文档。
使用“已用时间”字段查找筛选数据的平均响应时间
我的代码

{
  "from": 0,
  "size": 20,
  "aggs": {
    "filtered_elapsed_time": {
        "query": {
            "bool": {
              "should": [
                {
                  "bool": {
                    "must": [
                      {
                        "match": {
                          "processName": "AddCustomer"
                        }
                      },
                      {
                        "match": {
                          "messageType": "Response"
                        }
                      }
                    ]
                  }
                }
              ]
            }       
      },
      "aggs": {
        "avg_et": {
          "avg": {
            "field": "elapsed_time"
          }
        }
      }
    }
  }
}

错误响应

{
  "error": {
    "root_cause": [
      {
        "type": "named_object_not_found_exception",
        "reason": "[6:18] unable to parse BaseAggregationBuilder with name [query]: parser not found"
      }
    ],
    "type": "named_object_not_found_exception",
    "reason": "[6:18] unable to parse BaseAggregationBuilder with name [query]: parser not found"
  },
  "status": 400
}
eyh26e7m

eyh26e7m1#

添加索引数据、搜索查询和搜索结果的工作示例
索引数据:

{
  "processName":"AddCustomer",
  "messageType":"Response",
   "elapsed_time":20
}
{
  "processName":"AddCustomer",
  "messageType":"Response",
   "elapsed_time":10
}

搜索查询:

{                   <-- note this
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "must": [
              {
                "match": {
                  "processName": "AddCustomer"
                }
              },
              {
                "match": {
                  "messageType": "Response"
                }
              }
            ]
          }
        }
      ]
    }
  },
  "aggs": {
    "avg_et": {
      "avg": {
        "field": "elapsed_time"
      }
    }
  }
}

搜索结果:

"hits": [
      {
        "_index": "64444060",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.36464313,
        "_source": {
          "processName": "AddCustomer",
          "messageType": "Response",
          "elapsed_time": 10
        }
      },
      {
        "_index": "64444060",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.36464313,
        "_source": {
          "processName": "AddCustomer",
          "messageType": "Response",
          "elapsed_time": 20
        }
      }
    ]
  },
  "aggregations": {
    "avg_et": {
      "value": 15.0        <-- note this
    }
  }

更新1:
您的搜索查询也可以工作,只需替换 queryfilter 关键字
如果使用过滤器聚合将当前聚合上下文缩小到一组特定的文档,则修改后的搜索查询将是:

{
  "from": 0,
  "size": 20,
  "aggs": {
    "filtered_elapsed_time": {
      "filter": {               <-- note this
        "bool": {
          "should": [
            {
              "bool": {
                "must": [
                  {
                    "match": {
                      "processName": "AddCustomer"
                    }
                  },
                  {
                    "match": {
                      "messageType": "Response"
                    }
                  }
                ]
              }
            }
          ]
        }
      },
      "aggs": {
        "avg_et": {
          "avg": {
            "field": "elapsed_time"
          }
        }
      }
    }
  }
}

相关问题