We are storing category in our index, here is the mapping for the category
"name": {
"type": "string",
"analyzer": "autocomplete",
"search_analyzer": "standard"
}
Now, we want to do an exact search for this field, here is the query that we are trying
{
"from": "0",
"size": "15",
"query": {
"filtered": {
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "a",
"type": "cross_fields",
"fields": [
"field1^20",
"filed1^12"
]
}
},
{
"match": {
"category.name": "Arts and Culture"
}
}
]
}
},
"filter": {
"bool": {
"must": [
{
"terms": {
"address_type": [
"val1",
"val2"
]
}
},
{
"terms": {
"status": ["Active","Inactive"]
}
}
]
}
}
}
}
}
But it is not matching exactly, I don't want to change the mapping as We want to do a partial search also in some other use case.
I just want to do exact match with some modification in the query, Is it possible or is there any workaround?
Thanks for the help.
The mapping influences how your data gets stored and how you can query that field. Your category field (named name)field is configured to be used for auto-complete and not for exact-match search. So you have to change the mapping in order to support exact match-search.
However, you can do so without changing the current field, but simply by adding an additional field by converting your field to a multi-field and adding a field that supports exact-match queries. Mapping snippet:
"name": { "type": "string", "analyzer": "autocomplete", "search_analyzer": "standard", "fields": { "raw": { "type" "string", "index": "not_analyzed" } } } }
Note: I noticed that you are on an older version of Elasticsearch (the type "string" got replaced with types "text" (for full-text-search) and "keyword" (for exact match search). I tried to come up with a mapping that works with older versions of Elasticsearch and hope it works. If not you need to provide the version of Elasticsearch you are using.
After fixing the mapping, you need to ensure that the field is also populated. You can do so, by getting everything re-indexed, by executing the following request:
POST <your_index_name>/_update_by_query
In your query you would then need to search as follows (for exact-match):
"match": {
"category.name.raw": "Arts and Culture"
}
For partial searches you would continue using the category.name
-field (without suffix raw)