2017-11-18 3 views
1

月ごとの従業員数を示す日付ヒストグラムを作成しようとしています。Elasticsearch Dateヒストグラムの文書の時刻をカウントする

{ 
    "number": 1234, 
    "firstName": "Chris", 
    "lastName": "Smith", 
    "employmentDates: [ 
     { 
      "startDate": "2014-10-03T06:00:00Z", 
      "endDate": "2017-11-04T06:00:00Z" 
     } 
    ], 
    "lastPaidOnDate": "2017-11-10T06:00:00Z", 
    .... 
} 

を考えると、このような開始終了シナリオ(のための3つの従業員)::私はヒストグラムが、これに類似していると期待される

|----------------| 
     |-----------------------------| 
    |---| |---------------------| 
^ ^^^^^

従業員のマッピングは次のようになります

"aggregations": { 
    "employees_per_month": { 
     "buckets": [ 
      { 
       "key_as_string": "2017-01-01", 
       "doc_count": 1 
      }, 
      { 
       "key_as_string": "2017-02-01", 
       "doc_count": 2 
      }, 
      { 
       "key_as_string": "2017-03-01", 
       "doc_count": 2 
      }, 
      { 
       "key_as_string": "2017-04-01", 
       "doc_count": 3 
      }, 
      { 
       "key_as_string": "2017-05-01", 
       "doc_count": 3 
      }, 
      { 
       "key_as_string": "2017-06-01", 
       "doc_count": 2 
      } 
     ] 
    } 
} 

スクリプトフィールドにサブ集計が必要なようですが、どこから始めるべきかわからない。

ご協力いただき誠にありがとうございます。

答えて

0

私はそれがDateHistogramを使って行うことができると信じています。しかし、私は単純なアプローチを提案しています。あなたは、ある特定の月のたびにクエリを実行する必要があります:

{ 
    "size": 0, 
    "aggregations": { 
    "bool_agg": { 
     "filter": { 
     "bool": { 
      "must": [ 
      { 
       "range": { 
       "employmentDates.startDate": { 
        "lt": "2017-12-01T00:00:00Z" 
       } 
       } 
      }, 
      { 
       "range": { 
       "employmentDates.endDate": { 
        "gte": "2017-11-01T00:00:00Z" 
       } 
       } 
      } 
      ] 
     } 
     }, 
     "aggregations": { 
     "distinct_agg": { 
      "cardinality": { 
      "field": "number" 
      } 
     } 
     } 
    } 
    } 
} 
  • bool_agg:のみ雇用をフィルタリングするFilter Aggregationを使用して11月
  • distinct_agg:カウントするCardinality Aggregationを使用して、一意のフィールドによってnumber、総従業員

employmentDatesにその後、一つのレコード、例えば:

"employmentDates: [ 
    { 
     "startDate": "2014-10-03T06:00:00Z", 
     "endDate": "2017-11-04T06:00:00Z" 
    } 
    { 
     "startDate": "2018-03-03T06:00:00Z", 
     "endDate": "2018-07-04T06:00:00Z" 
    } 

あなたが行くNested Datatypeでネストされている必要があります、例はhereを見つけることができます。 クエリを更新する:

{ 
    "size": 0, 
    "aggregations": { 
    "nested_agg": { 
     "nested": { 
     "path": "employmentDates" 
     }, 
     "aggregations": { 
     "bool_agg": { 
      "filter": { 
      "bool": { 
       "must": [ 
       { 
        "range": { 
        "employmentDates.startDate": { 
         "lt": "2017-12-01T00:00:00Z" 
        } 
        } 
       }, 
       { 
        "range": { 
        "employmentDates.endDate": { 
         "gte": "2017-11-01T00:00:00Z" 
        } 
        } 
       } 
       ] 
      } 
      }, 
      "aggregations": { 
      "comment_to_issue": { 
       "reverse_nested": {}, 
       "aggregations": { 
       "distinct_agg": { 
        "cardinality": { 
        "field": "number" 
        } 
       } 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
} 
関連する問題