Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Full Text Search Not Matching Partial Words

I'm using Django Full Text search to search across multiple fields but have an issue when searching using partial strings.

Lets say we have a report object with the name 'Sample Report'.

vector = SearchVector('name') + SearchVector('author__username')

search = SearchQuery('Sa')

Report.objects.exclude(visible=False).annotate(search=vector).filter(search=search)

The following QuerySet is empty but if I include the full word 'Sample' then the report will appear in the QuerySet.

Is there anyway to use icontains or prefixing with django full text search?

like image 201
Bryden Fogelman Avatar asked Aug 04 '17 23:08

Bryden Fogelman


1 Answers

This is working on Django 1.11:

tools = Tool.objects.annotate(
    search=SearchVector('name', 'description', 'expert__user__username'),
).filter(search__icontains=form.cleaned_data['query_string'])

Note the icontains in the filter.

like image 198
santiagopim Avatar answered Oct 16 '22 22:10

santiagopim