"反向基数"在elasticsearch?

时间:2016-02-12 10:49:49

标签: elasticsearch elasticsearch-aggregation

假设我有一个映射存储人员的行程,并且该映射中的字段是用户ID。例如:

"trip": {
  "destination": {
    "type": "string"
  },
  "departure_date": {
    "type": "date"
  },
  "return_date": {
    "type": "date"
  },
  "user_id": {
    "type": "integer"
  }
}

我试图通过人数(user_id)来确定将返回旅程数量(具有此映射的条目)的查询。

例如,它将返回如下内容:

amount of trips  |   people
---------------------------
                1|      723
                2|      422
                3|      210
                4|       82
                5|       11
                6|        0
                7|        1
                ...

我不知道如何面对这种查询。我一直在搞乱术语桶和子集合一段时间无济于事。

任何帮助表示感谢。

更新

以下是一个例子:

假设我的索引中有以下记录:

[
  {
    "_index": "my_index",
    "_type": "trip",
    "_id": "9034",
    "_score": 1,
    "_source": {
      "user_id": 11019,
      "destination": "Paris",
      "departure_date": "2015-01-09",
      "return_date": "2015-02-10",
    },
  {
    "_index": "my_index",
    "_type": "trip",
    "_id": "9035",
    "_score": 1,
    "_source": {
      "user_id": 11019,
      "destination": "Madrid",
      "departure_date": "2013-11-22",
      "return_date": "2013-11-29",
    },
  {
    "_index": "my_index",
    "_type": "trip",
    "_id": "9036",
    "_score": 1,
    "_source": {
      "user_id": 11020,
      "destination": "Boston",
      "departure_date": "2013-09-01",
      "return_date": "2013-10-15",
    },
  {
    "_index": "my_index",
    "_type": "trip",
    "_id": "9037",
    "_score": 1,
    "_source": {
      "user_id": 11021,
      "destination": "London",
      "departure_date": "2014-02-07",
      "return_date": "2014-03-10",
    }
]

我的查询会返回如下内容:

amount of trips  |   people
---------------------------
                1|        2
                2|        1

因为有一个用户有两次旅行(id 11019),两个用户有一次旅行(id 11020和11021)。

0 个答案:

没有答案