Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - Full text search - Wildcard

Is it possible to use wildcards in Django Full text search ?

https://docs.djangoproject.com/en/1.11/ref/contrib/postgres/search/

        post = request.POST.get('search')
        query = SearchQuery(post)
        vector = SearchVector('headline', weight='A') + SearchVector('content', weight='B')
        rank = SearchRank(vector, query, weights=[0.1,0.2])
        data = wiki_entry.objects.annotate(rank=SearchRank(vector,query)).filter(rank__gte=0.1).order_by('-rank')

At the moment it only matches on full words.

Characters like * % | & have no effect.

Or do i have to go back to icontains ?

https://docs.djangoproject.com/en/1.11/ref/models/querysets/#icontains

Any help is appreciated

like image 663
fuser60596 Avatar asked Jul 14 '17 19:07

fuser60596


People also ask

Where can I find the source code for search in Django?

I also have a Django Chat podcast episode all about search in discussion with Django Fellow Carlton Gibson. Complete source code can be found on Github. To start let's create a new Django project ( see here if you need help with this ). I've done so in a directory called search.

What is queryset in Django and how to use it?

In Django a QuerySet is used to filter the results from a database model. Currently our City model is outputting all its contents. Eventually we want to limit the search results page to filter the results outputted based upon a search query.

Is it possible to save the search vector in Django?

Because it takes time to convert strings to search vectors, it would also be better to save these search vectors in the database. Django 1.10 introduced the SearchVectorField model field to save the search vector in this column, which will be converted to TSVECTOR which is a PostgreSQL built-in text search type.

How do I display a populated database on my Django website?

We have a populated database but there are still a few steps before it can be displayed on our Django website. Ultimately we only need a homepage and search results page. Each page requires a dedicated view, url, and template. The order in which we create these doesn't really matter; all must be present for the site to work as intended.


1 Answers

I extend the django SearchQuery class and override plainto_tsquery with to_tsquery. Did some simple tests, it works. I will get back here if I find cases where this causes problems.

from django.contrib.postgres.search import SearchQuery

class MySearchQuery(SearchQuery):
    def as_sql(self, compiler, connection):
        params = [self.value]
        if self.config:
            config_sql, config_params = compiler.compile(self.config)
            template = 'to_tsquery({}::regconfig, %s)'.format(config_sql)
            params = config_params + [self.value]
        else:
            template = 'to_tsquery(%s)'
        if self.invert:
            template = '!!({})'.format(template)
        return template, params

Now I can do something like query = MySearchQuery('whatever:*')

like image 93
user1383029 Avatar answered Nov 11 '22 16:11

user1383029