2017-11-18 8 views
1

매월 직원 수를 나타내는 날짜 막대 그래프를 만들려고합니다.탄성 검색 날짜 히스토그램 (특정 시점의 문서 포함)

{ 
    "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", 
    .... 
} 

을 감안할 때이 같은 시작의 끝 시나리오 (세 명) : 나는 히스토그램과 유사한 것으로 기대

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

:

직원 매핑은 다음과 같이 보입니다

"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을 사용하여이 작업을 수행 할 수 있다고 생각합니다. 그러나 나는 간단한 접근을 제안하고있다. 독특한 필드에 의해, Cardinality Aggregation을 사용하여 계산하는 : distinct_agg이

  • 11월
  • 만 고용을 필터링 할 수 Filter Aggregation를 사용하여 :

    { 
        "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 : 당신은 하나의 특정 달 동안 쿼리 때마다 실행해야합니다 number, 총 종업원

    employmentDates에 다음 하나 개의 레코드, 예컨대 :

    당신은 Nested Datatype와 중첩 이동해야합니다
    "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" 
        } 
    

    , 예 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" 
            } 
           } 
           } 
          } 
          } 
         } 
         } 
        } 
        } 
    }