# Filter for embedded document works only for the first field

**URL:** <https://discuss.elastic.co/t/filter-for-embedded-document-works-only-for-the-first-field/23254>\
**Category:** Elasticsearch\
**Created:** [April 15, 2015, 2:46pm UTC](https://discuss.elastic.co/t/filter-for-embedded-document-works-only-for-the-first-field/23254 "2015-04-15T14:46:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hellboy](https://avatars.discourse-cdn.com/v4/letter/h/48db29/32.png) [@hellboy](https://discuss.elastic.co/u/hellboy)\
**Post date:** [April 15, 2015, 2:46pm UTC](https://discuss.elastic.co/t/filter-for-embedded-document-works-only-for-the-first-field/23254/1 "2015-04-15T14:46:45Z")

</div>

Document has filter sub-document with fields to be searched

{  
customField1: customFieldValue1,  
...  
// Fields to be filtered  
filter : {  
firstFieldName: firstFieldValue,  
secondFieldName: secondFieldValue,  
}  
}

I need to search for exact values within filter fields with OR - operator.

As I mentioned for exact-value searches, probably filter is the best option  
instead of a query, as a filter will be cached.

But I am not sure  
This search request filters only firstFieldValue properly

POST /index/type/\_search  
{  
"filter": {  
"bool": {  
"must": [  
{  
"term": {  
"filter.firstFieldName": "firstFieldValue"  
}  
},  
{  
"term": {  
"filter.secondFieldName": "secondFieldValue"  
}  
},  
]  
}  
}  
}

And query matching works properly:

POST /index/type/\_search  
{  
"query": {  
"match": {  
"firstFieldName": "firstFieldValue"  
}  
}  
}

why?

> 

--  
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/c43fe35b-09f3-41b1-811a-5bf4bb1d43e7%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/c43fe35b-09f3-41b1-811a-5bf4bb1d43e7%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Thierry\_Templier\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thierry_templier_2/32/765_2.png) [@Thierry\_Templier\_2](https://discuss.elastic.co/u/Thierry_Templier_2)\
**Post date:** [April 15, 2015, 3:08pm UTC](https://discuss.elastic.co/t/filter-for-embedded-document-works-only-for-the-first-field/23254/2 "2015-04-15T15:08:31Z")

</div>

Hello,

Could you give us the mapping of your document? Do you use nested fields  
(see this link

> **[Nested Type | Elasticsearch Guide \[1.5\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/1.5/mapping-nested-type.html)**

)?

If so, you should consider to use a nested query (see this link  
[Nested Query | Elasticsearch Guide [1.5] | Elastic](http://www.elastic.co/guide/en/elasticsearch/reference/1.5/query-dsl-nested-query.html)).  
..

Thierry

2015-04-15 16:46 GMT+02:00 hellboy [pbond81@gmail.com](mailto:pbond81@gmail.com):

> Document has filter sub-document with fields to be searched
> 
> {  
> customField1: customFieldValue1,  
> ...  
> // Fields to be filtered  
> filter : {  
> firstFieldName: firstFieldValue,  
> secondFieldName: secondFieldValue,  
> }  
> }
> 
> I need to search for exact values within filter fields with OR - operator.
> 
> As I mentioned for exact-value searches, probably filter is the best  
> option instead of a query, as a filter will be cached.
> 
> But I am not sure  
> This search request filters only firstFieldValue properly
> 
> POST /index/type/\_search  
> {  
> "filter": {  
> "bool": {  
> "must": [  
> {  
> "term": {  
> "filter.firstFieldName": "firstFieldValue"  
> }  
> },  
> {  
> "term": {  
> "filter.secondFieldName": "secondFieldValue"  
> }  
> },  
> ]  
> }  
> }  
> }
> 
> And query matching works properly:
> 
> POST /index/type/\_search  
> {  
> "query": {  
> "match": {  
> "firstFieldName": "firstFieldValue"  
> }  
> }  
> }
> 
> why?
> 
> > 
> 
> --  
> 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/c43fe35b-09f3-41b1-811a-5bf4bb1d43e7%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/c43fe35b-09f3-41b1-811a-5bf4bb1d43e7%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/c43fe35b-09f3-41b1-811a-5bf4bb1d43e7%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/c43fe35b-09f3-41b1-811a-5bf4bb1d43e7%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

## -- Best, Thierry

_Thierry Templier, Lead Architect_  
[ttemplier@restlet.com](mailto:ttemplier@restlet.com)

Restlet SAS, 6 rue Rose Dieng-Kuntz • 44300 Nantes • France

--  
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/CAL73BC3h9PKGK2NU2rYOep7R6AtOZAFrsAaM4c-fu-skXK5RZw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL73BC3h9PKGK2NU2rYOep7R6AtOZAFrsAaM4c-fu-skXK5RZw%40mail.gmail.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:19am UTC](https://discuss.elastic.co/t/filter-for-embedded-document-works-only-for-the-first-field/23254/3 "2017-07-06T00:19:33Z")

</div>


