2017-01-09 4 views
4

ネストされたオブジェクトを介したフィルタリングを使用してフィルタリングされたElasticsearchクエリを構築し、ネストされたオブジェクトリスト内のネストされたオブジェクトの最小値を取得するネストされたプロパティのフィルタと集計を持つElasticsearch DSL pythonクエリ

フィルタリング部分は機能しますが、aggs(集約)部分とバインドできません。 .aggs.bucket部分をフィルタの後にコードに追加すると、無視されます(search.to_dict()には表示されません)。または構文エラーが表示されます。

誰かが一緒にバインドする方法の例を教えていただけますか?私が取得しようとしている両方のフィルタクエリ結果は、1つの応答

例スキーマにnested1.foo.barから算出された最小値を終わらせる:

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 
     } 
    ) 
) 

は基本的に私は必要なものを個々のMyExampleドキュメントのネストされたnested1.foo.bar値の最小値を取得することです(固有のmyexample_idフィールドを持っています)。

答えて

3

追加する次の行に

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

次の行には、トリックを行う必要があります。

+0

'' 'fested.bar'''の各グループの最小値を' '' nested1'''で個別に取得することはできますか?言い換え:単一の入れ子1のすべての '' 'foo''からすべての' 'bar''を集めて、最小値を取得しますか? – user2091046

+0

私は '' MyExample'''を少し編集しました。 私がする必要があるのは、一意の '' 'MyExample'''(それぞれ固有の' 'myexample_id'''を持っています)のネストされた' '' nested1.foo.bar'''の値の最小値を得ることですフィールド) – user2091046

+1

私の答えはこちらを参照してください:http://stackoverflow.com/questions/41594609/python-elasticsearch-dsl-aggregation-metric-of-nested-values-per-document/41608959#41608959 –

関連する問題