gpt4 book ai didi

Elasticsearch 2.3.1 用于日期范围过滤和聚合的 Java API

转载 作者:行者123 更新时间:2023-12-01 09:59:36 27 4
gpt4 key购买 nike

我正在开发 ElasticSearch 2.3.1。我需要获取过去 90 天的数据。在这方面我需要数据以及精细的聚合。我面临的问题是我能够获取过去 90 天的数据但无法在聚合上应用日期范围。

所以我有两个问题

问题 1 – 这应该适用于匹配查询字段不同的结果集。

问题 2 – 用于上述查询的 Java API

我使用的查询如下:

GET _search
{
"aggs": {
"t1": {
"filter": {
"range": {
"timestamp": {
"from" : "now-1d/d",
"to" : null,
"format" : "epoch_millis",
"include_lower" : true,
"include_upper" : true
}
}
},
"aggs": {
"t2": {
"date_histogram": {
"field": "timestamp",
"interval": "1m"
}
}
}
}
}
}

简而言之,我需要以下类型的 Java API

Java API for full:
match query {
aggregation {
filter aggregation,
date histogram
}
}

最佳答案

对于问题 1,以下查询有效:

GET _search {
"query": {
"match": {
"body": "Text_To_Search"
}
},
"aggs": {
"outer_agg": {
"filter": {
"bool": {
"must": [{
"range": {
"timestamp": {
"from": "now-90d/d",
"to": null,
"format": "epoch_millis",
"include_lower": true,
"include_upper": true
}
}
}]
}
},
"aggs": {
"inner_agg": {
"date_histogram": {
"field": "timestamp",
"interval": "1m"
}
}
}
}
}
}

其 Java API 是:

DateHistogramBuilder dhb = AggregationBuilders.dateHistogram("t2").field("timestamp").interval(DateHistogramInterval.MINUTE);

BoolQueryBuilder bqb = QueryBuilders.boolQuery().must(QueryBuilders.rangeQuery("timestamp").gte("now-90d").to("now").format("epoch_millis"));

FilterAggregationBuilder fab = AggregationBuilders.filter("t1").filter(bqb).subAggregation(dhb);

SearchResponse sr = TEFESConnector.getInstance().getClient().prepareSearch("index_name").setTypes("type_name").setQuery(QueryBuilders.matchQuery("field_to_search", "text_to_search")).addAggregation(fab).execute().actionGet();

关于Elasticsearch 2.3.1 用于日期范围过滤和聚合的 Java API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36916514/

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