Elisticsearch python DSL queries with filters and aggregations by nested properties

I would like to create a filtered Elasticsearch query using filtering through nested objects, and also do aggregation to get the minimum value of the nested object inside the list of nested objects.

The filtering part works, but I cannot associate it with the aggs (aggregations) part. When I add a part .aggs.bucketto my code after filters, it is either ignored (not displayed in search.to_dict()) or gives me syntax errors.

Can someone give me an example on how to tie them together? I try to get as a result of a filtered query, ending the calculated minimum value due nested1.foo.barto one answer

Example circuit:

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

Build Request:

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

nested1.foo.bar MyExample ( myexample_id)

+4
1

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

.

+3

Source: https://habr.com/ru/post/1666317/


All Articles