【发布时间】:2020-07-26 02:50:51
【问题描述】:
我正在尝试进行聚合查询以计算嵌套在弹性搜索中的元素数量。这是映射的一部分:
{
"news" : {
"aliases" : { },
"mappings" : {
"dynamic" : "false",
"properties" : {
"categories" : {
"type" : "nested",
"properties" : {
"categoryId" : {
"type" : "text"
},
"name" : {
"type" : "text"
},
"slug" : {
"type" : "text"
}
}
}
}
}
}
}
这是我做的查询:
{
"size": 0,
"aggs":{
"categories_count":{
"terms": {
"field":"categories.slug.keyword",
"size": 100
}
}
}
}
这是我得到的结果:
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 22,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"categories_count": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": []
}
}
}
这是我想要实现的目标: 说有两个文件:
doc 1 类别:
"categories" : [
{
"categoryId" : "aaaaffff",
"name" : "Category 1",
"slug" : "category-1",
},
{
"categoryId" : "aaaafffd",
"name" : "Category 2",
"slug" : "category-2",
},
{
"categoryId" : "aaaafffc",
"name" : "Category 3",
"slug" : "category-3",
}
],
文档 2 类别:
"categories" : [
{
"categoryId" : "aaaafffd",
"name" : "Category 2",
"slug" : "category-2",
},
{
"categoryId" : "aaaafffc",
"name" : "Category 3",
"slug" : "category-3",
},
{
"categoryId" : "aaaafffb",
"name" : "Category 4",
"slug" : "category-4",
},
{
"categoryId" : "aaaafffe",
"name" : "Category 5",
"slug" : "category-5",
},
],
输出应该是这样的:
{
{
"categoryId": "aaaaffff",
"name": "Category 1",
"slug": "category-1",
"count": 1
},
{
"categoryId": "aaaafffd",
"name": "Category 2",
"slug": "category-2",
"count": 2
},
{
"categoryId": "aaaafffc",
"name": "Category 3",
"slug": "category-3",
"count": 2
},
{
"categoryId": "aaaafffb",
"name": "Category 4",
"slug": "category-4",
"count": 1
},
{
"categoryId": "aaaafffe",
"name": "Category 5",
"slug": "category-5",
"count": 1
}
}
是否有任何查询可以实现这一目标?先感谢您。也可以不更改映射吗?因为我无法更改映射(它来自管理)。
【问题讨论】:
标签: elasticsearch elasticsearch-aggregation