gpt4 book ai didi

elasticsearch - 如何在Elasticsearch中触发单个查询以按月到日期获取以下用户数

转载 作者:行者123 更新时间:2023-12-03 02:10:58 25 4
gpt4 key购买 nike

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

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)
有没有一种快速的方法来使用单个查询来获取所有信息,我希望看到一个包含从单个查询中检索到的所有信息的响应?

最佳答案

如果我对问题的理解很好,建议您将date histogram聚合与范围查询一起使用。gte代表greater than or equal to,而lte代表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.html
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html

关于elasticsearch - 如何在Elasticsearch中触发单个查询以按月到日期获取以下用户数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64694504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com