可以用Elasticsearch聚合的关键字对结果进行分组吗? [英] Is it possible to group results by a key with Elasticsearch aggregations?

查看:741
本文介绍了可以用Elasticsearch聚合的关键字对结果进行分组吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行弹性搜索查询,并希望弹性搜索将结果分组给我,而不是让我的客户端代码手动进行。查看弹性搜索文档,看起来像抗衡聚合将是我正在寻找的,但是我找不到使用它的任何示例,或者什么输出看起来要确保这是我想要的。

I'm trying to perform an Elasticsearch query and would like for Elasticsearch to group the results for me, instead of having my client code do it manually. Looking at the Elasticsearch documentation, it appears like bucketing aggregation would be what I'm looking for, but I can't find any examples that use it, or what the output would look like to be sure that's what I want.

我的问题是:是否可以通过弹性搜索中的键分组文档?如果是,如何和哪里可以找到有关如何使用查询DSL或(最好)Javadoc作为Java API的文档?

My question is: is it possible to group documents by a key in Elasticsearch? If so, how and where can I find documentation on how to do it, either using the query DSL or (preferably) the Javadoc for the Java API?

推荐答案

我想你正在尝试通过弹性搜索中的一个字段进行分组,您可以使用术语聚合

I guess you are trying to group by a field in elasticsearch, you can do it by using Terms aggregation.

这里是如何使用查询dsl,

Here is how to do using query dsl,

POST _search 
{
   "aggs": {
      "genders": {
         "terms": {
            "field": "gender"
         },
         "aggs": {
            "top_tag_hits": {
               "top_hits": {
                  "_source": {
                     "include": [
                        "include_fields_name"
                     ]
                  },
                  "size": 100
               }
            }
         }
      }
   }
}

性别是文档中的字段,
其响应可以是

and gender is field in document, Its response can be

{
    ...

    "aggregations" : {
        "genders" : {
            "buckets" : [
                {
                    "key" : "male",
                    "doc_count" : 10,
                    "tag_top_hits":{"hits":...}
                },
                {
                    "key" : "female",
                    "doc_count" : 10,
                    "tag_top_hits":{"hits":...}
                },
            ]
        }
    }
}

使用 Java api ,我添加了 tophits聚合。 (但不是查询dsl)

Using Java api, I've added tophits aggregation for your comment. (but not in query dsl)

client.prepareSearch("index").setTypes("types").addAggregation(
                AggregationBuilders.terms("agg_name").field("gender").subAggregation(
                        AggregationBuilders.topHits("documents").setSize(10)
                )
        ).execute(new ActionListener<SearchResponse>() {
            @Override
            public void onResponse(SearchResponse response) {
                Terms agg_name_aggregation=response.getAggregations().get("agg_name");
                for (Terms.Bucket bucket : agg_name_aggregation.getBuckets()) {
                    TopHits topHits=bucket.getAggregations().get("documents");
                    System.out.println("term = " + bucket.getKey());
                    // do what you want with top hits..
                }
            }

            @Override
            public void onFailure(Throwable e) {
                e.printStackTrace();
            }
        });

希望这有帮助!!

这篇关于可以用Elasticsearch聚合的关键字对结果进行分组吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆