elasticsearch - Elasticsearch 根据条件聚合到存储桶中

标签 elasticsearch elasticsearch-aggregation

我从 flex 搜索开始,并且一直试图进行一些聚合。基本上,我有一个数据集,包含以下形式的数据:

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

现在,我想进行一些汇总,并在一个查询中获得所有的Continental餐馆,Good餐馆,New York餐馆。

请注意,我不希望统计所有类型的餐厅,而只希望统计特定类型的餐厅。而且,这些聚合是相互独立的。就是说,当我说“好”时,我并不一定希望它是“大陆”,它可以是意大利语或其他任何东西。

这是我尝试过的:
{
    "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"
            }
          }
        }
      }
    }
  }
}

关于elasticsearch - Elasticsearch 根据条件聚合到存储桶中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36819975/

相关文章:

Elasticsearch:使用 OR 过滤多个字段

java - 更新插入文档时索引未更新

elasticsearch - 我需要在Elasticsearch中按日期获取平均文档数

elasticsearch - ElasticSearch批量api中的其他操作参数在哪里?

elasticsearch - 通过Elastic Search中的匹配查询返回的分数进行范围查询

elasticsearch - 嵌套类型字段内的字段聚合

elasticsearch - 计算Elasticsearch聚合的结果

node.js - Elasticsearch 客户端 http 流量错误,正在关闭连接

python - 美化Python嵌套字典代码

sorting - boost 弹性聚集结果