我能够使用弹性搜索6.8中的聚合查询获取数据库中文本字段的所有值:
{
aggs: {
values: {
terms: { field: 'City.keyword', size: 200 },
},
},
size: 0,
};我试图对嵌套字段执行同样的操作。
下面是一个文本字段(City)和嵌套字段(冷却)的示例。
"City": "Fredericksburg",
"Cooling": [
{
"key": "Fuel",
"value": "Electric"
},
{
"key": "Central Air",
"value": "Y"
},
{
"key": "Central A/C",
"value": "true"
}
],以下是相关的映射:
{
"listings:366": {
"mappings": {
"_default_": {
"_all": {
"enabled": false
},
"dynamic_templates": [
...
{
"Cooling": {
"path_match": "Cooling.*",
"mapping": {
"type": "text"
}
}
},
...
],
"properties": {
...
"Cooling": {
"type": "nested"
},
...
}
},
"listings": {
"_all": {
"enabled": false
},
"dynamic_templates": [
...
{
"Cooling": {
"path_match": "Cooling.*",
"mapping": {
"type": "text"
}
}
},
...
],
"properties": {
...
"Cooling": {
"type": "nested",
"properties": {
"key": {
"type": "text"
},
"value": {
"type": "text"
}
}
},
}
}
}
}
}发布于 2021-07-23 15:35:12
如果Cooling是nested类型的,那么您可以通过首先在聚合中嵌套terms聚合来实现您想要的结果,以便“深入”冷却嵌套文档:
{
"size": 0,
"aggs": {
"cooling": {
"nested": {
"path": " Cooling"
},
"aggs": {
"values": {
"terms": {
"field": "Cooling.key", <--- make sure this field is mapped as keyword
"size": 200
}
}
}
}
}
}更新
您的映射需要更改为:
"properties": {
...
"Cooling": {
"type": "nested",
"properties": {
"key": {
"type": "keyword" <--- change this
},
"value": {
"type": "text"
}
}
},
}发布于 2021-07-23 13:35:25
尝试使用复合聚合:参见复合聚集
https://stackoverflow.com/questions/68499831
复制相似问题