這是資料:
{
"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.specification里面筑巢car_detail.car_brand
這是嵌套聚合查詢:
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"
}
}
}
}
}
}
但是你可以看到所有 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
}
]
}
}
]
}
}
uj5u.com熱心網友回復:
我不知道您的映射如何,但我像這樣測驗過它并且它有效。
映射
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"
}
}
}
}
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/481763.html
標籤:弹性搜索
上一篇:使用術語查詢搜索多欄位
