Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking for not null with completion suggester query in Elastic Search

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.

like image 998
Naibedya Kar Avatar asked Jan 29 '26 15:01

Naibedya Kar


1 Answers

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.

like image 163
Alexey Prudnikov Avatar answered Feb 03 '26 08:02

Alexey Prudnikov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!