gpt4 book ai didi

Elasticsearch - 返回聚合以匹配特定值?

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

使用 Elasticsearch 2,是否可以返回文档类别与特定字段值匹配的聚合?例如,我想获取 categories.type = "application"的所有类别。

我的映射看起来像这样:

"mappings": {
"products": {
"_all": {
"enabled": true
},
"properties": {
"title": {
"type": "string"
},
"categories": {
"type":"nested",
"properties": {
"type": {
"type": "string",
"index": "not_analyzed"
},
"name": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}

我的查询看起来像这样,它返回所有类别类型,但我只想过滤 categories.type = "application"的类别。

{  
"query":{
"multi_match": {
"query": "Sound",
"fields": [
"title"
]
}
},
"aggs":{
"Applications": {
"nested": {
"path": "categories"
},
"aggs": {
"meta": {
"terms": {
"field": "categories.type"
},
"aggs": {
"name": {
"terms": {
"field": "categories.name"
}
}
}
}
}
}
}
}

最佳答案

您可以使用 aggregation filter如果我理解正确的话:

{   
size : 50,
"query":{
"multi_match": {
"query": "Sound",
"fields": [
"title"
]
}
},
"aggs":{
"Applications": {
"nested": {
"path": "categories"
},
"aggs": {
"meta": {
"filter" : {
"term" : {
"categories.type" : "application"
}
},
"aggs": {
"name": {
"terms": {
"field": "categories.name"
}
}
}
}
}
}
}
}

希望对您有所帮助。

关于Elasticsearch - 返回聚合以匹配特定值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37079445/

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