Help needed with search as you type

My goal is to create a search engine (elasticsearch 7.x) that suggests you the correct searches to input, a sort of what amazon does, based on previous searches (machine learning). My fields to index are the following:

  • Keywords = search phrases
  • occurrences = number of times this search was performed
  • total = number of results (it only to exclude searches that produced no results)

INDEX
{
"mappings": {
"properties": {
"keywords": {
"type": "search_as_you_type"
},
"occurences":{
"type": "integer"
}
}
}
}

What I would like to achieve is a search as you type "starts with" but when I apply the query:

{
"query": {
"match_phrase_prefix": {
"keywords": "fill"
}
}
}

I get not only keywords starting wiht fill (such as "filler") But also other that contain the query, such as "real filler" or any other phrase that contains "fill". What I would like to ahcieve is returning only the phrases starting with the query term.

The next step would be to boost results that have a higher value of occurences

Any hint?
Thank you

I think the completion suggester may be a better fit for what you're trying to do. It will only offer suggestions that start with the prefix you're requesting. It also allows you to specify a weight for every suggestion, which will be used for ranking the suggestions. In your case the weight could be equal to the number of occurrences.

Given an index mapping like this:

PUT my_index
{
  "mappings": {
    "properties": {
      "keywords": {
        "type": "completion"
      }
    }
  }
}

You can index a few documents like below. The weight here would be populated with the value of occurrences:

PUT my_index/_doc/1
{
  "keywords": {
    "input": "filler1",
    "weight": 1
  }
}

PUT my_index/_doc/2
{
  "keywords": {
    "input": "filler2",
    "weight": 2
  }
}

PUT my_index/_doc/3
{
  "keywords": {
    "input": "real filler",
    "weight": 3
  }
}

If you now send the following suggest request to Elasticsearch, it will only return documents 1 and 2, and it will rank them based on the provided weight:

POST my_index/_search
{
  "suggest": {
    "my_suggestion": {
      "prefix": "fill",
      "completion": {
        "field": "keywords"
      }
    }
  }
}

Got it!
I tested it and it works very fine. Thank you Abdon, I appreciate your help

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