# Searching for fields in main documents OR nested documents

**URL:** <https://discuss.elastic.co/t/searching-for-fields-in-main-documents-or-nested-documents/20214>\
**Category:** Elasticsearch\
**Created:** [October 13, 2014, 10:24am UTC](https://discuss.elastic.co/t/searching-for-fields-in-main-documents-or-nested-documents/20214 "2014-10-13T10:24:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Werner](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@Michael\_Werner](https://discuss.elastic.co/u/Michael_Werner)\
**Post date:** [October 13, 2014, 10:24am UTC](https://discuss.elastic.co/t/searching-for-fields-in-main-documents-or-nested-documents/20214/1 "2014-10-13T10:24:42Z")

</div>

I am pretty new to ES and have a problem that I could not yet solve using  
the docuemntation, so any help is appreciated...

I have these douments in my index:

DocType1  
{  
"id" : 1,  
"name" : "ABC",  
"year" : 2014,  
"page" : 123,  
"linked\_docs" : {  
{  
{  
"id" : 2,  
"name" : "DEF",  
"year" : 2013,  
"page" : 111  
},  
{  
"id" : 3,  
"name" : "GHI",  
"year" : 2012,  
"page" : 222  
},  
}  
}  
},  
{  
"id": 12345,  
"name" : "XYZ",  
"year" : 2014,  
"page" : 999,  
"linked\_docs" : { }  
}

DocType2  
{  
"id" : 98765,  
"user" : "User 1",  
"type" : "Test",  
"linked\_docs" : {  
{  
{  
"id" : 1,  
"name" : "ABC",  
"year" : 2014,  
"page" : 123  
},  
{  
"id" : 2,  
"name" : "DEF",  
"year" : 2013,  
"page" : 111  
},  
}  
}  
},  
{  
"id" : 4321,  
"user" : "User 2",  
"type" : "Test 2",  
"linked\_docs" : { }  
}

Now I want to get all documents matching name = ABC, year = 2014, page =  
123 either in the main document \>\>\> OR \<\<\< in the linked\_docs.

So with the data aboce the search should deliver the following documents:

- DocType1, id: 1 (matches in main document)
- DocType2, id: 98765 (matches in linked docs)

I tried with nested query but I am not getting the result as above.

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/7a92ccfd-3df4-47a4-93bf-dd08a7eb8fb9%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/7a92ccfd-3df4-47a4-93bf-dd08a7eb8fb9%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Ramy](https://avatars.discourse-cdn.com/v4/letter/r/8491ac/32.png) [@Ramy](https://discuss.elastic.co/u/Ramy)\
**Post date:** [October 13, 2014, 10:34am UTC](https://discuss.elastic.co/t/searching-for-fields-in-main-documents-or-nested-documents/20214/2 "2014-10-13T10:34:41Z")

</div>

Hi,  
This query should works for you:  
{  
"query": {  
"bool": {  
"should": [  
{  
"query\_string": {  
"query": "ABC 2014 123",  
"default\_operator": "AND",  
"fields": [  
"name",  
"year",  
"page"  
]  
}  
},  
{  
"nested": {  
"path": "linked\_docs",  
"query": {  
"query\_string": {  
"query": "ABC 2014 123",  
"default\_operator": "AND",  
"fields": [  
"linked\_docs.name",  
"linked\_docs.year",  
"linked\_docs.page"  
]  
}  
}  
}  
}  
]  
}  
}  
}

Cheers, Ramy

Am Montag, 13. Oktober 2014 12:24:42 UTC+2 schrieb Michael Werner:

> I am pretty new to ES and have a problem that I could not yet solve using  
> the docuemntation, so any help is appreciated...
> 
> I have these douments in my index:
> 
> DocType1  
> {  
> "id" : 1,  
> "name" : "ABC",  
> "year" : 2014,  
> "page" : 123,  
> "linked\_docs" : {  
> {  
> {  
> "id" : 2,  
> "name" : "DEF",  
> "year" : 2013,  
> "page" : 111  
> },  
> {  
> "id" : 3,  
> "name" : "GHI",  
> "year" : 2012,  
> "page" : 222  
> },  
> }  
> }  
> },  
> {  
> "id": 12345,  
> "name" : "XYZ",  
> "year" : 2014,  
> "page" : 999,  
> "linked\_docs" : { }  
> }
> 
> DocType2  
> {  
> "id" : 98765,  
> "user" : "User 1",  
> "type" : "Test",  
> "linked\_docs" : {  
> {  
> {  
> "id" : 1,  
> "name" : "ABC",  
> "year" : 2014,  
> "page" : 123  
> },  
> {  
> "id" : 2,  
> "name" : "DEF",  
> "year" : 2013,  
> "page" : 111  
> },  
> }  
> }  
> },  
> {  
> "id" : 4321,  
> "user" : "User 2",  
> "type" : "Test 2",  
> "linked\_docs" : { }  
> }
> 
> Now I want to get all documents matching name = ABC, year = 2014, page =  
> 123 either in the main document \>\>\> OR \<\<\< in the linked\_docs.
> 
> So with the data aboce the search should deliver the following documents:
> 
> - DocType1, id: 1 (matches in main document)
> - DocType2, id: 98765 (matches in linked docs)
> 
> I tried with nested query but I am not getting the result as above.

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/a701b8f1-92ab-4186-9c98-d4995e5f653b%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/a701b8f1-92ab-4186-9c98-d4995e5f653b%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Michael\_Werner](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@Michael\_Werner](https://discuss.elastic.co/u/Michael_Werner)\
**Post date:** [October 13, 2014, 11:16am UTC](https://discuss.elastic.co/t/searching-for-fields-in-main-documents-or-nested-documents/20214/3 "2014-10-13T11:16:22Z")

</div>

You're great!

I have been trying for several hours... as far as I can tell right now,  
your solution is delivering correct results.

Thank you very much!

Am Montag, 13. Oktober 2014 12:24:42 UTC+2 schrieb Michael Werner:

> I am pretty new to ES and have a problem that I could not yet solve using  
> the docuemntation, so any help is appreciated...
> 
> I have these douments in my index:
> 
> DocType1  
> {  
> "id" : 1,  
> "name" : "ABC",  
> "year" : 2014,  
> "page" : 123,  
> "linked\_docs" : {  
> {  
> {  
> "id" : 2,  
> "name" : "DEF",  
> "year" : 2013,  
> "page" : 111  
> },  
> {  
> "id" : 3,  
> "name" : "GHI",  
> "year" : 2012,  
> "page" : 222  
> },  
> }  
> }  
> },  
> {  
> "id": 12345,  
> "name" : "XYZ",  
> "year" : 2014,  
> "page" : 999,  
> "linked\_docs" : { }  
> }
> 
> DocType2  
> {  
> "id" : 98765,  
> "user" : "User 1",  
> "type" : "Test",  
> "linked\_docs" : {  
> {  
> {  
> "id" : 1,  
> "name" : "ABC",  
> "year" : 2014,  
> "page" : 123  
> },  
> {  
> "id" : 2,  
> "name" : "DEF",  
> "year" : 2013,  
> "page" : 111  
> },  
> }  
> }  
> },  
> {  
> "id" : 4321,  
> "user" : "User 2",  
> "type" : "Test 2",  
> "linked\_docs" : { }  
> }
> 
> Now I want to get all documents matching name = ABC, year = 2014, page =  
> 123 either in the main document \>\>\> OR \<\<\< in the linked\_docs.
> 
> So with the data aboce the search should deliver the following documents:
> 
> - DocType1, id: 1 (matches in main document)
> - DocType2, id: 98765 (matches in linked docs)
> 
> I tried with nested query but I am not getting the result as above.

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/9c99d4b9-b3f3-4242-85f6-d387a1f2a231%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/9c99d4b9-b3f3-4242-85f6-d387a1f2a231%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Michael\_Werner](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@Michael\_Werner](https://discuss.elastic.co/u/Michael_Werner)\
**Post date:** [October 13, 2014, 11:18am UTC](https://discuss.elastic.co/t/searching-for-fields-in-main-documents-or-nested-documents/20214/4 "2014-10-13T11:18:43Z")

</div>

You're great!

I have been trying for several hours without success... as far as I can  
tell from a few tests so far your solution is delivering the correct  
results.

Thank you very much!

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/429a8757-2d70-4653-a4ec-09a873da646f%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/429a8757-2d70-4653-a4ec-09a873da646f%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 12:56am UTC](https://discuss.elastic.co/t/searching-for-fields-in-main-documents-or-nested-documents/20214/5 "2017-07-06T00:56:35Z")

</div>


