弹性搜索根据条件聚合到存储桶中 [英] Elastic Search Aggregate into buckets on conditions

查看:93
本文介绍了弹性搜索根据条件聚合到存储桶中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Elastic Search开始,一直坚持尝试进行一些汇总。基本上,我有一个数据集,该数据集包含以下格式的数据:

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.

这是我尝试过的事情:

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

我知道这似乎是一个简单的问题,但我已经坚持了很长时间。任何帮助将不胜感激。

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

推荐答案

通过执行以下操作,可以使其按预期的方式工作:

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

这篇关于弹性搜索根据条件聚合到存储桶中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆