Elasticsearch大列表聚合



我正试图计算成分在不同文档中出现的次数。我的索引体类似于

index_body = {
"settings":{
"index":{
"number_of_replicas":0,
"number_of_shards":4,
"refresh_interval":"-1",
"knn":"true"
}
},
"mappings":{
"properties":{
"recipe_id":{
"type":"keyword"
},
"recipe_title":{
"type":"text",
"analyzer":"standard",
"similarity":"BM25"
},
"description":{
"type":"text",
"analyzer":"standard",
"similarity":"BM25"
},
"ingredient":{
"type":"keyword"
},
"image":{
"type":"keyword"
},
....
}
}

在ingredient字段中,我存储了每个成分[ingredient1,ingredient2,....]

的字符串数组我有大约900个文档。每一种都有自己的成分表。

我试过使用Elasticsearch的聚合,但它似乎没有返回我所期望的。以下是我一直在使用的查询:

{
"size":0,
"aggs":{
"ingredients":{
"terms": {"field":"ingredient"} 
}
}
}

但是它返回这个:

{'took': 4, 'timed_out': False, '_shards': {'total': 4, 'successful': 4, 'skipped': 0, 'failed': 0}, 'hits': {'total': {'value': 994, 'relation': 'eq'}, 'max_score': None, 'hits': []}, 'aggregations': {'ingredients': {'doc_count_error_upper_bound': 56, 'sum_other_doc_count': 4709, 'buckets': [{'key': 'salt', 'doc_count': 631}, {'key': 'oil', 'doc_count': 320}, {'key': 'sugar', 'doc_count': 314}, {'key': 'egg', 'doc_count': 302}, {'key': 'butter', 'doc_count': 291}, {'key': 'flour', 'doc_count': 264}, {'key': 'garlic', 'doc_count': 220}, {'key': 'ground pepper', 'doc_count': 185}, {'key': 'vanilla extract', 'doc_count': 146}, {'key': 'lemon', 'doc_count': 131}]}}}

这显然是错误的,因为我有很多成分。我做错了什么?为什么它只返回这些?是否有一种方法可以强制Elasticsearch返回所有计数?

您需要在聚合中指定大小。

{"size" 0,"aggs" {"ingredients" {terms"; {"field";;ingredient";;size";}}}

最新更新