We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.

Sabo Lucian • 7 years ago

Thanks for this post ! I don't see how you can avoid to index a nested type when you have array of objects, if you want your search to correlate with other values of the same nested object.
For instance you have an array of objects with type and name. You want to match the name only if type is "x". I don't see how you can do that with the flat structure, because that's what the nested object is trying to fix in Elastic. Do you have some ideas on how to use the flattened structure with this? My only idea is to add a new mapping for that nested object.

smnh • 7 years ago

Hi Sabo Lucian
Sorry for the late answer, but better late than never! :)
You can mix multiple "bool" queries to search by multiple nested fields from the same document. Although the structure is flat, all the flattened fields are still related to the same parent document.
I've answered a similar question to yours here: https://stackoverflow.com/q...
Just instead of the "type" and "name" fields, that question has "type" and "memorygb" fields but the solution is the same.
Note the outer "query.bool.must" that makes sure that both nested documents will be related to the same parent document, and the inner "query.bool.must" makes sure that field name and field value of a single nested flat document will be related.

Sabo Lucian • 7 years ago

Thank you for your answer. I think your solution addresses something else, which I already solved in the same way. You are matching items in the same document, but what I asked is how to match and correlate different array items in the same document.
Let me give you an example. I have an array of persons in a document. A person has two properties: role and name. I want to retrieve the documents where role is "admin" and name matches "John", I can have two persons in a document: one is an admin but is not named John and the second is not an admin but his name is John. I want to satisfy both conditions on the same array item. And that's what the Nested type solves in Elastic, because the object type is internally flattened by Lucene.

smnh • 7 years ago

Oh, I see. Yes, if you have a document, with an array field of objects, then that array will be flattened and you will not be able to correlate by fields of its objects.

Guest • 6 years ago
smnh • 6 years ago

Hi @caibar, thank you for your interest in this post. But I would suggest posting a question to StackOverflow. You will get much more better answers there :)