elasticsearch aggregation - 为什么匹配所有查询不返回更具体查询的键?

时间:2015-05-11 09:44:52

标签: elasticsearch

我正在做一些聚合。但结果完全不是我所期望的,似乎它们没有聚合在索引中匹配我的查询的所有文档,在这种情况下 - 它有什么用处?

举个例子,首先我做这个查询:

{"index":"datalayer","type":"analysis2","body":{"query":{
        "match_all" : {}
    },
    "aggs" : {
        "objects" : {
            "terms" : {
              "field" : "action"
            }
        }
    }
}}

,结果是500次点击,汇总如下:

"aggregations": {
    "objects": {
        "buckets": [
            {
                "key": "thing",
                "doc_count": 278
            },
            {
                "key": "hover",
                "doc_count": 273
            },
            {
                "key": "embedded",
                "doc_count": 57
            },
            {
                "key": "view",
                "doc_count": 50
            },
            {
                "key": "widgets",
                "doc_count": 49
            },
            {
                "key": "hovered",
                "doc_count": 20
            },
            {
                "key": "widgetembed",
                "doc_count": 20
            },
            {
                "key": "products",
                "doc_count": 19
            },
            {
                "key": "create",
                "doc_count": 15
            },
            {
                "key": "image",
                "doc_count": 13
            }
        ]
    }
}

这一切都很好但我知道我有一些关键应该激活的地方。 所以如果我再进行查询

{"index":"datalayer","type":"analysis2","body":{"query":{
        "bool": {
        "must" : [
            {"match": {"object": "Widget"}}
        ]
    }},
    "aggs" : {
        "objects" : {
            "terms" : {
              "field" : "action"
            }
        }
    }
}}

然后结果是45次点击聚合

"aggregations": {
        "objects": {
            "buckets": [
                {
                    "key": "widgets",
                    "doc_count": 41
                },
                {
                    "key": "embedded",
                    "doc_count": 40
                },
                {
                    "key": "view",
                    "doc_count": 32
                },
                {
                    "key": "activation",
                    "doc_count": 9
                },
                {
                    "key": "image",
                    "doc_count": 4
                },
                {
                    "key": "create",
                    "doc_count": 3
                },
                {
                    "key": "mapping",
                    "doc_count": 3
                },
                {
                    "key": "widget",
                    "doc_count": 3
                },
                {
                    "key": "adding",
                    "doc_count": 2
                },
                {
                    "key": "edit",
                    "doc_count": 1
                }
            ]
        }
    }

从这些聚合中可以看出,我有一些键不在我的第一个匹配所有文档的动作聚合中。这是为什么?我需要做些什么才能获得包含所有文档操作的存储桶。

我不认为我需要做分页或其他事情,因为我还试图做

{"index":"datalayer","type":"analysis2","body":{"from":0,"size":500,"query":{
        "match_all" : {}
    },
    "aggs" : {
        "objects" : {
            "terms" : {
              "field" : "action"
            }
        }
    }
}}

的完全相同的聚合结果
"aggregations": {
    "objects": {
        "buckets": [
            {
                "key": "thing",
                "doc_count": 278
            },
            {
                "key": "hover",
                "doc_count": 273
            },
            {
                "key": "embedded",
                "doc_count": 57
            },
            {
                "key": "view",
                "doc_count": 50
            },
            {
                "key": "widgets",
                "doc_count": 49
            },
            {
                "key": "hovered",
                "doc_count": 20
            },
            {
                "key": "widgetembed",
                "doc_count": 20
            },
            {
                "key": "products",
                "doc_count": 19
            },
            {
                "key": "create",
                "doc_count": 15
            },
            {
                "key": "image",
                "doc_count": 13
            }
        ]
    }
}

所以,我希望有人可以向我解释为什么我没有看到我期待的桶中的钥匙?

1 个答案:

答案 0 :(得分:1)

来自the documentation

  

默认情况下,聚合术语将返回doc_count排序的前十个术语的存储桶。可以通过设置size参数来更改此默认行为。

因此,您需要指定一个大于10的数字的"size"来查看更多存储桶。或设置为0以查看所有存储桶。来自相同的文档:

  

如果设置为0,则大小将设置为Integer.MAX_VALUE。

   "aggs" : {
        "objects" : {
            "terms" : {
              "field" : "action",
              "size": 0
            }
        }
    }