# Dynamic template for nested object at arbitrary locations in document?

**URL:** <https://discuss.elastic.co/t/dynamic-template-for-nested-object-at-arbitrary-locations-in-document/14024>\
**Category:** Elasticsearch\
**Created:** [October 19, 2013, 5:06am UTC](https://discuss.elastic.co/t/dynamic-template-for-nested-object-at-arbitrary-locations-in-document/14024 "2013-10-19T05:06:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Myung](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/daniel_myung/32/2042_2.png) [@Daniel\_Myung](https://discuss.elastic.co/u/Daniel_Myung)\
**Post date:** [October 19, 2013, 5:06am UTC](https://discuss.elastic.co/t/dynamic-template-for-nested-object-at-arbitrary-locations-in-document/14024/1 "2013-10-19T05:06:48Z")

</div>

Hi,

We have user defined data coming in that may have interspersed within its  
data a "packet" of well structured data that we'd like to index as a nested  
document.

so for example:

form1 = {  
"name": "dan",  
"location": "zambia",  
"CASEBLOCK": {  
"item\_id": "asfsaf",  
"create": {  
etc, etc...  
}  
}

form2 = {  
"name": "logistics",  
"role": "foo",  
"manifest": {  
"customer1": {  
"CASEBLOCK": ....  
},  
"customer2": {  
"CASEBLOCK": ...  
}  
}  
}

form3 = {  
"foo": { "CASEBLOCK": ...},  
"bar": { "hello": {"goodbye": {"CASEBLOCK": ... } } }  
}

etc. etc.

1: Is there a dynamic\_template matching definition that allows me to  
capture all instances of CASEBOCK? I want to be able to do a nested query  
on the caseblock to float up the parent doc.

I have little control in how customers might set up their document  
structure, but I do know that that tag and the structure within that tag  
are well defined. In the example for form1, I can capture that with a  
dynamic template. However, form2 and form3 examples, I have little  
knowledge ahead of time to know how to structure an arbitrary match rule.

I've experimented with making multiple dynamic templates with path\_matches  
like:

path\_match: "_.CASEBLOCK" =\> this is indexed  
path\_match: "_._.CASEBLOCK  
path\_match: "_._._.CASEBLOCK  
...

but the deeper objects don't get detected despite the overall mapping of  
the form documents being set to dynamic:true

2: With the arbitrary depth and path, is it possible to have a query based  
upon properties in the nested object be done without knowledge of its  
depth? The documentation seems to suggest that knowing the full depth of a  
nested object is required.

So instead of needing to do form1.CASEBLOCK AND  
form2.manifest.customer1.CASEBLOCK etc etc. I just want to interrogate the  
index and query CASEBLOCK.foo and see which parent docs have it. I won't  
necessarily now the path's depth ahead of time...but i will know the  
structured object's information.

I'm running 0.90.5

Thanks,

Dan

Thanks,

Dan

--  
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:** ![javanna](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/javanna/32/4698_2.png) [@javanna](https://discuss.elastic.co/u/javanna)\
**Post date:** [October 21, 2013, 9:35am UTC](https://discuss.elastic.co/t/dynamic-template-for-nested-object-at-arbitrary-locations-in-document/14024/2 "2013-10-21T09:35:26Z")

</div>

Hi Dan,  
if you switch to the ordinary "match" in the dynamic template, instead of  
the "path\_match", that matches on the field name regardless of its path.  
Did you give it a try already?

Regarding your query, I would play around with "path":"just\_name", which  
allows you to collapse a field to the top-level of the json object, and  
index it using just its name, instead of the whole path. Have a look here:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

. That said, I'm not sure how this would work with nested documents, as I  
haven't tried it myself. Maybe you can give it a shot and let us know how  
it went?

On Saturday, October 19, 2013 7:06:48 AM UTC+2, dmyung wrote:

> Hi,
> 
> We have user defined data coming in that may have interspersed within its  
> data a "packet" of well structured data that we'd like to index as a nested  
> document.
> 
> so for example:
> 
> form1 = {  
> "name": "dan",  
> "location": "zambia",  
> "CASEBLOCK": {  
> "item\_id": "asfsaf",  
> "create": {  
> etc, etc...  
> }  
> }
> 
> form2 = {  
> "name": "logistics",  
> "role": "foo",  
> "manifest": {  
> "customer1": {  
> "CASEBLOCK": ....  
> },  
> "customer2": {  
> "CASEBLOCK": ...  
> }  
> }  
> }
> 
> form3 = {  
> "foo": { "CASEBLOCK": ...},  
> "bar": { "hello": {"goodbye": {"CASEBLOCK": ... } } }  
> }
> 
> etc. etc.
> 
> 1: Is there a dynamic\_template matching definition that allows me to  
> capture all instances of CASEBOCK? I want to be able to do a nested query  
> on the caseblock to float up the parent doc.
> 
> I have little control in how customers might set up their document  
> structure, but I do know that that tag and the structure within that tag  
> are well defined. In the example for form1, I can capture that with a  
> dynamic template. However, form2 and form3 examples, I have little  
> knowledge ahead of time to know how to structure an arbitrary match rule.
> 
> I've experimented with making multiple dynamic templates with path\_matches  
> like:
> 
> path\_match: "_.CASEBLOCK" =\> this is indexed  
> path\_match: "_._.CASEBLOCK  
> path\_match: "_._._.CASEBLOCK  
> ...
> 
> but the deeper objects don't get detected despite the overall mapping of  
> the form documents being set to dynamic:true
> 
> 2: With the arbitrary depth and path, is it possible to have a query based  
> upon properties in the nested object be done without knowledge of its  
> depth? The documentation seems to suggest that knowing the full depth of a  
> nested object is required.
> 
> So instead of needing to do form1.CASEBLOCK AND  
> form2.manifest.customer1.CASEBLOCK etc etc. I just want to interrogate the  
> index and query CASEBLOCK.foo and see which parent docs have it. I won't  
> necessarily now the path's depth ahead of time...but i will know the  
> structured object's information.
> 
> I'm running 0.90.5
> 
> Thanks,
> 
> Dan
> 
> Thanks,
> 
> Dan

--  
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:11am UTC](https://discuss.elastic.co/t/dynamic-template-for-nested-object-at-arbitrary-locations-in-document/14024/3 "2017-07-06T02:11:28Z")

</div>


