# Search and Filter on Analyzed Fields

**URL:** <https://discuss.elastic.co/t/search-and-filter-on-analyzed-fields/115090>\
**Category:** Elasticsearch\
**Created:** [January 11, 2018, 1:04pm UTC](https://discuss.elastic.co/t/search-and-filter-on-analyzed-fields/115090 "2018-01-11T13:04:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinaygarg30](https://avatars.discourse-cdn.com/v4/letter/v/6bbea6/32.png) [@vinaygarg30](https://discuss.elastic.co/u/vinaygarg30)\
**Post date:** [January 11, 2018, 1:04pm UTC](https://discuss.elastic.co/t/search-and-filter-on-analyzed-fields/115090/1 "2018-01-11T13:04:31Z")

</div>

We need to search as well as apply filters on the text fields (which are analysed and they are required to be so in our application). But, while applying filters on analysed text returns results which do not match the filter (because the search happens on analysed text) e.g.  
GET accounts/account/\_search  
{  
"query": {  
"bool" : {  
"must" : [  
{  
"bool" : {  
"should" : [  
{"match\_phrase" : {"region": "NA - Central"}},  
{"match\_phrase" : {"region": "NA - West"}}  
]  
}  
},  
{  
"bool" : {  
"should" : [  
{"match\_phrase" : {"industryType": "Police"}},  
{"match\_phrase" : {"industryType": "Government"}}]  
}  
}  
]  
}  
}  
}  
Now in industryType field, few possible values are ['National Government Security', 'Government' , 'Police'].  
So applying a match\_phrase for industry:Government return results for both 'National Government Security' & 'Government'.  
But the documents with exact industryType - 'Government' are desired in the results.  
Please suggest the solution which I can follow.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 11, 2018, 2:55pm UTC](https://discuss.elastic.co/t/search-and-filter-on-analyzed-fields/115090/2 "2018-01-11T14:55:02Z")

</div>

Why not using a match query instead?

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [January 11, 2018, 3:32pm UTC](https://discuss.elastic.co/t/search-and-filter-on-analyzed-fields/115090/3 "2018-01-11T15:32:59Z")

</div>

You can also use multi-fields on the same source, so that you have the  
original instance to execute free text search, and another strict for  
filtering (assuming you have the exact text) and aggregations. This method  
will also be more performant since non-analyzed text uses doc\_values.

[https://www.elastic.co/guide/en/elasticsearch/reference/current/multi-fields.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/multi-fields.html)

---

<div class="post-metadata">

**Author:** ![vinaygarg30](https://avatars.discourse-cdn.com/v4/letter/v/6bbea6/32.png) [@vinaygarg30](https://discuss.elastic.co/u/vinaygarg30)\
**Post date:** [January 12, 2018, 6:04am UTC](https://discuss.elastic.co/t/search-and-filter-on-analyzed-fields/115090/4 "2018-01-12T06:04:46Z")

</div>

I tried using the match query, but still the same issue persists as it is not giving exact results for industryType:Government. It gives all results wherever the industryType contains substring 'Government' which does not satisfy my requirement.

Moreover, in the should clause, the default operator is OR. But for the values like 'NA - West', we need to give the operator as 'AND' in the match query as the text fields are analyzed and we need to do 'AND' operation to search exact text field 'NA - West'

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 12, 2018, 6:32am UTC](https://discuss.elastic.co/t/search-and-filter-on-analyzed-fields/115090/5 "2018-01-12T06:32:58Z")

</div>

Ha. I misread your problem. Sorry.

I agree with what @Ivan said. Add a `keyword` subfield and filter on it then.

---

<div class="post-metadata">

**Author:** ![vinaygarg30](https://avatars.discourse-cdn.com/v4/letter/v/6bbea6/32.png) [@vinaygarg30](https://discuss.elastic.co/u/vinaygarg30)\
**Post date:** [January 15, 2018, 11:09am UTC](https://discuss.elastic.co/t/search-and-filter-on-analyzed-fields/115090/6 "2018-01-15T11:09:35Z")

</div>

Thanks Ivan. It solved my problem.

---

<div class="post-metadata">

**Author:** ![vinaygarg30](https://avatars.discourse-cdn.com/v4/letter/v/6bbea6/32.png) [@vinaygarg30](https://discuss.elastic.co/u/vinaygarg30)\
**Post date:** [January 15, 2018, 11:09am UTC](https://discuss.elastic.co/t/search-and-filter-on-analyzed-fields/115090/7 "2018-01-15T11:09:57Z")

</div>

Yes dadoonet. It helped me 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:** [February 12, 2018, 11:10am UTC](https://discuss.elastic.co/t/search-and-filter-on-analyzed-fields/115090/8 "2018-02-12T11:10:15Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
