我有一个“ PRODUCT_SUPPLIER”索引,其中包含与供应商的产品文档,其字段名为“ product_name”和“ product_supplier”。
"mappings" : {
"properties" : {
"product_name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword"
}
}
},
"product_supplier" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword"
}
}
}
}
}
一种产品可以有多个供应商。我有一个供应商名称,说“ supplier1”。我得到了这个供应商提供的产品桶。现在,我要从这些存储桶中进行查询,以获取存储桶中除特定供应商“ supplier1”之外其他产品的其他供应商名称。我的查询是这个
GET /products_supplier/_search
{
"query": {
"term": {
"product_supplier": {
"value": "supplier1"
}
}
}, "aggs": {
"products_list": {
"terms": {
"field": "product_name.keyword",
"size": 10
}, "aggs": {
"others_supplier": {
"terms": {
"field": "product_supplier.keyword",
"size": 10
}
}
}
}
}
}
我得到以下结果。我不知道如何消除“ others_supplier”聚集中的“ Supplier1”。
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 0.6931472,
"hits" : [
{
"_index" : "products_supplier",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.6931472,
"_source" : {
"product_name" : "maggie",
"product_supplier" : "supplier1"
}
},
{
"_index" : "products_supplier",
"_type" : "_doc",
"_id" : "3",
"_score" : 0.6931472,
"_source" : {
"product_name" : "potatoes",
"product_supplier" : "supplier1"
}
},
{
"_index" : "products_supplier",
"_type" : "_doc",
"_id" : "4",
"_score" : 0.6931472,
"_source" : {
"product_name" : "chips",
"product_supplier" : "supplier1"
}
}
]
},
"aggregations" : {
"products_list" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "chips",
"doc_count" : 1,
"others_supplier" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "supplier1",
"doc_count" : 1
}
]
}
},
{
"key" : "maggie",
"doc_count" : 1,
"others_supplier" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "supplier1",
"doc_count" : 1
}
]
}
},
{
"key" : "potatoes",
"doc_count" : 1,
"others_supplier" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "supplier1",
"doc_count" : 1
}
]
}
}
]
}
}
}
答案 0 :(得分:1)
您可以在聚合中使用排除设置。例如
GET /products_supplier/_search
{
"query": {
"term": {
"product_supplier": {
"value": "supplier1"
}
}
}, "aggs": {
"products_list": {
"terms": {
"field": "product_name.keyword",
"size": 10
}, "aggs": {
"others_supplier": {
"terms": {
"field": "product_supplier.keyword",
"exclude": "supplier1",
"size": 10
}
}
}
}
}
}