Select suitable objects from an array in elasticsearch

{ class: 1, users: [{ name: 'abc', surname: 'def' }, { name: 'xyz', surname: 'wef' }, { name: 'abc', surname: 'pqr' }] } 

I have a document structure like the one above and I want to return only users with the name "abc", but the problem is that it matches the name "abc" but returns all arrays. I want only consistent users.

Matching -

 { "class":"string", "users" : { "type" : "nested", "properties": { "name" : {"type": "string" }, "surname" : {"type": "string" } } } } 
+3
source share
1 answer

Then, if you have a users field displayed as a nested type, this is a good start!

Using the nested inner_hits , you can only get matching usernames with the same query:

 { "_source": false, "query": { "nested": { "path": "users", "inner_hits": { <---- this is where the magic happens "_source": [ "name" ] }, "query": { "bool": { "must": [ { "term": { "users.name": "abc" } } ] } } } } } 
+5
source

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


All Articles