Elasticsearch-dsl-py: Searching documents with nested fields, using "exists" filter?

Created on 29 Feb 2016  路  7Comments  路  Source: elastic/elasticsearch-dsl-py

I have a document similar to this:

class MyDoc(DocType):
   field1 = String()
   field2 = Nested(properties={"field3": String(), "field4: String()})

I would like to search for all documents that actually have field3...

As a control, I have tried:

>>> s = MyDoc.search()
>>> s.filter("exists", field="field1").count()
>>> 26870

But, with the nested field, it doesn't seem to work even though I know that there are such documents:

>>> s.filter("exists", field="field2").count()
>>> 0
>>> s.filter("exists", field="field2.field3").count()
>>> 0
>>> s.filter("exists", field="field2__field3").count()
>>> 0

So, my question is: how to search for documents that have a specified nested field?
(Working with elasticsearch_dsl v0.0.5.dev0)

Most helpful comment

I think I found it:

s = MyDoc.search()
s = s.filter('nested', path='field2', filter=F('exists', field="field2.field3"))

And now it works.

Thanks again for your help!

All 7 comments

Regardless of what version you are using you need to use the nested filter:

s = MyDoc.search()
s = s.filter('nested', filter=F('exists', field="field2.field3"))

In version 2.x you'd just use query=Q('exists', field='field2.field3') instead of the filter argument.

Hope this helps

Ah, OK! Thanks

However, when I try that, I get the following error when I call count() or execute() on it:

raise HTTP_EXCEPTIONS.get(status_code, TransportError)(status_code, error_message, additional_info)
elasticsearch.exceptions.RequestError: 
    TransportError(400, '{"count":0,"_shards":{"total":5,"successful":0,"failed":5,"failures":[
    {"index":"MyIndex","shard":0,"reason":"BroadcastShardOperationFailedException[[MyIndex][0] ]; nested: QueryParsingException[[MyIndex] [nested] requires \'path\' field]; "},
    {"index":"MyIndex","shard":1,"reason":"BroadcastShardOperationFailedException[[MyIndex][1] ]; nested: QueryParsingException[[MyIndex] [nested] requires \'path\' field]; "},
    {"index":"MyIndex","shard":2,"reason":"BroadcastShardOperationFailedException[[MyIndex][2] ]; nested: QueryParsingException[[MyIndex] [nested] requires \'path\' field]; "},
    {"index":"MyIndex","shard":3,"reason":"BroadcastShardOperationFailedException[[MyIndex][3] ]; nested: QueryParsingException[[MyIndex] [nested] requires \'path\' field]; "},
    {"index":"MyIndex","shard":4,"reason":"BroadcastShardOperationFailedException[[MyIndex][4] ]; nested: QueryParsingException[[MyIndex] [nested] requires \'path\' field]; "}]}}')

Am I missing something?

I think I found it:

s = MyDoc.search()
s = s.filter('nested', path='field2', filter=F('exists', field="field2.field3"))

And now it works.

Thanks again for your help!

Ah, yes, forgot about path, sorry!

Since F is depreciated, what to use now?

@osama-v8 Q() serves the same purpose now

s = s.filter('nested', path='field2', filter=Q('exists', field="field2.field3"))
doesn't work, marking Q as an unresolved refrence.
did the syntax change?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

quasiben picture quasiben  路  4Comments

mortada picture mortada  路  3Comments

beanaroo picture beanaroo  路  4Comments

takaomag picture takaomag  路  3Comments

MauriJHN picture MauriJHN  路  4Comments