无法使弹性搜索聚合正常工作
Having trouble getting Elastic Search Aggregates to work
问题:
我只想从每个变体组中退回 1 件商品。
上下文:
我正在使用 Elasticsearch 7。
如果我搜索甜椒,应该只会列出一项。从技术上讲,我出售 4 种传统甜椒和 4 种有机甜椒。在 Bell Pepper 详细信息页面上,它允许您 select 颜色,因此没有理由用大量的灯笼椒淹没搜索结果页面。我很确定聚合是解决方案,但我找到的所有示例以及如何执行此操作的建议对我来说似乎都失败了。
有没有人看到我的错误,或者有一个优雅的方法来实现这个建议?
文档结构:
{
'id': 2843,
'name': 'Yellow Bell pepper',
'variant_id': 3311
}
使用的查询:
{
'query': {
'query_string': {
'query': 'bell pepper',
'fields': [ 'name' ]
}
},
'aggs': {
'variants' : {
'terms' : { 'field' : 'variant_id' }
}
}
};
当前回复:
"total": 23,
"statusCode": 200,
"hits": [
{
"id": 2843,
"name": "Yellow Bell pepper",
"variant_id": 3311
},
{
"id": 2842,
"name": "Orange Bell Pepper",
"variant_id": 3311
},
{
"id": 2839,
"name": "Organic Green Bell Pepper",
"variant_id": 3312
},
{
"id": 2840,
"name": "Organic Yellow Bell Pepper",
"variant_id": 3312
},
]
}
我需要的回复:
"total": 23,
"statusCode": 200,
"hits": [
{
"id": 2843,
"name": "Yellow Bell pepper",
"variant_id": 3311
},
{
"id": 2839,
"name": "Organic Green Bell Pepper",
"variant_id": 3312
}
]
}
GET Whosebug/_search
{
"query": {
"query_string": {
"query": "bell pepper",
"fields": [
"name"
]
}
},
"aggs": {
"variants": {
"terms": {
"field": "variant_id"
},
"aggs": {
"results": {
"top_hits": {
"size": 1
}
}
}
}
}
}
结果
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : 0.22380026,
"hits" : [
{
"_index" : "Whosebug",
"_type" : "_doc",
"_id" : "Wt7GrnEBo-Xqbvtw2rIB",
"_score" : 0.22380026,
"_source" : {
"id" : 2843,
"name" : "Yellow Bell pepper",
"variant_id" : 3311
}
},
{
"_index" : "Whosebug",
"_type" : "_doc",
"_id" : "W97GrnEBo-Xqbvtw9rKy",
"_score" : 0.22380026,
"_source" : {
"id" : 2842,
"name" : "Orange Bell Pepper",
"variant_id" : 3311
}
},
{
"_index" : "Whosebug",
"_type" : "_doc",
"_id" : "XN7HrnEBo-XqbvtwDbJ2",
"_score" : 0.19908613,
"_source" : {
"id" : 2839,
"name" : "Organic Green Bell Pepper",
"variant_id" : 3312
}
},
{
"_index" : "Whosebug",
"_type" : "_doc",
"_id" : "Xd7HrnEBo-XqbvtwMrLw",
"_score" : 0.19908613,
"_source" : {
"id" : 2840,
"name" : "Organic Yellow Bell Pepper",
"variant_id" : 3312
}
}
]
},
"aggregations" : {
"variants" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 3311,
"doc_count" : 2,
"results" : {
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.22380026,
"hits" : [
{
"_index" : "Whosebug",
"_type" : "_doc",
"_id" : "Wt7GrnEBo-Xqbvtw2rIB",
"_score" : 0.22380026,
"_source" : {
"id" : 2843,
"name" : "Yellow Bell pepper",
"variant_id" : 3311
}
}
]
}
}
},
{
"key" : 3312,
"doc_count" : 2,
"results" : {
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.19908613,
"hits" : [
{
"_index" : "Whosebug",
"_type" : "_doc",
"_id" : "XN7HrnEBo-XqbvtwDbJ2",
"_score" : 0.19908613,
"_source" : {
"id" : 2839,
"name" : "Organic Green Bell Pepper",
"variant_id" : 3312
}
}
]
}
}
}
]
}
}
}
这使用 filter_path 以忽略噪音并将大小设置为 0 以便不 return 任何命中,因为您不会使用它们。
GET Whosebug/_search?filter_path=aggregations.variants.buckets.results.hits.hits._source
{
"size": 0,
"query": {
"query_string": {
"query": "bell pepper",
"fields": [
"name"
]
}
},
"aggs": {
"variants": {
"terms": {
"field": "variant_id"
},
"aggs": {
"results": {
"top_hits": {
"size": 1
}
}
}
}
}
}
结果
{
"aggregations" : {
"variants" : {
"buckets" : [
{
"results" : {
"hits" : {
"hits" : [
{
"_source" : {
"id" : 2843,
"name" : "Yellow Bell pepper",
"variant_id" : 3311
}
}
]
}
}
},
{
"results" : {
"hits" : {
"hits" : [
{
"_source" : {
"id" : 2839,
"name" : "Organic Green Bell Pepper",
"variant_id" : 3312
}
}
]
}
}
}
]
}
}
}
问题:
我只想从每个变体组中退回 1 件商品。
上下文: 我正在使用 Elasticsearch 7。 如果我搜索甜椒,应该只会列出一项。从技术上讲,我出售 4 种传统甜椒和 4 种有机甜椒。在 Bell Pepper 详细信息页面上,它允许您 select 颜色,因此没有理由用大量的灯笼椒淹没搜索结果页面。我很确定聚合是解决方案,但我找到的所有示例以及如何执行此操作的建议对我来说似乎都失败了。
有没有人看到我的错误,或者有一个优雅的方法来实现这个建议?
文档结构:
{
'id': 2843,
'name': 'Yellow Bell pepper',
'variant_id': 3311
}
使用的查询:
{
'query': {
'query_string': {
'query': 'bell pepper',
'fields': [ 'name' ]
}
},
'aggs': {
'variants' : {
'terms' : { 'field' : 'variant_id' }
}
}
};
当前回复:
"total": 23,
"statusCode": 200,
"hits": [
{
"id": 2843,
"name": "Yellow Bell pepper",
"variant_id": 3311
},
{
"id": 2842,
"name": "Orange Bell Pepper",
"variant_id": 3311
},
{
"id": 2839,
"name": "Organic Green Bell Pepper",
"variant_id": 3312
},
{
"id": 2840,
"name": "Organic Yellow Bell Pepper",
"variant_id": 3312
},
]
}
我需要的回复:
"total": 23,
"statusCode": 200,
"hits": [
{
"id": 2843,
"name": "Yellow Bell pepper",
"variant_id": 3311
},
{
"id": 2839,
"name": "Organic Green Bell Pepper",
"variant_id": 3312
}
]
}
GET Whosebug/_search
{
"query": {
"query_string": {
"query": "bell pepper",
"fields": [
"name"
]
}
},
"aggs": {
"variants": {
"terms": {
"field": "variant_id"
},
"aggs": {
"results": {
"top_hits": {
"size": 1
}
}
}
}
}
}
结果
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : 0.22380026,
"hits" : [
{
"_index" : "Whosebug",
"_type" : "_doc",
"_id" : "Wt7GrnEBo-Xqbvtw2rIB",
"_score" : 0.22380026,
"_source" : {
"id" : 2843,
"name" : "Yellow Bell pepper",
"variant_id" : 3311
}
},
{
"_index" : "Whosebug",
"_type" : "_doc",
"_id" : "W97GrnEBo-Xqbvtw9rKy",
"_score" : 0.22380026,
"_source" : {
"id" : 2842,
"name" : "Orange Bell Pepper",
"variant_id" : 3311
}
},
{
"_index" : "Whosebug",
"_type" : "_doc",
"_id" : "XN7HrnEBo-XqbvtwDbJ2",
"_score" : 0.19908613,
"_source" : {
"id" : 2839,
"name" : "Organic Green Bell Pepper",
"variant_id" : 3312
}
},
{
"_index" : "Whosebug",
"_type" : "_doc",
"_id" : "Xd7HrnEBo-XqbvtwMrLw",
"_score" : 0.19908613,
"_source" : {
"id" : 2840,
"name" : "Organic Yellow Bell Pepper",
"variant_id" : 3312
}
}
]
},
"aggregations" : {
"variants" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 3311,
"doc_count" : 2,
"results" : {
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.22380026,
"hits" : [
{
"_index" : "Whosebug",
"_type" : "_doc",
"_id" : "Wt7GrnEBo-Xqbvtw2rIB",
"_score" : 0.22380026,
"_source" : {
"id" : 2843,
"name" : "Yellow Bell pepper",
"variant_id" : 3311
}
}
]
}
}
},
{
"key" : 3312,
"doc_count" : 2,
"results" : {
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.19908613,
"hits" : [
{
"_index" : "Whosebug",
"_type" : "_doc",
"_id" : "XN7HrnEBo-XqbvtwDbJ2",
"_score" : 0.19908613,
"_source" : {
"id" : 2839,
"name" : "Organic Green Bell Pepper",
"variant_id" : 3312
}
}
]
}
}
}
]
}
}
}
这使用 filter_path 以忽略噪音并将大小设置为 0 以便不 return 任何命中,因为您不会使用它们。
GET Whosebug/_search?filter_path=aggregations.variants.buckets.results.hits.hits._source
{
"size": 0,
"query": {
"query_string": {
"query": "bell pepper",
"fields": [
"name"
]
}
},
"aggs": {
"variants": {
"terms": {
"field": "variant_id"
},
"aggs": {
"results": {
"top_hits": {
"size": 1
}
}
}
}
}
}
结果
{
"aggregations" : {
"variants" : {
"buckets" : [
{
"results" : {
"hits" : {
"hits" : [
{
"_source" : {
"id" : 2843,
"name" : "Yellow Bell pepper",
"variant_id" : 3311
}
}
]
}
}
},
{
"results" : {
"hits" : {
"hits" : [
{
"_source" : {
"id" : 2839,
"name" : "Organic Green Bell Pepper",
"variant_id" : 3312
}
}
]
}
}
}
]
}
}
}