Elasticsearch: Query nested object contained within an object -
i'm struggling build query can nested search across sub-object of document.
say have following index/mapping:
curl -xpost "http://localhost:9200/author/" -d ' { "mappings": { "item": { "properties": { "books": { "type": "object", "properties": { "data": { "type": "nested" } } } } } } } '
and following 2 documents in index:
{ "id": 1, "name": "robert louis stevenson", "books": { "count": 2, "data": [ { "id": 1, "label": "treasure island" }, { "id": 3, "label": "dr jekyll , mr hyde" } ] } }
and
{ "id": 2, "name": "philip k. dick", "books": { "count": 1, "data": [ { "id": 4, "label": "do android dream of electric sheep" } ] } }
i have array of book id's, [1,4]
; how write query keyword search of author name , returns them if wrote 1 of books in array?
i haven't managed query doesn't cause sort of query parse_exception
, starting block, here's current iteration of query - maybe it's obvious i'm going wrong?
{ "query": { "bool": { "must": { "match": { "label": "louis" } } }, "nested": { "path": "books.data", "query": { "bool": { "must": { "terms": { "books.data.id": [ 1, 4 ] } } } } } }, "from": 0, "size": 8 }
in above scenario i'd document mr robert louis stevenson returned, name contains louis
, wrote book id 1
.
for it's worth, current error looks this:
{ "error": { "root_cause": [ { "type": "parse_exception", "reason": "failed parse search source. expected field name got [start_object]" } ], "type": "search_phase_execution_exception", "reason": "all shards failed", "phase": "query", "grouped": true, "failed_shards": [ { "shard": 0, "index": "author", "node": "sck3su4ysnqhvdtgjoztlw", "reason": { "type": "parse_exception", "reason": "failed parse search source. expected field name got [start_object]" } } ] }, "status": 400 }
this makes me feel i've got "nested" object wrong, docs suggest i'm right!
you have right, nested
query must located inside bool
1 in query below. match
query needs made on name
field since author name stored:
{ "query": { "bool": { "must": [ { "match": { "name": "louis" } }, { "nested": { "path": "books.data", "query": { "bool": { "must": { "terms": { "books.data.id": [ 1, 4 ] } } } } } } ] } }, "from": 0, "size": 8 }
Comments
Post a Comment