【问题标题】:elasticsearch aggregation - why is a match all query not returning keys that a more specific query is?elasticsearch 聚合 - 为什么匹配所有查询不返回更具体查询的键?
【发布时间】:2015-05-11 18:00:16
【问题描述】:

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

例如,首先我做这个查询:

{"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
            }
        ]
    }
}

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

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    来自the documentation

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2015-03-09
      • 2020-07-21
      • 2016-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多