2017-01-11 1 views
1

2 레벨 중첩 (문서 당 최소 개별 값)에서 최소 (최소) 값을 찾으려고합니다.Python elasticsearch 문서 당 중첩 값의 DSL 집계/메트릭

지금까지는 검색 결과에 중첩 된 모든 값의 최소값을 계산하지만 문서별로 구분하지 않는 집계를 만들 수 있습니다.

내 예를 들어, 스키마

class MyExample(DocType): 
    myexample_id = Integer() 
    nested1 = Nested(
     properties={ 
      'timestamp': Date(), 
      'foo': Nested(
       properties={ 
        'bar': Float(), 
       } 
      ) 
     } 
    ) 
    nested2 = Nested(
     multi=False, 
     properties={ 
      'x': String(), 
      'y': String(), 
     } 
    ) 

그리고 이것이 내가 검색 및 집계하고있어 방법은 다음과 같습니다

from elasticsearch_dsl import Search, Q 

search = Search().filter(
    'nested', path='nested1', inner_hits={}, 
    query=Q(
     'range', **{ 
      'nested1.timestamp': { 
       'gte': exampleDate1, 
       'lte': exampleDate2 
      } 
     } 
    ) 
).filter(
    'nested', path='nested2', inner_hits={'name': 'x'}, 
    query=Q(
     'term', **{ 
      'nested2.x': x 
     } 
    ) 
).filter(
    'nested', path='nested2', inner_hits={'name': 'y'}, 
    query=Q(
     'term', **{ 
      'nested2.y': y 
     } 
    ) 
) 

search.aggs.bucket(
    'nested1', 'nested', path='nested1' 
).bucket(
    'nested_foo', 'nested', path='nested1.foo' 
).metric(
    'min_bar', 'min', field='nested1.foo.bar' 
) 

기본적으로 내가해야 할 일입니다 모든 중첩 된 nested1에 대한 최소 값을 얻기 위해 고유 한 myexample_id 필드가있는 각 고유 MyExample의 .foo.bar 값

답변

2

문서 당 최소값을 원할 경우 nested 버킷을 bu myexample_id 필드 위에 terms 집계를 cket이 집계는 각 문서에 대한 버킷을 생성해야하기 때문에 계산하기가 매우 비쌀 수 있다는

search.aggs..bucket(
    'docs', 'terms', field='myexample_id' 
).bucket(
    'nested1', 'nested', path='nested1' 
).bucket(
    'nested_foo', 'nested', path='nested1.foo' 
).metric(
    'min_bar', 'min', field='nested1.foo.bar' 
) 

참고. 이와 같은 유즈 케이스의 경우 문서 단위로 최소값을 script_field 또는 앱에서 계산하는 것이 더 쉽습니다.

+1

이 최소값은 인덱싱 할 때 알아 내고 문서의 루트 수준에 저장해야한다고 제안하기까지했습니다. '중첩 된 '문서 나 스크립트를 사용하여 여러 수준의 agg보다 훨씬 더 많은 것을 수행 할 수 있습니다. – Val

관련 문제