# ElasticSearch: Mapping & querying multi-layer nested objects

**URL:** <https://discuss.elastic.co/t/elasticsearch-mapping-querying-multi-layer-nested-objects/13790>\
**Category:** Elasticsearch\
**Created:** [September 27, 2013, 4:11pm UTC](https://discuss.elastic.co/t/elasticsearch-mapping-querying-multi-layer-nested-objects/13790 "2013-09-27T16:11:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nirmala\_ranganathan](https://avatars.discourse-cdn.com/v4/letter/n/e99b99/32.png) [@nirmala\_ranganathan](https://discuss.elastic.co/u/nirmala_ranganathan)\
**Post date:** [September 27, 2013, 4:11pm UTC](https://discuss.elastic.co/t/elasticsearch-mapping-querying-multi-layer-nested-objects/13790/1 "2013-09-27T16:11:44Z")

</div>

This question might have been answered but it is hard to find it in this  
forum. Thanks for your help.

I have multi-level nested object, say for example -  
In this case eData is a nested object that contains bData and contact.

{  
"eId" : "123",  
"eData" : {  
"bData" : {  
"reviewList" : [  
{  
"id" : "1950158",  
"author" : "fancylaw",  
"text" : "I love this Beauty salon. The staff is friendly and professional.  
",  
"rating" : "5",  
"date" : "2007-07-23",  
},  
{  
"id" : "1950159",  
"author" : "mmeylor",  
"text" : "I called & asked the price of a haircut & was told $45. When I  
got there I found out that if I wanted it blow dryed it would actually be  
$80! ",  
"rating" : "1",  
"date" : "2009-03-20",  
},  
],  
"categoryList" : [  
"Beauty & Spas",  
"Hair Care & Salons",  
"Wigs Toupees & Hairpieces"  
]  
},  
"contact" : {  
"streetAddress" : "124 1/2 N Larchmont Blvd",  
"phone" : "3234612979",  
"website" : "[http://www.jessica.com](http://www.jessica.com)",  
"country" : "USA",  
"addressLocality" : "Los Angeles",  
"postalCode" : "90004",  
"email" : "sshssh@gmail.com",  
"addressRegion" : "CA",  
"googlePlusUrl" : "[http://googleplus](http://googleplus)",  
"name" : "Jessica's Beauty",  
"twitterUrl" : "[http://twitter](http://twitter)",  
}  
}  
}

Then I do the mapping.

_Question:_ Do I need to map every single field in here or just a high  
level field (of type = nested) is good enough? In other words, in this  
mapping below, I have set bData and contact type as nested but didn't list  
all the fields that it holds. I thought this is good enough to be able to  
query.

curl -XPOST '[http://localhost:9200/myindex/](http://localhost:9200/myindex/)' [http://localhost:9200/mongoentityindex/'](http://localhost:9200/mongoentityindex/') -d '  
{  
"mappings":{  
"eType":{  
"properties":{  
"eData":{  
"type": "nested",  
"include\_in\_parent": true,  
"include\_in\_root": true,  
"properties": {  
"bData": {  
"type": "nested"  
},  
"contact": {  
"type": "nested"  
}  
}  
}  
}  
}  
}  
}'

Next to sync the data from my mongo database. It works fine. I can see that  
all the data gets loaded as expected.

curl -XPUT '[http://localhost:9200/\_river/mbservices/\_meta](http://localhost:9200/_river/mbservices/_meta)' -d '{ "type":  
"mongodb", "mongodb": { "db": "mydb", "collection": "entity" }, "index": {  
"name": "myindex", "type": "eType" } }'

Now I would like to query for text "Beauty" within the nested field path  
"eData".  
You can see from the sample record above the word "Beauty" exists in 3  
fields "eData.bData.reviewList[0].text", "eData.bData.categoryList" and  
"eData.bData.contact".  
I expect the output fields to contain eId and only those that had the  
matched text. I would like to highlight the part of the matched text to the  
user.  
The following search returns nothing.

curl -XGET 'localhost:9200/myindex/\_search?pretty=true' -d '  
{  
"query": {  
"nested": {  
"path": "eData",  
"score\_mode": "max",  
"query": {  
"query\_string": {  
"fields": [  
"eData.contact", "eData.bData"  
],  
"query": "Beauty"  
}  
}  
}  
}  
}'

When I search for the text "Beauty" within "eData.contact.name" in path  
"eData.contact", it works.  
But in case I don't even know where the text will be present and if present  
in any fields, I need to display that record to the user.

curl -XGET 'localhost:9200/mongoentityindex/\_search?pretty=true' -d '  
{  
"fields" : ["entityData.contact.name"],  
"query": {  
"nested": {  
"path": "entityData.contact",  
"score\_mode": "max",  
"query": {  
"query\_string": {  
"fields": [  
"eData.contact.name"  
],  
"query": "Beauty"  
}  
}  
}  
}  
}'

If you need additional information, please let me know. Thanks again for  
your help.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 2:14am UTC](https://discuss.elastic.co/t/elasticsearch-mapping-querying-multi-layer-nested-objects/13790/2 "2017-07-06T02:14:24Z")

</div>


