gpt4 book ai didi

elasticsearch - Elasticsearch中的嵌套聚合

转载 作者:行者123 更新时间:2023-12-02 23:00:37 25 4
gpt4 key购买 nike

我正在尝试使用elasticsearch实现自动完成功能。我正在研究的索引的结构是这个

"title": "blog post 1",
"body": "body of the blog post",
"category": "programming",
"locations": [
{"name": "united states"},
{"name": "new york"},
{"name": "venice"}
]

我正在尝试使用嵌套聚合。我正在使用的映射是这个
{                            
blog : {
mappings : {
tag : {
properties : {
body : {
type : string
},
category : {
type : string
},
locations : {
properties : {
name : {
type : string
}
}
},
title : {
type : string
}
}
}
}
}
}

应该基于 locations.name汇总结果的查询是
GET /blog/tag/_search
{
"size": 0,
"query": {
"match": {
"locations.name": "montreal"
}
},
"aggs": {
"aggregated_locations": {
"nested": {
"path": "locations"
},
"aggs": {
"filtered_locations": {
"terms": {
"fields": "locations.name"
}
}
}
}
}
}

目前,我正在使用用于Elasticsearch的chrome插件执行上述请求。上面的查询给出了一个错误,它很长,但是如果有人提示,我也可以发布它。我想我在解释嵌套聚合的含义时可能是错误的,这意味着我的映射是错误的。但是我无法弄清楚问题出在哪里。

最佳答案

映射中缺少一些部分,您需要将location字段设置为nested类型,并且不分析内部字段以便能够根据您的期望进行汇总:

PUT blog
{
"mappings": {
"tag": {
"properties": {
"body": {
"type": "string"
},
"category": {
"type": "string"
},
"locations": {
"type": "nested",
"properties": {
"name": {
"type": "string",
"index": "not_analyzed"
}
}
},
"title": {
"type": "string"
}
}
}
}
}

然后运行以下查询以对嵌套字段进行过滤,并在嵌套字段上进行聚合:
GET blog/_search
{
"size": 0,
"query": {
"nested": {
"path": "locations",
"query": {
"match": {
"locations.name": "montreal"
}
}
}
},
"aggs": {
"aggregated_locations": {
"nested": {
"path": "locations"
},
"aggs": {
"filtered": {
"terms": {
"field": "locations.name"
}
}
}
}
}
}

可以合理地复制/粘贴那些查询( https://www.elastic.co/guide/en/sense/current/installing.html)

关于elasticsearch - Elasticsearch中的嵌套聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35162877/

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