POST / aggstest / test / 1
{
"categories": [
{
"type": "book",
"words": [
{"word":"storm","count":277},
{"word":"pooh","count":229}
]
},
{
"type": "magazine",
"words": [
{"word":"vibe","count":100},
{"word":"sunny","count":50}
]
}
]
}
POST / aggstest / test / 2
{
"categories": [
{
"type": "book",
"words": [
{"word":"rain","count":160},
{"word":"jurassic park","count":150}
]
},
{
"type": "megazine",
"words": [
{"word":"tech","count":200},
{"word":"homes","count":30}
]
}
]
}
GET / aggstest / test / _search
{
"size": 0,
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"term": {
"categories.type": "book"
}
},
{
"term": {
"categories.words.word": "storm"
}
}
]
}
}
}
},
"aggs": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"term": {
"categories.type": "book"
}
}
]
}
},
"aggs": {
"book_category": {
"terms": {
"field": "categories.words.word",
"size": 10
}
}
}
}
},
"post_filter": {
"term": {
"categories.type": "book"
}
}
}
结果
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0,
"hits": []
},
"aggregations": {
"filtered": {
"doc_count": 1,
"book_category": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "pooh",
"doc_count": 1
},
{
"key": "storm",
"doc_count": 1
},
{
"key": "sunny",
"doc_count": 1
},
{
"key": "vibe",
"doc_count": 1
}
]
}
}
}
}
========================
预期的aggs结果集不应包括"晴天"和" vibe"因为它是"杂志"类型。
我使用了过滤查询和post_filter,但我不能只得到#34; book"输入aggs结果。
答案 0 :(得分:1)
您应用的所有过滤器(查询中和聚合内)仍会返回整个categories
文档。此文档包含所有4个单词,是聚合的范围。因此,你总能获得所有4个桶。
据我所知,在Elasticsearch 2.0版本中,reducers会引入一些在服务器端操作存储区的方法。
您现在可以使用的是更改映射,以便categories
为nested object。因此,您将能够独立查询它们并使用嵌套聚合进行相应聚合。将对象类型更改为嵌套需要重新编制索引。
另请注意,post-filters不适用于任何聚合。当您需要在比返回的命中更广泛的范围内聚合时,它们用于过滤原始查询而不影响聚合。
还有一件事,如果您已经在查询中使用过滤器,则无需将其置于聚合中,范围已经过滤。