2016-09-05 11 views
0

特定の期間にdate_histogramを取得したいのですが、期間を制限する方法はありますか? extended_boundsパラメータを使用する必要がありますか?たとえば、「2016-08-01」と「2016-08-31」の間のdate_histogramを照会したいとします。間隔はdayです。私はこの表現で問い合わせる:弾性検索date_histogram extended_bounds

{ 
    "aggs": { 
    "cf_loan": { 
     "date_histogram": { 
     "field": "createDate", 
     "interval": "day", 
     "format": "yyyy-MM-dd", 
     "min_doc_count": 0, 
     "extended_bounds": { 
      "min": "2016-08-01", 
      "max": "2016-08-31" 
     } 
     } 
    } 
    } 
} 

しかし、私は範囲内にないdate_histogramを取得します。

答えて

2

ほとんどの場合、createDateフィールドが希望の範囲にあるドキュメントのみを選択するには、rangeクエリを追加する必要があります。

{ 
    "query": { 
    "range": {       <---- add this range query 
     "createDate": { 
     "gte": "2016-08-01T00:00:00.000Z", 
     "lt": "2016-09-01T00:00:00.000Z" 
     } 
    } 
    }, 
    "aggs": { 
    "cf_loan": { 
     "date_histogram": { 
     "field": "createDate", 
     "interval": "day", 
     "format": "yyyy-MM-dd", 
     "min_doc_count": 0, 
     "extended_bounds": { 
      "min": "2016-08-01", 
      "max": "2016-08-31" 
     } 
     } 
    } 
    } 
} 

extended_boundsパラメータの役割は、それらに文書がない場合でも、あなたはminからmaxまでの毎日バケツを買ってあげることを確認することです。たとえば、2016-08-04と2016-08-28の間に毎日1つのドキュメントがあり、extended_boundsパラメータがないと、25個のバケット(2016-08-04,2016-08-05,2016- 08-06、...、2016-08-28)。

extended_boundsパラメータを使用すると、あなたはまた、次のバケットを取得するが、0文書とされます:

  • 2016年8月1日
  • 2016年8月2日
  • 2016年8月3日
  • 2016年8月29日
  • 2016年8月30日
  • 2016年8月31日