Elasticsearch 聚合,在桶中获取额外的字段

Elasticsearch aggregations, get additional field in bucket

我查询 ES 索引以过滤结果并按所选术语获取聚合。示例查询如下:

GET buyer_requests/vehicle_requests/_search
{
  "query": {
    "filtered": {
        "filter": {
          "and": [
            {
              "terms": {
                "vehicle.make.raw": [
                  "Audi",
                  "BMW",
                  "Chevrolet"
                ]
              }
            },
            {
              "range": {
                "style.price": {
                  "gte": 15000,
                  "lte": 20000
                }
              }
            },
            {
              "geo_distance": {
                "distance": "20000km",
                "info.pin": {
                  "lat": 42,
                  "lon": 21
                }
              }
            }
          ]
        }
    }
  }, 
  "aggs": {
    "makes": {
      "filter": {
        "range": {
          "style.price": {
            "gte": 5000,
            "lte": 40000
          }
        }
      },
      "aggs": {
        "makes": {
          "terms": {
            "field": "vehicle.make.raw",
            "order": {
              "_term": "asc"
            }
          }
        }
      }
    },
    "model": {
      "filter": {
        "and": [
          {
            "terms": {
              "vehicle.make.raw": [
                "Audi",
                "BMW",
                "Chevrolet"
              ]
            }
          }
        ]
      },
      "aggs": {
        "models": {
          "terms": {
            "field": "vehicle.model.raw",
            "size": 10,
            "order": {
              "_term": "asc"
            }
          }
        }
      }
    }
  }
}

我得到的结果是这样的:

如何在 "buckets" 部分的 "models" 术语中从结果集中获取另一个字段。我想参考 Makes 所以结果看起来像这样:

"model": {
   "doc_count": 7,
   "models": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
         {
            "key": "3 Series",
            "make": "bmw",                  <----------- this key
            "doc_count": 3
         },
         {
            "key": "4 Series",
            "make": "bmw",                  <----------- this key
            "doc_count": 4
         },
         {
           "key": "Camaro",
           "make": "chevrolet",             <----------- this key
           "doc_count": 2
         }
      ]
   }
}

您需要将 models 聚合移动为 make 聚合的子聚合,并稍微重新安排 filter 聚合。结果在语法上不会像您期望的那样,但在语义上您将获得所需的数据。

GET buyer_requests/vehicle_requests/_search
{
  "query": {
    "filtered": {
      "filter": {
        "and": [
          {
            "terms": {
              "vehicle.make.raw": [
                "Audi",
                "BMW",
                "Chevrolet"
              ]
            }
          },
          {
            "range": {
              "style.price": {
                "gte": 15000,
                "lte": 20000
              }
            }
          },
          {
            "geo_distance": {
              "distance": "20000km",
              "info.pin": {
                "lat": 42,
                "lon": 21
              }
            }
          }
        ]
      }
    }
  },
  "aggs": {
    "makes": {
      "filter": {
        "and": [
          {
            "terms": {
              "vehicle.make.raw": [
                "Audi",
                "BMW",
                "Chevrolet"
              ]
            }
          },
          {
            "range": {
              "style.price": {
                "gte": 5000,
                "lte": 40000
              }
            }
          }
        ]
      },
      "aggs": {
        "makes": {
          "terms": {
            "field": "vehicle.make.raw",
            "order": {
              "_term": "asc"
            }
          },
          "aggs": {
            "models": {
              "terms": {
                "field": "vehicle.model.raw",
                "size": 10,
                "order": {
                  "_term": "asc"
                }
              }
            }
          }
        }
      }
    }
  }
}