question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Searching documents with nested fields, using "exists" filter?

See original GitHub issue

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)

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
njoannincommented, Feb 29, 2016

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!

1reaction
honzakralcommented, Mar 5, 2019

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

elastic exists query for nested documents
May 12, 2021 at 10:15. This query returns empty results for me.. Although in Discover mode I see documents with non-empty nested field....
Read more >
Execute a filter on nested document only if it exists
I am using ES 2.3 and have a query in which filter section looks as follows: "filter": { "query": { "bool": { "must":...
Read more >
Query on Embedded/Nested Documents
This page provides examples of query operations on embedded/nested documents using the db.collection.find() method in mongosh . The examples on this page ...
Read more >
Nested - OpenSearch documentation
A nested field type is a special type of object field type. Any object field can take an array of objects. Each of...
Read more >
Querying arrays with complex types and nested structures
To filter an array that includes a nested structure by one of its child elements, issue a query with an UNNEST operator. For...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found