Search and Filter on Analyzed Fields

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.

Why not using a match query instead?

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

1 Like

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'

Ha. I misread your problem. Sorry.

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

Thanks Ivan. It solved my problem.

Yes dadoonet. It helped me out.

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