Elasticsearch,Kibana group by / aggregate documents

时间:2016-03-09 15:49:27

标签: elasticsearch group-by aggregate kibana

在Elastisearch索引中,我正在收集用户的评分。也就是说,在索引中 localhost:9200 / content_ratings / 我们有 content_rating 类型的文档。此文档类型包含以下字段:

  • 标题
  • 作者日期
  • URL
  • 等级
  • ratedBy

假设我们的索引包含以下文档:

{
  "title": "Data Quality", "author": "John Queue", "url": "/news/2015/data-quality", "rating":3, "ratedBy": "userA"
}
{
  "title": "Data Quality", "author": "John Queue", "url": "/news/2015/data-quality", "rating":1, "ratedBy": "userB"
}
{
  "title": "User Story Estimations", "author": "Abby Edye", "url": "/news/2014/story-splitting", "rating":5, "ratedBy": "userC"
}

我需要一份报告(Elasticsearch 1.0,Kibana 3),它会显示每个评级项目并计算其平均评分。所以我们需要'按网址分组',并且有类似的内容:

"title": "Data Quality", "author": "John Queue", "url": "/news/2015/data-quality", "avgRating":2
"title": "User Story Estimations", "author": "Abby Edye", "url": "/news/2014/story-splitting", "avgRating":5

a)ES中有没有办法使用'group by'等效结构?

b)ES中有没有办法查询a)(另一个)查询返回的结果? - 我这是问这个问题,因为根据汇总的结果,在另一份报告(Kibana小组)中,我需要算一下这些项目的评分是好(4)还是优秀(5)?

我是ES / kibana的新手,经过大量谷歌搜索后,最接近我要求的是以下链接:  How do I group documents in elasticsearch by a single field? https://www.elastic.co/guide/en/elasticsearch/reference/1.3/search-aggregations-metrics-top-hits-aggregation.html

我已经尝试了 terms facet ,这是我目前的设置(ES 1.0,kibana3)所暗示支持的,但远不及我所需要的......

1 个答案:

答案 0 :(得分:2)

是的,你可以使用" aggs"在elasticsearch中执行groupby操作。

我在这里有一个样本你可以知道如何制作你的: 在这里,我使用了嵌套的aggrigations(groupby)来获得每个国家中处于特殊年龄段的人的平均年龄。

GET customer/_search
{
"size": 0,
    "aggs": {
        "group_by_state": {
            "range": {
                "field": "age",
                "ranges": [
                        { "from": 20,"to": 40 },
                        { "from": 40, "to": 60 },
                        { "from": 60, "to": 100}
                ]
            },
            "aggs": {  
                "group_by_nationality": {
                    "terms": { 
                        "field": "nationality.keyword"
                    },
                    "aggs": {
                      "average_age": {
                            "avg": {
                                "field": "age"
                            }
                        }
                    }
                }
            }
        }              
    }
}