Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Full-Text Search with multiple words

This isn't really a complex problem (to my knowledge).

I know in MongoDB you can feed in a string and it automatically tokenizes and performs full-text search using that string as a query.

However, in Django, I have yet to find similar functionality, and all of the examples I've seen have done something along the lines of:

from django.contrib.postgres.search import SearchQuery
query = SearchQuery('foo')

Is the reason people only use one word because SearchQuery can only use one word?

What I want to know is how to perform full-text search with multiple words. Is it as easy as doing

from django.contrib.postgres.search import SearchQuery
query = SearchQuery('foo and also bar')

? Or does it need to be more complicated than that?

like image 543
Quontas Avatar asked Oct 17 '22 17:10

Quontas


1 Answers

To perform Full Text Search with Django you have to combine using GiN index with SearchVector.
Here is full working example what I used somewhere. It works also on 2+ words in a query and searches them in 3 fields.

like image 59
Chiefir Avatar answered Oct 20 '22 19:10

Chiefir