繁体   English   中英

是否可以通过Elasticsearch聚合的键对结果进行分组?

[英]Is it possible to group results by a key with Elasticsearch aggregations?

我正在尝试执行Elasticsearch查询,并希望Elasticsearch为我分组结果,而不是让我的客户端代码手动执行。 看看Elasticsearch 文档 ,它看起来像是我正在寻找的存储聚合,但我找不到任何使用它的示例,或者输出看起来是什么样的,以确保这就是我想要的。

我的问题是:是否可以通过Elasticsearch中的密钥对文档进行分组? 如果是这样,我如何以及在哪里可以找到有关如何操作的文档,使用查询DSL或(最好)Java API的Javadoc?

我猜你试图通过elasticsearch中的一个字段进行分组,你可以通过使用术语聚合来实现。

以下是使用查询dsl的方法,

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

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

{
    ...

    "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中)

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();
            }
        });

希望这可以帮助!!

使用聚合函数进行分组

    client.prepareSearch(indexName).setTypes(documentType).addAggregation
    (AggregationBuilders.terms("agg_name").field("gender").subAggregation(
    AggregationBuilders.topHits("documents").setSize(10))).execute(new ActionListener<SearchResponse>()
    {
        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");

                SearchResponse response1 = client
                        .prepareSearch(indexName)

                        .setQuery(QueryBuilders.termQuery("gender",bucket.getKey()))
                        .addAggregation(
                                AggregationBuilders.max("salary").field(
                                        "salary")).execute().actionGet();

                for (Aggregation avgAggs : response1.getAggregations())
                {
                    Max avg = (Max) avgAggs;

                    double maxValue = avg.getValue();

                    System.out.println("Avg Value => " + maxValue);
                }


             //   System.out.println("term = " + bucket.getKey());

             //   System.out.println("count =" + bucket.getDocCount());

              //  System.out.println(topHits.getHits());


                for (SearchHit hit: topHits.getHits())
                {

                    System.out.println(hit.getSource());
                }

            }

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM