Elasticsearch聚合,获取存储桶中的其他字段

时间:2015-06-02 08:11:49

标签: elasticsearch elastica

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

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

我得到的结果如下: Elasicsearch results

我怎样才能进入&#34;水桶&#34;关于&#34;模型&#34;从结果集中指定另一个字段。我想引用Makes所以结果看起来像这样:

public @interface MyAnnotation {
    String value() default "";
    Class[] exceptionList;
}

@MyAnnotation(value="hello", exceptionList={TimeOutException.class})
public void method() {}


@Aspect
public class MyAspect {
    @Around("@annotation(MyAnnotation)")
    public Object handle(ProceedingJoinPoint joinPoint, MyAnnotation myAnnotation) {
        System.out.println(myAnnotation.exceptionList); // should print out TimeOutException
    }
}

1 个答案:

答案 0 :(得分: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"
                }
              }
            }
          }
        }
      }
    }
  }
}