Elasticsearch之Nested Aggregation,elasticsearch


                                                  (这是一个小系列:请戳:ElasticsearchNested(嵌套)系列,查看其他nested相关文章)

             In the same way as we need to use the special nested query to gain access to nested objects at search time, the dedicated nested aggregation allows

 us to aggregate fields in nested objects:

  与在检索时需要使用特定的nested查询来接触nested object一样,特定的nested聚合同样能让我们对nested object内的字段进行聚合:

curl -XGET 'localhost:9200/my_index/blogpost/_search?search_type=count' -d '
{
  "aggs":{
     "comments":{①
	    "nested":{
		   "path":"comments"
		},
		"aggs":{
		   "by_month":{
		      "date_histogram":{②
			      "field":"comments.date",
				  "interval":"month",
				  "format":"yyyy-MM"
			  },
			  "aggs":{
			     "avg_stars":{
				    "avg":{③
					   "field":"comments.stars"
					}
				 }
			  }
		   }
		}
	 }
  }
}

①:The nested aggregation “steps down” into the nested comments object.

nested 聚合进入nested评论对象。

②:Comments are bucketed into months based on the comments.date field

   评论基于comments.date字段聚合成月

③:The average number of stars is calculated for each bucket.

对于每一个簇,计算星级 的平均值。

The results show that aggregation has happened at the nested document level:

结果表明,聚合的确发生在nested文本层:

...
"aggregations": {
  "comments": {
     "doc_count": 4, 
     "by_month": {
        "buckets": [
           {
              "key_as_string": "2014-09",
              "key": 1409529600000,
              "doc_count": 1, 
              "avg_stars": {
                 "value": 4
              }
           },
           {
              "key_as_string": "2014-10",
              "key": 1412121600000,
              "doc_count": 3, 
              "avg_stars": {
                 "value": 2.6666666666666665
              }
           }
        ]
     }
  }
}

reverse_nested Aggregation

反嵌套聚合

 

A nested aggregation can access only the fields within the nested document. It can’t see fields in the root document or in a different 

nested document. However, we can step out of the nested scope back into the parent with a reverse_nested aggregation.

一个nested聚合只能接入nested文本内的字段,它不能看到根文本或者不同nested文本内的字段。但是,我们可以通过一个反嵌套聚合跳出nested局域进入父层。

For instance, we can find out which tags our commenters are interested in, based on the age of the commenter. The comment.age is

 a nested field, while the tags are in the root document:

比如我们基于评论者的年龄找出哪些标签是评论者感兴趣的。comment.age是一个nested字段,而tags位于根文本中:

curl -XGET 'localhost:9200/my_index/blogpost/_search?search_type=count' -d '
{
  "aggs":{
     "comments":{
	    "nested":{①
		   "path":"comments"
		},
		"aggs":{
		  "age_group":{
		      "histogram":{②
			     "field":"comments.age",
				 "interval":10
			  },
			  "aggs":{
			     "blogposts":{
				    "reverse_nested":{},③
					"aggs":{
					   "tags":{
					      "terms":{④
						     "field":"tags"
						  }
					   }
					}
				 }
			  }
		  }
		}
	  }
    }		
}

②:The histogram agg groups on the comments.age field, in buckets of 10 years.

histogram(直方图)聚合在comments.age字段上分组,每10年一组。

③:The reverse_nested agg steps back up to the root document.

reverse_nested聚合跳转回根文本。

④:The terms agg counts popular terms per age group of the commenter.

terms聚合计算每个年龄组的流行terms

The abbreviated results show us the following:

下面是简化结果:

..
"aggregations": {
  "comments": {
     "doc_count": 4, 
     "age_group": {
        "buckets": [
           {
              "key": 20, 
              "doc_count": 2, 
              "blogposts": {
                 "doc_count": 2, 
                 "tags": {
                    "doc_count_error_upper_bound": 0,
                    "buckets": [ 
                       { "key": "shares",   "doc_count": 2 },
                       { "key": "cash",     "doc_count": 1 },
                       { "key": "equities", "doc_count": 1 }
                    ]
                 }
              }
           },
...

When to Use Nested Objects

何时使用nested 对象。

Nested objects are useful when there is one main entity, like our blogpost, with a limited number of closely related but less important entities, 

such as comments. It is useful to be able to find blog posts based on the content of the comments, and the nested query and filter provide for

 fast query-time joins.

当有一个主要的实体,就像blogpost(博客文章),还有有限的一些相关但是没这么重要的其他实体,比如comments(评论)nested对象就显得非常有用。基于评论的内容找到博客文章是有价值的。同时为了快速查询时间拼接,还提供了nested查询和过滤。

The disadvantages of the nested model are as follows:

nested模式的缺点如下:

1.To add, change, or delete a nested document, the whole document must be reindexed. This becomes more costly the more nested

 documents there are.

1.为了增加,改变或者删除一个nested文本,整个文本必须重新建索引。nested文本越多,这就会变得代价越大。

2.Search requests return the whole document, not just the matching nested documents. Although there are plans afoot to support returning 

the best -matching nested documents with the root document, this is not yet supported.

2.检索请求返回整个文本,而不仅是匹配的nested文本。尽管有计划正在执行以能够支持返回根文本的同时返回最匹配的nested文本,但目前还未实现。



原文:http://www.elastic.co/guide/en/elasticsearch/guide/master/nested-aggregation.html

相关内容