elasticsearchmatch-phrase

Elasticsearch query for multi-word search suggestions


I'm currently using this ES query for search suggestion from ES (using edge n grams)

var terms = query.split(' '),
    baseTerms = terms.length === 1 ? '' : terms.slice(0, -1).join(' ') + ' ',
    lastTerm = terms[terms.length - 1].toLowerCase();

"query": {
      "simple_query_string": {
        "fields": ['title.autocomplete'], //title.basic
        "query": baseTerms + '(' + lastTerm + '|' + lastTerm + '*)',
        "default_operator": "and"
      }
    }

Which works but only for single words. As I type letters, single word suggestions appear, but I'm trying to get multi-word suggestions, separated by spaces (phrase suggestions). Is there a better ES query to use so that I can get some phrase suggestions?


Solution

  • Use match_phrase_prefix Query. It will fetch you phrase suggestions: ES Query is like:

     {
     "match_phrase_prefix" : {
        "message" : {
            "query" : "quick brown f"
         }
       }
     }