gpt4 book ai didi

arrays - Elasticsearch -使用嵌套的过滤数组作为存储桶

转载 作者:行者123 更新时间:2023-12-02 22:24:48 25 4
gpt4 key购买 nike

我有点迷路了...

考虑一下这个简单的索引文档:

{
"url" : "http://...?mypage"
"pages": [
{
"elapsed": 1190,
"type": "LOADPAGE"
},
{
"elapsed": 115400,
"type": "ONPAGE"
},
{
"elapsed": 1100,
"type": "LOADPAGE"
},
{
"elapsed": 1340,
"type": "ONPAGE"
}
]
}

我正在尝试计算平均LOADPAGE,所以我知道我将需要“avg”或“stats”聚合。
"aggs": {
"compute_loadpage": {
"filter": { "term": { "pages.type": "loadpage" } },
"aggs": {
"loadpage_all": {
"stats": {
"field": "pages.elapsed"
}
}
}
}
}

我知道“过滤器”聚合将创建一个存储桶,其中包含与我的过滤器相对应的所有文档,因此可以理解,我的聚合将在完整的“页面”数组上完成。

如何创建仅包含LOADPAGE值的存储桶,然后就可以对其进行汇总,或者必须使用脚本化的汇总?

最佳答案

只要您的文档映射使用nested aggregation,就可以使用nested type做到这一点。

为了进行测试,我设置了一个像这样的简单索引(注意嵌套类型,以及"index": "not_analyzed"上的"pages.type"):

PUT /test_index
{
"settings": {
"number_of_shards": 1
},
"mappings": {
"doc": {
"properties": {
"pages": {
"type": "nested",
"properties": {
"elapsed": {
"type": "long"
},
"type": {
"type": "string",
"index": "not_analyzed"
}
}
},
"url": {
"type": "string"
}
}
}
}
}

然后,我为您的文档建立了索引:
PUT /test_index/doc/1
{
"url": "http://...?mypage",
"pages": [
{
"elapsed": 1190,
"type": "LOADPAGE"
},
{
"elapsed": 115400,
"type": "ONPAGE"
},
{
"elapsed": 1100,
"type": "LOADPAGE"
},
{
"elapsed": 1340,
"type": "ONPAGE"
}
]
}

然后,这种聚合似乎可以提供您想要的东西:
POST /test_index/_search?search_type=count
{
"aggs": {
"pages_nested": {
"nested": {
"path": "pages"
},
"aggs": {
"loadpage_filtered": {
"filter": {
"term": {
"pages.type": "LOADPAGE"
}
},
"aggs": {
"loadpage_avg_elap": {
"avg": {
"field": "pages.elapsed"
}
}
}
}
}
}
}
}
...
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0,
"hits": []
},
"aggregations": {
"pages_nested": {
"doc_count": 4,
"loadpage_filtered": {
"doc_count": 2,
"loadpage_avg_elap": {
"value": 1145,
"value_as_string": "1145.0"
}
}
}
}
}

这是我用来测试的代码:

http://sense.qbox.io/gist/b526427f14225b02e7268ed15d8c6dde4793fc8d

关于arrays - Elasticsearch -使用嵌套的过滤数组作为存储桶,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31009674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com