3

I have an existing query that is providing suggestions for postcode having the query as below (I have hard coded it with postcode as T0L)

 "suggest":{
    "suggestions":{
      "text":"T0L", 
      "completion":{
        "field": "postcode.suggest"
      }
    }
  }

This works fine, but it searches for some results where the city contains null values. So I need to filter the addresses where the city is not null.

So I followed the solution on this and prepared the query like this.

{
 "query": {
   "constant_score": {
     "filter": {
       "exists": {
         "field": "city"
       }
     }
   }
 }, 
  "suggest":{
    "suggestions":{
      "text":"T0L", 
      "completion":{
        "field": "postcode.suggest"
      }
    }
  }
}

But unfortunately this is not giving the required addresses where the postcode contains T0L, rather I am getting results where postcode starts with A1X. So I believe it is querying for all the addresses where the city is present and ignoring the completion suggester query. Can you please let me know where is the mistake. Or may be how to write it correctly.

Naibedya Kar
  • 305
  • 2
  • 9

1 Answers1

2

There is no way to filter out suggestions at query time, because completion suggester use FST (special in-memory data structure that built at index time) for lightning-fast search.

But you can change your mapping and add context for your suggester. The basic idea of context that it also filled at index time along with completion field and therefore can be used at query time with suggest query.

Alexey Prudnikov
  • 1,083
  • 12
  • 11