gpt4 book ai didi

elasticsearch - 如何在 Elasticsearch 中使用 Java API 将响应按月份划分并添加计数器

转载 作者:行者123 更新时间:2023-12-01 17:56:32 25 4
gpt4 key购买 nike

我在elasticsearch中有几千个不同时间和三个serviceId的文档。是否可以用计数器将所有这些文档按月划分?文档模型:

{
"dateTime" : "2011-03-13T11:34:14.869Z",
"organizationId" : "1a4b7625-dcec-4326-b7dc-96e038b31d0b",
"accountId" : "a9bfcced-ddaa-477c-8021-18364ac320ee",
"processInstanceId" : "711e73cb-1286-461f-810f-d5791f71101f",
"serviceConfigurationId" : "e8be10e8-2ba2-4365-bfb0-96052d90be7e",
"clusterId" : "542096b3-3982-4d4b-bce1-44b1f988cf7f",
"serviceId" : "asdf"
}

我正在执行一个具有范围(例如一年)的 SearchRequest,并期望它执行如下操作:

{
"_index": "test",
"_type": "_doc",
"_id": "Jc0H03AB-y_MhSAimo7v",
"_score": null,
"_month": {
"Jan": [
{
"serviceId": "asdf",
"counter": 4
},
{
"serviceId": "zxcv",
"counter": 9
}
],
"Feb":[
{
"serviceId": "asdf",
"counter": 12
},
{
"serviceId": "zxcv",
"counter": 11
}
], etc
}
}

我找到了如何使用 Java API 创建范围查询。

RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("dateTime").from(LocalDateTime.parse("2011-11-09T10:30")).to(LocalDateTime.parse("2022-11-09T10:30")).timeZone("Z");
sourceBuilder.query(rangeQueryBuilder);
searchRequest.source(sourceBuilder);
client().search(searchRequest, RequestOptions.DEFAULT);

也许这是不可能的,我在浪费时间寻找解决方案?

最佳答案

范围查询将返回日期时间在给定范围内的文档。您不能在查询部分中按月份对它们进行分组。这可以使用 date_histogram aggregation 来完成和 top_hits aggregationb

{
"size": 0,
"aggs": {
"filter_year": { --> filter documents which fall in given interval
"filter": {
"range": {
"dateTime": {
"gte": "2011-01-01",
"lte": "2011-12-31"
}
}
},
"aggs": {
"month": {
"date_histogram": { --> group documents on monthly interval
"field": "dateTime",
"format": "MMM",
"interval": "month"
},
"aggs": {
"documents": {
"top_hits": { --> return documents under months
"_source": [
"clusterId"
],
"size": 10
}
}
}
}
}
}
}
}

关于elasticsearch - 如何在 Elasticsearch 中使用 Java API 将响应按月份划分并添加计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60706394/

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