Elastic Search Aggregate into buckets on conditions

匿名 (未验证) 提交于 2019-12-03 09:58:14

问题:

I am starting out with Elastic Search, and am stuck at trying to do some aggregation. Basically, I have a data set consisting of data in the following form:

{     "name": "The Chef Restaurant",     "city": "New York",     "state": "New York",     "rating": "GOOD",     "type": "Continental" } 

Now, I want to do some aggregation and get all the Continental restaurants, Good restaurants, Restaurants in New York in one query.

Note that I don't want the count of all types of restaurants, I just want the count of the specific types. Also, these aggregations are mutually independent. That is, when I say GOOD, I don't necessarily want it to be Continental, it can be Italian or anything else.

This is what I have tried:

{     "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"                 }             }         }     } } 

which gives me the error:

{    "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 } 

I know this seems like a simple question, but I have been stuck at it for a long time. Any help will be appreciated.

回答1:

You can make it work the way you expect, by doing it like this:

{   "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"             }           }         }       }     }   } } 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!