Elasticsearch 嵌套聚合 return 重复值
Elasticsearch nested aggregations return duplicate value
这是数据:
{
"search_product_sku" : "FD0044S",
"price" : 500.00,
"car_detail" : [
{
"car_brand" : "TOYOTA",
"specification" : "TOYOTA Avanza 1.3L F601 2004"
},
{
"car_brand" : "SUZUKI",
"specification" : "SUZUKI APV 1.6L GC416X8A 2005"
}
],
}
我想在car_detail.car_brand
中嵌套car_detail.specification
这是嵌套聚合查询:
GET /my_products/_search
{
"size": 50,
"query": {
"bool": {
"must":[
{
"query_string": {
"query": "*FD0044S*",
"fields": [ "search_product_sku"]
}
}
]
}
},
"aggs": {
"total_car_brand" : {
"terms":
{
"field": "car_detail.car_brand.keyword"
},
"aggs": {
"total_car_spec": {
"terms": {
"field": "car_detail.specification.keyword"
}
}
}
}
}
}
但是你看到的结果 all car_detail.specification
都放在每个 car_detail.car_brand
里面
"aggregations" : {
"total_car_brand" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "SUZUKI",
"doc_count" : 1,
"total_car_spec" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "SUZUKI APV 1.6L GC416X8A 2005",
"doc_count" : 1
},
{
"key" : "TOYOTA Avanza 1.3L F601 2004",
"doc_count" : 1
}
]
}
},
{
"key" : "TOYOTA",
"doc_count" : 1,
"total_car_spec" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "SUZUKI APV 1.6L GC416X8A 2005",
"doc_count" : 1
},
{
"key" : "TOYOTA Avanza 1.3L F601 2004",
"doc_count" : 1
}
]
}
}
]
}
}
这是我想要的结果。
"aggregations" : {
"total_car_brand" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "SUZUKI",
"doc_count" : 1,
"total_car_spec" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "SUZUKI APV 1.6L GC416X8A 2005",
"doc_count" : 1
}
]
}
},
{
"key" : "TOYOTA",
"doc_count" : 1,
"total_car_spec" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "TOYOTA Avanza 1.3L F601 2004",
"doc_count" : 1
}
]
}
}
]
}
}
我不知道你的映射是怎样的,但我这样测试过它并且有效。
映射
PUT idx_nested
{
"mappings": {
"properties": {
"search_product_sku": {
"type": "text"
},
"price": {
"type": "integer"
},
"car_detail": {
"type": "nested",
"properties": {
"car_brand": {
"type": "keyword"
},
"specification": {
"type": "keyword"
}
}
}
}
}
}
查询
GET /idx_nested/_search
{
"size": 0,
"aggs": {
"car_detail": {
"nested": {
"path": "car_detail"
},
"aggs": {
"total_car_brand": {
"terms": {
"field": "car_detail.car_brand",
"size": 10
},
"aggs": {
"total_car_spec": {
"terms": {
"field": "car_detail.specification"
}
}
}
}
}
}
}
}
这是数据:
{
"search_product_sku" : "FD0044S",
"price" : 500.00,
"car_detail" : [
{
"car_brand" : "TOYOTA",
"specification" : "TOYOTA Avanza 1.3L F601 2004"
},
{
"car_brand" : "SUZUKI",
"specification" : "SUZUKI APV 1.6L GC416X8A 2005"
}
],
}
我想在car_detail.car_brand
car_detail.specification
这是嵌套聚合查询:
GET /my_products/_search
{
"size": 50,
"query": {
"bool": {
"must":[
{
"query_string": {
"query": "*FD0044S*",
"fields": [ "search_product_sku"]
}
}
]
}
},
"aggs": {
"total_car_brand" : {
"terms":
{
"field": "car_detail.car_brand.keyword"
},
"aggs": {
"total_car_spec": {
"terms": {
"field": "car_detail.specification.keyword"
}
}
}
}
}
}
但是你看到的结果 all car_detail.specification
都放在每个 car_detail.car_brand
"aggregations" : {
"total_car_brand" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "SUZUKI",
"doc_count" : 1,
"total_car_spec" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "SUZUKI APV 1.6L GC416X8A 2005",
"doc_count" : 1
},
{
"key" : "TOYOTA Avanza 1.3L F601 2004",
"doc_count" : 1
}
]
}
},
{
"key" : "TOYOTA",
"doc_count" : 1,
"total_car_spec" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "SUZUKI APV 1.6L GC416X8A 2005",
"doc_count" : 1
},
{
"key" : "TOYOTA Avanza 1.3L F601 2004",
"doc_count" : 1
}
]
}
}
]
}
}
这是我想要的结果。
"aggregations" : {
"total_car_brand" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "SUZUKI",
"doc_count" : 1,
"total_car_spec" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "SUZUKI APV 1.6L GC416X8A 2005",
"doc_count" : 1
}
]
}
},
{
"key" : "TOYOTA",
"doc_count" : 1,
"total_car_spec" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "TOYOTA Avanza 1.3L F601 2004",
"doc_count" : 1
}
]
}
}
]
}
}
我不知道你的映射是怎样的,但我这样测试过它并且有效。
映射
PUT idx_nested
{
"mappings": {
"properties": {
"search_product_sku": {
"type": "text"
},
"price": {
"type": "integer"
},
"car_detail": {
"type": "nested",
"properties": {
"car_brand": {
"type": "keyword"
},
"specification": {
"type": "keyword"
}
}
}
}
}
}
查询
GET /idx_nested/_search
{
"size": 0,
"aggs": {
"car_detail": {
"nested": {
"path": "car_detail"
},
"aggs": {
"total_car_brand": {
"terms": {
"field": "car_detail.car_brand",
"size": 10
},
"aggs": {
"total_car_spec": {
"terms": {
"field": "car_detail.specification"
}
}
}
}
}
}
}
}