2016-04-28 113 views
0

我正在致力於ElasticSearch 2.3.1。我有要求獲取最近90天的數據。在這個我需要數據以及明智的聚合。我面臨的問題是,我能夠獲取最近90天的數據,但不能在聚合上應用日期範圍Elasticsearch 2.3.1用於篩選和聚合的Java API,日期範圍

所以,我有兩個問題

問題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 for full: 
match query { 
    aggregation { 
    filter aggregation, 
    date histogram 
    } 
} 
的Java API的

回答

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();