13

For search I want to use the search vector from PostgreSQL;

I wrote a custom query method:

  def search(self, text):

        search_vectors = (
            SearchVector('name', weight='A', config='english') +
            SearchVector('short_description', weight='B', config='english') +
            SearchVector('description', weight='C', config='english')
        )
        search_query = SearchQuery(text)
        search_rank = SearchRank(search_vectors, search_query, weights=[0.2, 0.4, 0.6, 1])

        return self.annotate(rank=search_rank).filter(rank__gte=0.2).order_by('-rank').

There are 2 'issues'. For example for the name 'Eric', if I search:

  • 'eric' I get the correct results
  • 'eric john' I get not result
  • 'eri' I get no results

I kind of understand why it fails, but I don't how to implement the fixes.

M.javid
  • 6,387
  • 3
  • 41
  • 56
user3541631
  • 3,686
  • 8
  • 48
  • 115

1 Answers1

1

If I understand correctly, you want to have a partial search Well, for this, it is enough for you to paste from * to the end of the query and send it and you will find the results.

.
.    
search_query = SearchQuery(text + '*')
.
.

Of course, if you want to include the user's query, you can put query between two * as below and send it.

.
.
search_query = SearchQuery('*' + text + '*')
.
.

To know how it works read the following answer https://stackoverflow.com/a/44382089/16829292