I have a nested documents as:
"someField": "hello", "users": [ { "name": "John", "surname": "Doe", "age": 2 } ]
according to this https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-exists-query.html, the above should match:
GET /_search { "query": { "exists" : { "field" : "users" } }
}
whereas the following should not,
"someField": "hello", "users": []
but unfortunately both do not match. any ideas?
You can perform a nested query in Elasticsearch by using the nested parameter. A nested query will search the nested field objects and return the document's root parent if there's a matching object.
The nested type is a specialised version of the object data type that allows arrays of objects to be indexed in a way that they can be queried independently of each other.
When a packed class contains an instance field that is a packed type, the data for that field is packed directly into the containing class. The field is known as a nested field . When reading from a nested field, a small object is created as a pointer to the data.
Nested data types are structured data types for some common data patterns. Nested data types support structs, arrays, and maps. A struct is similar to a relational table. It groups object properties together.
The example mentioned on the Elasticsearch blog refers to string and array of string types, not for nested types.
The following query should work for you:
{ "query": { "nested": { "path": "users", "query": { "bool": { "must": [ { "exists": { "field": "users" } } ] } } } } }
Also, you can refer to this issue for more info, which discusses this usage pattern.
This works for me
GET /type/_search?pretty=true { "query": { "bool": { "must": [ { "nested": { "path": "outcome", "query": { "exists": { "field": "outcome.outcomeName" } } } } ] } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With