如何在elasticsearch中触发单个查询以获取以下按月截止日期的用户数

vshtjzan  于 2021-06-10  发布在  ElasticSearch
关注(0)|答案(1)|浏览(283)

如何触发单个查询以获取以下按月到日期的用户数索引:user(公司中所有用户的列表)

users who joined this month (month till date - i.e. in Nov)
users who joined previous month (say Oct)
...
users who joined on second month (say Feb)
users who joined on first month (say Jan)

有没有一个快速的方法来获取所有信息使用一个单一的查询,我想看到一个响应,其中包含所有信息检索到一个单一的查询?

a64a0gku

a64a0gku1#

如果我理解你的问题,我建议你使用 date histogram 与范围查询一起聚合。 gte 代表 greater than or equal tolte 代表 less than or equal to .
我首先指定我在这个例子中需要的范围 Nov 2020Jan 2020 . 基于这个结果,我将以 one month .
我假设在索引中为每个用户创建一个文档。
我在索引中索引了以下数据:

"hits" : [
  {
    "_index" : "month-count",
    "_type" : "_doc",
    "_id" : "UPv_l3UBrH4n7Et0xLpD",
    "_score" : 1.0,
    "_source" : {
      "timestamp" : "2020-01-01"
    }
  },
  {
    "_index" : "month-count",
    "_type" : "_doc",
    "_id" : "qPv_l3UBrH4n7Et0-7r9",
    "_score" : 1.0,
    "_source" : {
      "timestamp" : "2020-01-01"
    }
  },
  {
    "_index" : "month-count",
    "_type" : "_doc",
    "_id" : "r_sAmHUBrH4n7Et0e7s-",
    "_score" : 1.0,
    "_source" : {
      "timestamp" : "2020-09-01"
    }
  },
  {
    "_index" : "month-count",
    "_type" : "_doc",
    "_id" : "s_sAmHUBrH4n7Et0gLsh",
    "_score" : 1.0,
    "_source" : {
      "timestamp" : "2020-09-01"
    }
  },
  {
    "_index" : "month-count",
    "_type" : "_doc",
    "_id" : "TfsAmHUBrH4n7Et0nrwS",
    "_score" : 1.0,
    "_source" : {
      "timestamp" : "2020-02-01"
    }
  },
  {
    "_index" : "month-count",
    "_type" : "_doc",
    "_id" : "-vsAmHUBrH4n7Et07Ly-",
    "_score" : 1.0,
    "_source" : {
      "timestamp" : "2020-03-01"
    }
  },
  {
    "_index" : "month-count",
    "_type" : "_doc",
    "_id" : "_vsAmHUBrH4n7Et09LyD",
    "_score" : 1.0,
    "_source" : {
      "timestamp" : "2020-03-01"
    }
  },
  {
    "_index" : "month-count",
    "_type" : "_doc",
    "_id" : "APsAmHUBrH4n7Et0972w",
    "_score" : 1.0,
    "_source" : {
      "timestamp" : "2020-03-01"
    }
  }
]

我使用的查询:

GET month-count/_search
{
"query": {
    "range": {
    "timestamp": {
        "gte": "now-11M/M",
        "lte": "now/M"
    }
    }
}, 
"aggs": {
    "get_Month": {
    "date_histogram": {
        "field": "timestamp",
        "interval": "month"
    }
    }
}
}

答案是:

"hits" : [
    {
        "_index" : "month-count",
        "_type" : "_doc",
        "_id" : "UPv_l3UBrH4n7Et0xLpD",
        "_score" : 1.0,
        "_source" : {
        "timestamp" : "2020-01-01"
        }
    },
    {
        "_index" : "month-count",
        "_type" : "_doc",
        "_id" : "qPv_l3UBrH4n7Et0-7r9",
        "_score" : 1.0,
        "_source" : {
        "timestamp" : "2020-01-01"
        }
    },
    {
        "_index" : "month-count",
        "_type" : "_doc",
        "_id" : "r_sAmHUBrH4n7Et0e7s-",
        "_score" : 1.0,
        "_source" : {
        "timestamp" : "2020-09-01"
        }
    },
    {
        "_index" : "month-count",
        "_type" : "_doc",
        "_id" : "s_sAmHUBrH4n7Et0gLsh",
        "_score" : 1.0,
        "_source" : {
        "timestamp" : "2020-09-01"
        }
    },
    {
        "_index" : "month-count",
        "_type" : "_doc",
        "_id" : "TfsAmHUBrH4n7Et0nrwS",
        "_score" : 1.0,
        "_source" : {
        "timestamp" : "2020-02-01"
        }
    },
    {
        "_index" : "month-count",
        "_type" : "_doc",
        "_id" : "-vsAmHUBrH4n7Et07Ly-",
        "_score" : 1.0,
        "_source" : {
        "timestamp" : "2020-03-01"
        }
    },
    {
        "_index" : "month-count",
        "_type" : "_doc",
        "_id" : "_vsAmHUBrH4n7Et09LyD",
        "_score" : 1.0,
        "_source" : {
        "timestamp" : "2020-03-01"
        }
    },
    {
        "_index" : "month-count",
        "_type" : "_doc",
        "_id" : "APsAmHUBrH4n7Et0972w",
        "_score" : 1.0,
        "_source" : {
        "timestamp" : "2020-03-01"
        }
    }
    ]
},
"aggregations" : {
    "get_Month" : {
    "buckets" : [
        {
        "key_as_string" : "2020-01-01T00:00:00.000Z",
        "key" : 1577836800000,
        "doc_count" : 2
        },
        {
        "key_as_string" : "2020-02-01T00:00:00.000Z",
        "key" : 1580515200000,
        "doc_count" : 1
        },
        {
        "key_as_string" : "2020-03-01T00:00:00.000Z",
        "key" : 1583020800000,
        "doc_count" : 3
        },
        {
        "key_as_string" : "2020-04-01T00:00:00.000Z",
        "key" : 1585699200000,
        "doc_count" : 0
        },
        {
        "key_as_string" : "2020-05-01T00:00:00.000Z",
        "key" : 1588291200000,
        "doc_count" : 0
        },
        {
        "key_as_string" : "2020-06-01T00:00:00.000Z",
        "key" : 1590969600000,
        "doc_count" : 0
        },
        {
        "key_as_string" : "2020-07-01T00:00:00.000Z",
        "key" : 1593561600000,
        "doc_count" : 0
        },
        {
        "key_as_string" : "2020-08-01T00:00:00.000Z",
        "key" : 1596240000000,
        "doc_count" : 0
        },
        {
        "key_as_string" : "2020-09-01T00:00:00.000Z",
        "key" : 1598918400000,
        "doc_count" : 2
        }
    ]
    }
}

这个 doc_count 我想这就是你需要的。
如果你需要帮助,请告诉我,我很乐意帮助你。
链接:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.htmlhttps://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html

相关问题