Elastic request exists for attached documents

I have attached documents:

"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 it should not

 "someField": "hello", "users": [] 

but, unfortunately, both of them do not match. any ideas?

+5
source share
3 answers

The example mentioned on the Elasticsearch blog is for strings and an array of string types, not nested types.

The following query should work for you:

 { "query": { "nested": { "path": "users", "query": { "bool": { "must": [ { "exists": { "field": "users" } } ] } } } } } 

Alternatively, you can address this issue for more information that discusses this usage pattern.

+7
source

With the following index mapping:

 { "index_name": { "mappings": { "object_name": { "dynamic": "strict", "properties": { "nested_field_name": { "type": "nested", "properties": { "some_property": { "type": "keyword" } } } } } } } } 

I needed to use this query:

 GET /index_name/_search { "query": { "nested": { "path": "nested_field_name", "query": { "bool": { "must": [ { "exists": { "field": "nested_field_name.some_property" } } ] } } } } } 

Elastic Version 5.4.3

+1
source

It works for me

 GET /type/_search?pretty=true { "query": { "bool": { "must": [ { "nested": { "path": "outcome", "query": { "exists": { "field": "outcome.outcomeName" } } } } ] } } } 
+1
source

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


All Articles