# Searching term in subdocuments with elasticsearch

**URL:** <https://discuss.elastic.co/t/searching-term-in-subdocuments-with-elasticsearch/9524>\
**Category:** Elasticsearch\
**Created:** [November 2, 2012, 7:02am UTC](https://discuss.elastic.co/t/searching-term-in-subdocuments-with-elasticsearch/9524 "2012-11-02T07:02:59Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![cubuzoa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cubuzoa/32/2648_2.png) [@cubuzoa](https://discuss.elastic.co/u/cubuzoa)\
**Post date:** [November 2, 2012, 7:02am UTC](https://discuss.elastic.co/t/searching-term-in-subdocuments-with-elasticsearch/9524/1 "2012-11-02T07:02:59Z")

</div>

I have a index document structure like below;

{  
"term":"some term",  
"inlang":"some lang"  
"translations" : {  
{  
"translation":"some translation",  
"outlang":"some lang",  
"translations" : {  
{  
"translation":"some translation 1"  
"outlang": "some lang 1"  
"translations" : {...}  
}  
}  
},  
...  
}  
}

I want to find a translation in such documents. However, this translation  
can exists at any level of this document. Is it possible to search term  
dynamically by using elasticsearch?

For example,

{  
"query": {  
"\*.translation":"searchterm"  
}  
}

Thanks in advance

--

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [November 2, 2012, 1:34pm UTC](https://discuss.elastic.co/t/searching-term-in-subdocuments-with-elasticsearch/9524/2 "2012-11-02T13:34:55Z")

</div>

You can use Query String[http://www.elasticsearch.org/guide/reference/query-dsl/query-string-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/query-string-query.html)or Multi  
Match[http://www.elasticsearch.org/guide/reference/query-dsl/multi-match-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/multi-match-query.html)queries. For example:

{  
"query": {  
"query\_string": {  
"query": "someterm",  
"fields": ["\*.translation"]  
}  
}  
}

or

{  
"query": {  
"multi\_match": {  
"query": "someterm",  
"fields": ["\*.translation"]  
}  
}  
}

On Friday, November 2, 2012 3:02:59 AM UTC-4, cubuzoa wrote:

> I have a index document structure like below;
> 
> {  
> "term":"some term",  
> "inlang":"some lang"  
> "translations" : {  
> {  
> "translation":"some translation",  
> "outlang":"some lang",  
> "translations" : {  
> {  
> "translation":"some translation 1"  
> "outlang": "some lang 1"  
> "translations" : {...}  
> }  
> }  
> },  
> ...  
> }  
> }
> 
> I want to find a translation in such documents. However, this translation  
> can exists at any level of this document. Is it possible to search term  
> dynamically by using elasticsearch?
> 
> For example,
> 
> {  
> "query": {  
> "\*.translation":"searchterm"  
> }  
> }
> 
> Thanks in advance

--

---

<div class="post-metadata">

**Author:** ![cubuzoa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cubuzoa/32/2648_2.png) [@cubuzoa](https://discuss.elastic.co/u/cubuzoa)\
**Post date:** [November 2, 2012, 2:28pm UTC](https://discuss.elastic.co/t/searching-term-in-subdocuments-with-elasticsearch/9524/3 "2012-11-02T14:28:01Z")

</div>

Thanks for your reply, it works! May i ask you a related question? Let's  
say i have two index of same size I-1 and I-2 . I-1 has 10 documents and  
I-2 has one document . I-2 has less document because it is embedded. Is  
there any disadvantage of bigger index count on elasticsearch world?

Thanks in advance

On Fri, Nov 2, 2012 at 3:34 PM, Igor Motov [imotov@gmail.com](mailto:imotov@gmail.com) wrote:

> You can use Query String[http://www.elasticsearch.org/guide/reference/query-dsl/query-string-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/query-string-query.html)or Multi  
> Match[http://www.elasticsearch.org/guide/reference/query-dsl/multi-match-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/multi-match-query.html)queries. For example:
> 
> {  
> "query": {  
> "query\_string": {  
> "query": "someterm",  
> "fields": ["\*.translation"]  
> }  
> }  
> }
> 
> or
> 
> {  
> "query": {  
> "multi\_match": {  
> "query": "someterm",  
> "fields": ["\*.translation"]  
> }  
> }  
> }
> 
> On Friday, November 2, 2012 3:02:59 AM UTC-4, cubuzoa wrote:
> 
> > I have a index document structure like below;
> > 
> > {  
> > "term":"some term",  
> > "inlang":"some lang"  
> > "translations" : {  
> > {  
> > "translation":"some translation",  
> > "outlang":"some lang",  
> > "translations" : {  
> > {  
> > "translation":"some translation 1"  
> > "outlang": "some lang 1"  
> > "translations" : {...}  
> > }  
> > }  
> > },  
> > ...  
> > }  
> > }
> > 
> > I want to find a translation in such documents. However, this translation  
> > can exists at any level of this document. Is it possible to search term  
> > dynamically by using elasticsearch?
> > 
> > For example,
> > 
> > {  
> > "query": {  
> > "\*.translation":"searchterm"  
> > }  
> > }
> > 
> > Thanks in advance
> 
> --

--

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [November 2, 2012, 4:04pm UTC](https://discuss.elastic.co/t/searching-term-in-subdocuments-with-elasticsearch/9524/4 "2012-11-02T16:04:30Z")

</div>

If I understood your question correctly, you are trying to compare indexing  
of one large document with 10 subdocuments embedded into it to indexing  
these 10 documents as separate small documents. To answer this question, I  
need to know a little bit more about what types of queries you are going to  
make against this index and what types of subdocuments you are going to  
store. Are they going to look like documents in your first post? How many  
levels are you expecting to have in a single document?

On Friday, November 2, 2012 10:28:06 AM UTC-4, cubuzoa wrote:

> Thanks for your reply, it works! May i ask you a related question? Let's  
> say i have two index of same size I-1 and I-2 . I-1 has 10 documents and  
> I-2 has one document . I-2 has less document because it is embedded. Is  
> there any disadvantage of bigger index count on elasticsearch world?
> 
> Thanks in advance
> 
> On Fri, Nov 2, 2012 at 3:34 PM, Igor Motov \<[imo...@gmail.com](mailto:imo...@gmail.com) \<javascript:\>
> 
> > wrote:
> 
> > You can use Query String[http://www.elasticsearch.org/guide/reference/query-dsl/query-string-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/query-string-query.html)or Multi  
> > Match[http://www.elasticsearch.org/guide/reference/query-dsl/multi-match-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/multi-match-query.html)queries. For example:
> > 
> > {  
> > "query": {  
> > "query\_string": {  
> > "query": "someterm",  
> > "fields": ["\*.translation"]  
> > }  
> > }  
> > }
> > 
> > or
> > 
> > {  
> > "query": {  
> > "multi\_match": {  
> > "query": "someterm",  
> > "fields": ["\*.translation"]  
> > }  
> > }  
> > }
> > 
> > On Friday, November 2, 2012 3:02:59 AM UTC-4, cubuzoa wrote:
> > 
> > > I have a index document structure like below;
> > > 
> > > {  
> > > "term":"some term",  
> > > "inlang":"some lang"  
> > > "translations" : {  
> > > {  
> > > "translation":"some translation",  
> > > "outlang":"some lang",  
> > > "translations" : {  
> > > {  
> > > "translation":"some translation 1"  
> > > "outlang": "some lang 1"  
> > > "translations" : {...}  
> > > }  
> > > }  
> > > },  
> > > ...  
> > > }  
> > > }
> > > 
> > > I want to find a translation in such documents. However, this  
> > > translation can exists at any level of this document. Is it possible to  
> > > search term dynamically by using elasticsearch?
> > > 
> > > For example,
> > > 
> > > {  
> > > "query": {  
> > > "\*.translation":"searchterm"  
> > > }  
> > > }
> > > 
> > > Thanks in advance
> > 
> > --

--

---

<div class="post-metadata">

**Author:** ![cubuzoa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cubuzoa/32/2648_2.png) [@cubuzoa](https://discuss.elastic.co/u/cubuzoa)\
**Post date:** [November 3, 2012, 6:57am UTC](https://discuss.elastic.co/t/searching-term-in-subdocuments-with-elasticsearch/9524/5 "2012-11-03T06:57:15Z")

</div>

Yes, that is exactly as you anderstood. There can be n level in one  
document.For some terms, there can be 100 level depth document. For this  
reason, i used 2 level separate documents like my example above.

On Fri, Nov 2, 2012 at 6:04 PM, Igor Motov [imotov@gmail.com](mailto:imotov@gmail.com) wrote:

> If I understood your question correctly, you are trying to compare  
> indexing of one large document with 10 subdocuments embedded into it to  
> indexing these 10 documents as separate small documents. To answer this  
> question, I need to know a little bit more about what types of queries you  
> are going to make against this index and what types of subdocuments you are  
> going to store. Are they going to look like documents in your first post?  
> How many levels are you expecting to have in a single document?
> 
> On Friday, November 2, 2012 10:28:06 AM UTC-4, cubuzoa wrote:
> 
> > Thanks for your reply, it works! May i ask you a related question? Let's  
> > say i have two index of same size I-1 and I-2 . I-1 has 10 documents and  
> > I-2 has one document . I-2 has less document because it is embedded. Is  
> > there any disadvantage of bigger index count on elasticsearch world?
> > 
> > Thanks in advance
> > 
> > On Fri, Nov 2, 2012 at 3:34 PM, Igor Motov [imo...@gmail.com](mailto:imo...@gmail.com) wrote:
> > 
> > > You can use Query String[http://www.elasticsearch.org/guide/reference/query-dsl/query-string-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/query-string-query.html)or Multi  
> > > Match[http://www.elasticsearch.org/guide/reference/query-dsl/multi-match-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/multi-match-query.html)queries. For example:
> > > 
> > > {  
> > > "query": {  
> > > "query\_string": {  
> > > "query": "someterm",  
> > > "fields": ["\*.translation"]  
> > > }  
> > > }  
> > > }
> > > 
> > > or
> > > 
> > > {  
> > > "query": {  
> > > "multi\_match": {  
> > > "query": "someterm",  
> > > "fields": ["\*.translation"]  
> > > }  
> > > }  
> > > }
> > > 
> > > On Friday, November 2, 2012 3:02:59 AM UTC-4, cubuzoa wrote:
> > > 
> > > > I have a index document structure like below;
> > > > 
> > > > {  
> > > > "term":"some term",  
> > > > "inlang":"some lang"  
> > > > "translations" : {  
> > > > {  
> > > > "translation":"some translation",  
> > > > "outlang":"some lang",  
> > > > "translations" : {  
> > > > {  
> > > > "translation":"some translation 1"  
> > > > "outlang": "some lang 1"  
> > > > "translations" : {...}  
> > > > }  
> > > > }  
> > > > },  
> > > > ...  
> > > > }  
> > > > }
> > > > 
> > > > I want to find a translation in such documents. However, this  
> > > > translation can exists at any level of this document. Is it possible to  
> > > > search term dynamically by using elasticsearch?
> > > > 
> > > > For example,
> > > > 
> > > > {  
> > > > "query": {  
> > > > "\*.translation":"searchterm"  
> > > > }  
> > > > }
> > > > 
> > > > Thanks in advance
> > > 
> > > --
> > 
> > --

--

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [November 3, 2012, 3:48pm UTC](https://discuss.elastic.co/t/searching-term-in-subdocuments-with-elasticsearch/9524/6 "2012-11-03T15:48:49Z")

</div>

I am still not quite clear about how you are using these documents. Could  
you explain the reasons for the hierarchical structure and why a document  
can have 100 levels? What's the relationship between a translation on the  
5th level, for example, and the root term? How is it different from the  
translation on the 4th level? Can you just flatten all translations into an  
array, or you will loose some information if you will do it this way? It's  
typically better to flatten the document and there are many way to do it,  
but without knowing your use case, it's difficult to give a  
good recommendation here. Would something like work for you, for example?

{  
"translation.cs":"ananasovník",  
"[translation.de](http://translation.de)":"ananas",  
"translation.en":"pineapple",  
"translation.es":"ananas",  
"translation.he":"אננס",  
"translation.nl":"ananas",  
"[translation.ru](http://translation.ru)":"ананас",  
"translation.tr":"ananas"  
}

On Saturday, November 3, 2012 2:57:21 AM UTC-4, cubuzoa wrote:

> Yes, that is exactly as you anderstood. There can be n level in one  
> document.For some terms, there can be 100 level depth document. For this  
> reason, i used 2 level separate documents like my example above.
> 
> On Fri, Nov 2, 2012 at 6:04 PM, Igor Motov \<[imo...@gmail.com](mailto:imo...@gmail.com) \<javascript:\>
> 
> > wrote:
> 
> > If I understood your question correctly, you are trying to compare  
> > indexing of one large document with 10 subdocuments embedded into it to  
> > indexing these 10 documents as separate small documents. To answer this  
> > question, I need to know a little bit more about what types of queries you  
> > are going to make against this index and what types of subdocuments you are  
> > going to store. Are they going to look like documents in your first post?  
> > How many levels are you expecting to have in a single document?
> > 
> > On Friday, November 2, 2012 10:28:06 AM UTC-4, cubuzoa wrote:
> > 
> > > Thanks for your reply, it works! May i ask you a related question? Let's  
> > > say i have two index of same size I-1 and I-2 . I-1 has 10 documents and  
> > > I-2 has one document . I-2 has less document because it is embedded. Is  
> > > there any disadvantage of bigger index count on elasticsearch world?
> > > 
> > > Thanks in advance
> > > 
> > > On Fri, Nov 2, 2012 at 3:34 PM, Igor Motov [imo...@gmail.com](mailto:imo...@gmail.com) wrote:
> > > 
> > > > You can use Query String[http://www.elasticsearch.org/guide/reference/query-dsl/query-string-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/query-string-query.html)or Multi  
> > > > Match[http://www.elasticsearch.org/guide/reference/query-dsl/multi-match-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/multi-match-query.html)queries. For example:
> > > > 
> > > > {  
> > > > "query": {  
> > > > "query\_string": {  
> > > > "query": "someterm",  
> > > > "fields": ["\*.translation"]  
> > > > }  
> > > > }  
> > > > }
> > > > 
> > > > or
> > > > 
> > > > {  
> > > > "query": {  
> > > > "multi\_match": {  
> > > > "query": "someterm",  
> > > > "fields": ["\*.translation"]  
> > > > }  
> > > > }  
> > > > }
> > > > 
> > > > On Friday, November 2, 2012 3:02:59 AM UTC-4, cubuzoa wrote:
> > > > 
> > > > > I have a index document structure like below;
> > > > > 
> > > > > {  
> > > > > "term":"some term",  
> > > > > "inlang":"some lang"  
> > > > > "translations" : {  
> > > > > {  
> > > > > "translation":"some translation",  
> > > > > "outlang":"some lang",  
> > > > > "translations" : {  
> > > > > {  
> > > > > "translation":"some translation 1"  
> > > > > "outlang": "some lang 1"  
> > > > > "translations" : {...}  
> > > > > }  
> > > > > }  
> > > > > },  
> > > > > ...  
> > > > > }  
> > > > > }
> > > > > 
> > > > > I want to find a translation in such documents. However, this  
> > > > > translation can exists at any level of this document. Is it possible to  
> > > > > search term dynamically by using elasticsearch?
> > > > > 
> > > > > For example,
> > > > > 
> > > > > {  
> > > > > "query": {  
> > > > > "\*.translation":"searchterm"  
> > > > > }  
> > > > > }
> > > > > 
> > > > > Thanks in advance
> > > > 
> > > > --
> > > 
> > > --

--

---

<div class="post-metadata">

**Author:** ![cubuzoa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cubuzoa/32/2648_2.png) [@cubuzoa](https://discuss.elastic.co/u/cubuzoa)\
**Post date:** [November 3, 2012, 7:05pm UTC](https://discuss.elastic.co/t/searching-term-in-subdocuments-with-elasticsearch/9524/7 "2012-11-03T19:05:45Z")

</div>

Here is my current index document;

{  
"\_index":"u7gpslhmzbop3usl",  
"\_type":"terms",  
"\_id":"50922fca1e028a020000000f",  
"\_score":3.2512918,  
"\_source":{  
"lang":"en",  
"term":"use",  
"translations":[  
{  
"\_id":"50922fca1e028a0200000010",  
"lang":"tr",  
"translation":"kullanmak",  
"score":"0",  
"createDate":"2012-11-01T08:16:10.531Z",  
"author":"anonymous"  
},  
{  
"\_id":"509568056a435ff40c000003",  
"lang":"ar",  
"translation":"استخدم",  
"score":"0",  
"createDate":"2012-11-03T18:52:53.144Z",  
"author":"anonymous"  
},  
{  
"\_id":"509568186a435ff40c00000b",  
"lang":"pt",  
"translation":"usar",  
"score":"0",  
"createDate":"2012-11-03T18:53:12.834Z",  
"author":"anonymous"  
},  
{  
"\_id":"5095682b6a435ff40c000017",  
"lang":"ru",  
"translation":"использовать",  
"score":"0",  
"createDate":"2012-11-03T18:53:31.195Z",  
"author":"anonymous"  
},  
{  
"\_id":"5095683e6a435ff40c000027",  
"lang":"de",  
"translation":"verwenden",  
"score":"0",  
"createDate":"2012-11-03T18:53:50.812Z",  
"author":"anonymous"  
},  
{  
"\_id":"509568516a435ff40c00003b",  
"lang":"fr",  
"translation":"utiliser",  
"score":"0",  
"createDate":"2012-11-03T18:54:09.416Z",  
"author":"anonymous"  
}  
],  
"author":"anonymous",  
"createDate":"2012-11-01T08:16:10.531Z"  
}  
}

For the term \*use(en) \*i have added 6 translations. When i added  
_kullanmak(tr)_ as translation of term \*use(en), \*i create a new term  
lieke this;

{  
"\_index":"u7gpslhmzbop3usl",  
"\_type":"terms",  
"\_id":"50922fca1e028a0200000011",  
"\_score":3.2512918,  
"\_source":{  
"lang":"tr",  
"term":"kullanmak",  
"translations":[  
{  
"\_id":"50922fca1e028a0200000012",  
"lang":"en",  
"translation":"use",  
"score":"0",  
"createDate":"2012-11-01T08:16:10.549Z",  
"author":"anonymous"  
}  
],  
"author":"anonymous",  
"createDate":"2012-11-01T08:16:10.549Z"  
}  
}

In other words, whenever i create a term and translation, i also  
create reverse translation. en-\>tr and tr-\>en for example. This  
doubles my document size.

When i search for "kullanmak" or "use", one result will be responded.  
In here, doubling documents is and disadvantage?

On Sat, Nov 3, 2012 at 5:48 PM, Igor Motov [imotov@gmail.com](mailto:imotov@gmail.com) wrote:

> I am still not quite clear about how you are using these documents. Could  
> you explain the reasons for the hierarchical structure and why a document  
> can have 100 levels? What's the relationship between a translation on the  
> 5th level, for example, and the root term? How is it different from the  
> translation on the 4th level? Can you just flatten all translations into an  
> array, or you will loose some information if you will do it this way? It's  
> typically better to flatten the document and there are many way to do it,  
> but without knowing your use case, it's difficult to give a  
> good recommendation here. Would something like work for you, for example?
> 
> {  
> "translation.cs":"ananasovník",  
> "[translation.de](http://translation.de)":"ananas",  
> "translation.en":"pineapple",  
> "translation.es":"ananas",  
> "translation.he":"אננס",  
> "translation.nl":"ananas",  
> "[translation.ru](http://translation.ru)":"ананас",  
> "translation.tr":"ananas"  
> }
> 
> On Saturday, November 3, 2012 2:57:21 AM UTC-4, cubuzoa wrote:
> 
> > Yes, that is exactly as you anderstood. There can be n level in one  
> > document.For some terms, there can be 100 level depth document. For this  
> > reason, i used 2 level separate documents like my example above.
> > 
> > On Fri, Nov 2, 2012 at 6:04 PM, Igor Motov [imo...@gmail.com](mailto:imo...@gmail.com) wrote:
> > 
> > > If I understood your question correctly, you are trying to compare  
> > > indexing of one large document with 10 subdocuments embedded into it to  
> > > indexing these 10 documents as separate small documents. To answer this  
> > > question, I need to know a little bit more about what types of queries you  
> > > are going to make against this index and what types of subdocuments you are  
> > > going to store. Are they going to look like documents in your first post?  
> > > How many levels are you expecting to have in a single document?
> > > 
> > > On Friday, November 2, 2012 10:28:06 AM UTC-4, cubuzoa wrote:
> > > 
> > > > Thanks for your reply, it works! May i ask you a related question?  
> > > > Let's say i have two index of same size I-1 and I-2 . I-1 has 10 documents  
> > > > and I-2 has one document . I-2 has less document because it is embedded. Is  
> > > > there any disadvantage of bigger index count on elasticsearch world?
> > > > 
> > > > Thanks in advance
> > > > 
> > > > On Fri, Nov 2, 2012 at 3:34 PM, Igor Motov [imo...@gmail.com](mailto:imo...@gmail.com) wrote:
> > > > 
> > > > > You can use Query String[http://www.elasticsearch.org/guide/reference/query-dsl/query-string-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/query-string-query.html)or Multi  
> > > > > Match[http://www.elasticsearch.org/guide/reference/query-dsl/multi-match-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/multi-match-query.html)queries. For example:
> > > > > 
> > > > > {  
> > > > > "query": {  
> > > > > "query\_string": {  
> > > > > "query": "someterm",  
> > > > > "fields": ["\*.translation"]  
> > > > > }  
> > > > > }  
> > > > > }
> > > > > 
> > > > > or
> > > > > 
> > > > > {  
> > > > > "query": {  
> > > > > "multi\_match": {  
> > > > > "query": "someterm",  
> > > > > "fields": ["\*.translation"]  
> > > > > }  
> > > > > }  
> > > > > }
> > > > > 
> > > > > On Friday, November 2, 2012 3:02:59 AM UTC-4, cubuzoa wrote:
> > > > > 
> > > > > > I have a index document structure like below;
> > > > > > 
> > > > > > {  
> > > > > > "term":"some term",  
> > > > > > "inlang":"some lang"  
> > > > > > "translations" : {  
> > > > > > {  
> > > > > > "translation":"some translation",  
> > > > > > "outlang":"some lang",  
> > > > > > "translations" : {  
> > > > > > {  
> > > > > > "translation":"some translation 1"  
> > > > > > "outlang": "some lang 1"  
> > > > > > "translations" : {...}  
> > > > > > }  
> > > > > > }  
> > > > > > },  
> > > > > > ...  
> > > > > > }  
> > > > > > }
> > > > > > 
> > > > > > I want to find a translation in such documents. However, this  
> > > > > > translation can exists at any level of this document. Is it possible to  
> > > > > > search term dynamically by using elasticsearch?
> > > > > > 
> > > > > > For example,
> > > > > > 
> > > > > > {  
> > > > > > "query": {  
> > > > > > "\*.translation":"searchterm"  
> > > > > > }  
> > > > > > }
> > > > > > 
> > > > > > Thanks in advance
> > > > > 
> > > > > --
> > > > 
> > > > --
> > 
> > --

--

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [November 4, 2012, 2:50am UTC](https://discuss.elastic.co/t/searching-term-in-subdocuments-with-elasticsearch/9524/8 "2012-11-04T02:50:31Z")

</div>

That schema could be fine depending on the types of searches that are you  
doing. Are you searching for a term in a particular language and want to  
retrieve all translations? One possible variant that doesn't store all  
terms twice would be this:

{  
"en":{  
"\_id": "50922fca1e028a0200000010",  
"translation": "use",  
"score": "0",  
"createDate": "2012-11-01T08:16:10.531Z",  
"author": "anonymous"  
},  
"tr": {  
"\_id": "50922fca1e028a0200000010",  
"translation": "kullanmak",  
"score": "0",  
"createDate": "2012-11-01T08:16:10.531Z",  
"author": "anonymous"  
},  
"ar": {  
"\_id": "509568056a435ff40c000003",  
"translation": "استخدم",  
"score": "0",  
"createDate": "2012-11-03T18:52:53.144Z",  
"author": "anonymous"  
},  
"pt": {  
"\_id": "509568186a435ff40c00000b",  
"translation": "usar",  
"score": "0",  
"createDate": "2012-11-03T18:53:12.834Z",  
"author": "anonymous"  
},  
"ru": {  
"\_id": "5095682b6a435ff40c000017",  
"translation": "использовать",  
"score": "0",  
"createDate": "2012-11-03T18:53:31.195Z",  
"author": "anonymous"  
},  
"de": {  
"\_id": "5095683e6a435ff40c000027",  
"translation": "verwenden",  
"score": "0",  
"createDate": "2012-11-03T18:53:50.812Z",  
"author": "anonymous"  
},  
"fr": {  
"\_id": "509568516a435ff40c00003b",  
"translation": "utiliser",  
"score": "0",  
"createDate": "2012-11-03T18:54:09.416Z",  
"author": "anonymous"  
}  
}

Then if you need translation from English, you can search using  
en.translation field, and if you need translation from Turkish, search  
using tr.translation field. However, in this case you are loosing  
some flexibility if you will ever need to capture a term that doesn't  
translate back to the original term, like:

employ-\>kullanmak-\>use

On Saturday, November 3, 2012 3:05:52 PM UTC-4, cubuzoa wrote:

> Here is my current index document;
> 
> {  
> "\_index":"u7gpslhmzbop3usl",  
> "\_type":"terms",  
> "\_id":"50922fca1e028a020000000f",  
> "\_score":3.2512918,  
> "\_source":{  
> "lang":"en",  
> "term":"use",  
> "translations":[  
> {  
> "\_id":"50922fca1e028a0200000010",  
> "lang":"tr",  
> "translation":"kullanmak",  
> "score":"0",  
> "createDate":"2012-11-01T08:16:10.531Z",  
> "author":"anonymous"  
> },  
> {  
> "\_id":"509568056a435ff40c000003",  
> "lang":"ar",  
> "translation":"استخدم",  
> "score":"0",  
> "createDate":"2012-11-03T18:52:53.144Z",  
> "author":"anonymous"  
> },  
> {  
> "\_id":"509568186a435ff40c00000b",  
> "lang":"pt",  
> "translation":"usar",  
> "score":"0",  
> "createDate":"2012-11-03T18:53:12.834Z",  
> "author":"anonymous"  
> },  
> {  
> "\_id":"5095682b6a435ff40c000017",  
> "lang":"ru",  
> "translation":"использовать",  
> "score":"0",  
> "createDate":"2012-11-03T18:53:31.195Z",  
> "author":"anonymous"  
> },  
> {  
> "\_id":"5095683e6a435ff40c000027",  
> "lang":"de",  
> "translation":"verwenden",  
> "score":"0",  
> "createDate":"2012-11-03T18:53:50.812Z",  
> "author":"anonymous"  
> },  
> {  
> "\_id":"509568516a435ff40c00003b",  
> "lang":"fr",  
> "translation":"utiliser",  
> "score":"0",  
> "createDate":"2012-11-03T18:54:09.416Z",  
> "author":"anonymous"  
> }  
> ],  
> "author":"anonymous",  
> "createDate":"2012-11-01T08:16:10.531Z"  
> }  
> }
> 
> For the term \*use(en) \*i have added 6 translations. When i added _kullanmak(tr)_ as translation of term \*use(en), \*i create a new term lieke this;
> 
> {  
> "\_index":"u7gpslhmzbop3usl",  
> "\_type":"terms",  
> "\_id":"50922fca1e028a0200000011",  
> "\_score":3.2512918,  
> "\_source":{  
> "lang":"tr",  
> "term":"kullanmak",  
> "translations":[  
> {  
> "\_id":"50922fca1e028a0200000012",  
> "lang":"en",  
> "translation":"use",  
> "score":"0",  
> "createDate":"2012-11-01T08:16:10.549Z",  
> "author":"anonymous"  
> }  
> ],  
> "author":"anonymous",  
> "createDate":"2012-11-01T08:16:10.549Z"  
> }  
> }
> 
> In other words, whenever i create a term and translation, i also create reverse translation. en-\>tr and tr-\>en for example. This doubles my document size.
> 
> When i search for "kullanmak" or "use", one result will be responded. In here, doubling documents is and disadvantage?
> 
> On Sat, Nov 3, 2012 at 5:48 PM, Igor Motov \<[imo...@gmail.com](mailto:imo...@gmail.com) \<javascript:\>
> 
> > wrote:
> 
> > I am still not quite clear about how you are using these documents. Could  
> > you explain the reasons for the hierarchical structure and why a document  
> > can have 100 levels? What's the relationship between a translation on the  
> > 5th level, for example, and the root term? How is it different from the  
> > translation on the 4th level? Can you just flatten all translations into an  
> > array, or you will loose some information if you will do it this way? It's  
> > typically better to flatten the document and there are many way to do it,  
> > but without knowing your use case, it's difficult to give a  
> > good recommendation here. Would something like work for you, for example?
> > 
> > {  
> > "translation.cs":"ananasovník",  
> > "[translation.de](http://translation.de)":"ananas",  
> > "translation.en":"pineapple",  
> > "translation.es":"ananas",  
> > "translation.he":"אננס",  
> > "translation.nl":"ananas",  
> > "[translation.ru](http://translation.ru)":"ананас",  
> > "translation.tr":"ananas"  
> > }
> > 
> > On Saturday, November 3, 2012 2:57:21 AM UTC-4, cubuzoa wrote:
> > 
> > > Yes, that is exactly as you anderstood. There can be n level in one  
> > > document.For some terms, there can be 100 level depth document. For this  
> > > reason, i used 2 level separate documents like my example above.
> > > 
> > > On Fri, Nov 2, 2012 at 6:04 PM, Igor Motov [imo...@gmail.com](mailto:imo...@gmail.com) wrote:
> > > 
> > > > If I understood your question correctly, you are trying to compare  
> > > > indexing of one large document with 10 subdocuments embedded into it to  
> > > > indexing these 10 documents as separate small documents. To answer this  
> > > > question, I need to know a little bit more about what types of queries you  
> > > > are going to make against this index and what types of subdocuments you are  
> > > > going to store. Are they going to look like documents in your first post?  
> > > > How many levels are you expecting to have in a single document?
> > > > 
> > > > On Friday, November 2, 2012 10:28:06 AM UTC-4, cubuzoa wrote:
> > > > 
> > > > > Thanks for your reply, it works! May i ask you a related question?  
> > > > > Let's say i have two index of same size I-1 and I-2 . I-1 has 10 documents  
> > > > > and I-2 has one document . I-2 has less document because it is embedded. Is  
> > > > > there any disadvantage of bigger index count on elasticsearch world?
> > > > > 
> > > > > Thanks in advance
> > > > > 
> > > > > On Fri, Nov 2, 2012 at 3:34 PM, Igor Motov [imo...@gmail.com](mailto:imo...@gmail.com) wrote:
> > > > > 
> > > > > > You can use Query String[http://www.elasticsearch.org/guide/reference/query-dsl/query-string-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/query-string-query.html)or Multi  
> > > > > > Match[http://www.elasticsearch.org/guide/reference/query-dsl/multi-match-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/multi-match-query.html)queries. For example:
> > > > > > 
> > > > > > {  
> > > > > > "query": {  
> > > > > > "query\_string": {  
> > > > > > "query": "someterm",  
> > > > > > "fields": ["\*.translation"]  
> > > > > > }  
> > > > > > }  
> > > > > > }
> > > > > > 
> > > > > > or
> > > > > > 
> > > > > > {  
> > > > > > "query": {  
> > > > > > "multi\_match": {  
> > > > > > "query": "someterm",  
> > > > > > "fields": ["\*.translation"]  
> > > > > > }  
> > > > > > }  
> > > > > > }
> > > > > > 
> > > > > > On Friday, November 2, 2012 3:02:59 AM UTC-4, cubuzoa wrote:
> > > > > > 
> > > > > > > I have a index document structure like below;
> > > > > > > 
> > > > > > > {  
> > > > > > > "term":"some term",  
> > > > > > > "inlang":"some lang"  
> > > > > > > "translations" : {  
> > > > > > > {  
> > > > > > > "translation":"some translation",  
> > > > > > > "outlang":"some lang",  
> > > > > > > "translations" : {  
> > > > > > > {  
> > > > > > > "translation":"some translation 1"  
> > > > > > > "outlang": "some lang 1"  
> > > > > > > "translations" : {...}  
> > > > > > > }  
> > > > > > > }  
> > > > > > > },  
> > > > > > > ...  
> > > > > > > }  
> > > > > > > }
> > > > > > > 
> > > > > > > I want to find a translation in such documents. However, this  
> > > > > > > translation can exists at any level of this document. Is it possible to  
> > > > > > > search term dynamically by using elasticsearch?
> > > > > > > 
> > > > > > > For example,
> > > > > > > 
> > > > > > > {  
> > > > > > > "query": {  
> > > > > > > "\*.translation":"searchterm"  
> > > > > > > }  
> > > > > > > }
> > > > > > > 
> > > > > > > Thanks in advance
> > > > > > 
> > > > > > --
> > > > > 
> > > > > --
> > > 
> > > --

--

---

<div class="post-metadata">

**Author:** ![cubuzoa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cubuzoa/32/2648_2.png) [@cubuzoa](https://discuss.elastic.co/u/cubuzoa)\
**Post date:** [November 4, 2012, 11:51am UTC](https://discuss.elastic.co/t/searching-term-in-subdocuments-with-elasticsearch/9524/9 "2012-11-04T11:51:43Z")

</div>

Thanks for your recommendations, they really helped me! I will analyze all  
the stuffs you said and find a proper solution.

On Sun, Nov 4, 2012 at 4:50 AM, Igor Motov [imotov@gmail.com](mailto:imotov@gmail.com) wrote:

> That schema could be fine depending on the types of searches that are you  
> doing. Are you searching for a term in a particular language and want to  
> retrieve all translations? One possible variant that doesn't store all  
> terms twice would be this:
> 
> {  
> "en":{  
> "\_id": "50922fca1e028a0200000010",  
> "translation": "use",  
> "score": "0",  
> "createDate": "2012-11-01T08:16:10.531Z",  
> "author": "anonymous"  
> },  
> "tr": {  
> "\_id": "50922fca1e028a0200000010",  
> "translation": "kullanmak",  
> "score": "0",  
> "createDate": "2012-11-01T08:16:10.531Z",  
> "author": "anonymous"  
> },  
> "ar": {  
> "\_id": "509568056a435ff40c000003",  
> "translation": "استخدم",  
> "score": "0",  
> "createDate": "2012-11-03T18:52:53.144Z",  
> "author": "anonymous"  
> },  
> "pt": {  
> "\_id": "509568186a435ff40c00000b",  
> "translation": "usar",  
> "score": "0",  
> "createDate": "2012-11-03T18:53:12.834Z",  
> "author": "anonymous"  
> },  
> "ru": {  
> "\_id": "5095682b6a435ff40c000017",  
> "translation": "использовать",  
> "score": "0",  
> "createDate": "2012-11-03T18:53:31.195Z",  
> "author": "anonymous"  
> },  
> "de": {  
> "\_id": "5095683e6a435ff40c000027",  
> "translation": "verwenden",  
> "score": "0",  
> "createDate": "2012-11-03T18:53:50.812Z",  
> "author": "anonymous"  
> },  
> "fr": {  
> "\_id": "509568516a435ff40c00003b",  
> "translation": "utiliser",  
> "score": "0",  
> "createDate": "2012-11-03T18:54:09.416Z",  
> "author": "anonymous"  
> }  
> }
> 
> Then if you need translation from English, you can search using  
> en.translation field, and if you need translation from Turkish, search  
> using tr.translation field. However, in this case you are loosing  
> some flexibility if you will ever need to capture a term that doesn't  
> translate back to the original term, like:
> 
> employ-\>kullanmak-\>use
> 
> On Saturday, November 3, 2012 3:05:52 PM UTC-4, cubuzoa wrote:
> 
> > Here is my current index document;
> > 
> > {  
> > "\_index":"u7gpslhmzbop3usl"\*\*,  
> > "\_type":"terms",  
> > "\_id":"\*\*50922fca1e028a020000000f",  
> > "\_score":3.2512918,  
> > "\_source":{  
> > "lang":"en",  
> > "term":"use",  
> > "translations":[  
> > {  
> > "\_id":"\*\*50922fca1e028a0200000010",  
> > "lang":"tr",  
> > "translation":"\*\*kullanmak",  
> > "score":"0",  
> > "createDate":"\*\*2012-11-01T08:16:10.531Z",  
> > "author":"\*\*anonymous"  
> > },  
> > {  
> > "\_id":"\*\*509568056a435ff40c000003",  
> > "lang":"ar",  
> > "translation":"\*\*استخدم",  
> > "score":"0",  
> > "createDate":"\*\*2012-11-03T18:52:53.144Z",  
> > "author":"\*\*anonymous"  
> > },  
> > {  
> > "\_id":"\*\*509568186a435ff40c00000b",  
> > "lang":"pt",  
> > "translation":"\*\*usar",  
> > "score":"0",  
> > "createDate":"\*\*2012-11-03T18:53:12.834Z",  
> > "author":"\*\*anonymous"  
> > },  
> > {  
> > "\_id":"\*\*5095682b6a435ff40c000017",  
> > "lang":"ru",  
> > "translation":"\*\*использовать",  
> > "score":"0",  
> > "createDate":"\*\*2012-11-03T18:53:31.195Z",  
> > "author":"\*\*anonymous"  
> > },  
> > {  
> > "\_id":"\*\*5095683e6a435ff40c000027",  
> > "lang":"de",  
> > "translation":"\*\*verwenden",  
> > "score":"0",  
> > "createDate":"\*\*2012-11-03T18:53:50.812Z",  
> > "author":"\*\*anonymous"  
> > },  
> > {  
> > "\_id":"\*\*509568516a435ff40c00003b",  
> > "lang":"fr",  
> > "translation":"\*\*utiliser",  
> > "score":"0",  
> > "createDate":"\*\*2012-11-03T18:54:09.416Z",  
> > "author":"\*\*anonymous"  
> > }  
> > ],  
> > "author":"anonymous",  
> > "createDate":"2012-11-\*\*01T08:16:10.531Z"  
> > }  
> > }
> > 
> > For the term \*use(en) \*i have added 6 translations. When i added _kullanmak(tr)_ as translation of term \*use(en), \*i create a new term lieke this;
> > 
> > {
> > 
> > ...
> 
> [Message clipped]

--

---

<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, 3:06am UTC](https://discuss.elastic.co/t/searching-term-in-subdocuments-with-elasticsearch/9524/10 "2017-07-06T03:06:16Z")

</div>


