“reverse cardinality” in elasticsearch?

别说谁变了你拦得住时间么 提交于 2019-12-11 00:58:53

问题


Suppose I have a mapping that stores trips people made, and a field in that mapping that is the user id. For instance:

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

I'm trying to make a query that will return amount of trips (entries with this mapping) bucketed by amount of people (user_id).

For example, it would return something like this:

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

I have no idea how to face this kind of query. I've been messing with terms buckets and subaggregations for a while to no avail.

Any help appreciated.

UPDATE

Here's an example:

Assume I have the following records in my index:

[
  {
    "_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",
    }
]

My query would return something like:

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

Since there is one user with two trips (id 11019) and two users with one trip (ids 11020 and 11021).

来源:https://stackoverflow.com/questions/35360676/reverse-cardinality-in-elasticsearch

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