- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何触发单个查询以按月到日期获取以下用户数
索引:用户(公司中所有用户的列表)
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 2020
到Jan 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
是您所需要的。
关于elasticsearch - 如何在Elasticsearch中触发单个查询以按月到日期获取以下用户数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64694504/
This question already has answers here: Using Variable for Thread group Ramp up time (3个答案) 3年前关闭。 从
我希望使用 RPyC 为硬件板提供 API 作为服务。该板一次只能满足一个用户的需求。有什么方法可以让 RPyC 强制执行一次只有一个用户可以访问吗? 最佳答案 我不确定这是否有效(或有效),但您可以
如果我想以每秒 10 个请求运行测试。如何让 Jmeter 选择每秒处理该请求数所需的最佳线程数。 我将线程数设置为与每秒请求数相同。 最佳答案 您可以使用恒定吞吐量计时器 click here你只需
我正在尝试进行查询以检查客户表并返回过去 30 天、过去 365 天和所有时间具有特定值的用户数。 所有时间的计数很简单: $stmt = $conn->prepare("SELECT count(i
我是一名优秀的程序员,十分优秀!