I have a situation where I need to do elastic search based on multi-match.
case 1: phrase search for keywords field. should return the results on complete phrase match only(Should not split the phrase). case 2: for remaining fields 1 keyword match must return the results.
I combined the multi-match and terms queries using should. as below.
For example : if I search with "Phrase search in multiple fields"
First it should search as complete phrase in keywords field and later it can search in multi-match for other fields. Note: If I search with single word like "multiple" or "field" it should not return any results from keywords.
"search": {
"method": "POST",
"param": {
"query": {
"bool": {
"should": [
{
"terms": {
"keywords": [
"Phrase search in multiple fields",
" Phrase search in multiple fields"
]
}
},
{
"multi_match": {
"query": "Phrase search in multiple fields",
"fields": {
"title": "title",
"metaTitle": "title",
"href": "Path",
},
"fuzziness": "AUTO"
}
}
]
}
}
}
case 1: phrase search for keywords field. should return the results on complete phrase match only(Should not split the phrase)
Use the
keywordfield type.Should not split the phrase => if you use the
keywordfield type which mean is no analyzer for text it won't split the phrases.case 2: for remaining fields 1 keyword match must return the results.
Standard analyzer will
Use the
textfield type.1 keyword match must return the results => standard (default) analyzer tokenize the phrase with whitespaces (and some special characters) and split into the words.
I'm sharing some examples to explain deeply, hope it helps.