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

时间:2016-04-28 13:41:38

标签: elasticsearch java-api

我正在研究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 个答案:

答案 0 :(得分:1)

对于问题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();