弹性搜索根据条件聚合到桶中
Elastic Search Aggregate into buckets on conditions
我刚开始使用 Elastic Search,一直在尝试进行一些聚合。基本上,我有一个由以下形式的数据组成的数据集:
{
"name": "The Chef Restaurant",
"city": "New York",
"state": "New York",
"rating": "GOOD",
"type": "Continental"
}
现在,我想进行一些聚合,并在一次查询中获取纽约的所有大陆餐厅、优质餐厅和餐厅。
请注意,我不需要所有类型餐厅的数量,我只需要特定类型的餐厅数量。此外,这些聚合是相互独立的。也就是说,当我说 GOOD 时,我不一定希望它是 Continental,它可以是意大利语或其他任何东西。
这是我试过的:
{
"size": 0,
"query": {
"match_all": {}
},
"aggregations": {
"good_restaurants": {
"filters": {
"match": {
"rating": "CONTINENTAL"
}
}
},
"continental_restaurants": {
"filters": {
"match": {
"type": "CONTINENTAL"
}
}
},
"restaurants_in_new_york": {
"filters": {
"match": {
"type": "CONTINENTAL"
}
}
}
}
}
这给了我错误:
{
"error": {
"root_cause": [
{
"type": "search_parse_exception",
"reason": "Unknown key for a START_OBJECT in [good_restaurants]: [match].",
"line": 9,
"col": 17
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "test_master",
"node": "-aWy78_mRaaBMcOAeiN9tg",
"reason": {
"type": "search_parse_exception",
"reason": "Unknown key for a START_OBJECT in [good_restaurants]: [match].",
"line": 9,
"col": 17
}
}
]
},
"status": 400
}
我知道这似乎是一个简单的问题,但我已经坚持了很长时间。任何帮助将不胜感激。
您可以按照您期望的方式工作,方法是:
{
"size": 0,
"query": {
"match_all": {}
},
"aggregations": {
"selected_types": {
"filters": {
"filters": {
"good_restaurants": {
"match": {
"rating": "CONTINENTAL"
}
},
"continental_restaurants": {
"match": {
"type": "CONTINENTAL"
}
},
"restaurants_in_new_york": {
"match": {
"type": "CONTINENTAL"
}
}
}
}
}
}
}
我刚开始使用 Elastic Search,一直在尝试进行一些聚合。基本上,我有一个由以下形式的数据组成的数据集:
{
"name": "The Chef Restaurant",
"city": "New York",
"state": "New York",
"rating": "GOOD",
"type": "Continental"
}
现在,我想进行一些聚合,并在一次查询中获取纽约的所有大陆餐厅、优质餐厅和餐厅。
请注意,我不需要所有类型餐厅的数量,我只需要特定类型的餐厅数量。此外,这些聚合是相互独立的。也就是说,当我说 GOOD 时,我不一定希望它是 Continental,它可以是意大利语或其他任何东西。
这是我试过的:
{
"size": 0,
"query": {
"match_all": {}
},
"aggregations": {
"good_restaurants": {
"filters": {
"match": {
"rating": "CONTINENTAL"
}
}
},
"continental_restaurants": {
"filters": {
"match": {
"type": "CONTINENTAL"
}
}
},
"restaurants_in_new_york": {
"filters": {
"match": {
"type": "CONTINENTAL"
}
}
}
}
}
这给了我错误:
{
"error": {
"root_cause": [
{
"type": "search_parse_exception",
"reason": "Unknown key for a START_OBJECT in [good_restaurants]: [match].",
"line": 9,
"col": 17
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "test_master",
"node": "-aWy78_mRaaBMcOAeiN9tg",
"reason": {
"type": "search_parse_exception",
"reason": "Unknown key for a START_OBJECT in [good_restaurants]: [match].",
"line": 9,
"col": 17
}
}
]
},
"status": 400
}
我知道这似乎是一个简单的问题,但我已经坚持了很长时间。任何帮助将不胜感激。
您可以按照您期望的方式工作,方法是:
{
"size": 0,
"query": {
"match_all": {}
},
"aggregations": {
"selected_types": {
"filters": {
"filters": {
"good_restaurants": {
"match": {
"rating": "CONTINENTAL"
}
},
"continental_restaurants": {
"match": {
"type": "CONTINENTAL"
}
},
"restaurants_in_new_york": {
"match": {
"type": "CONTINENTAL"
}
}
}
}
}
}
}