In Django, one can use full text search natively when using Postgres. However, when using it with SQL Server (and django-pyodbc-azure) there is no simple way to do it (as far I know).
To do a full text search in SQL Server you use the CONTAINS(column, word)
function as described in the docs, but Django ORM contains do: LIKE '% text %'
.
I did find two alternative methods to bypass this problem. One is using RAW SQL the other is using django extra.
Snippet using django raw SQL:
sql = "SELECT id FROM table WHERE CONTAINS(text_field, 'term')"
table = Table.objects.raw(sql)
Using django extra:
where = "CONTAINS(text_field, 'term')"
table = Table.objects.extra(where=[where])
There is two problems with it:
- Raw queries are harder to mantain.
- Django docs. recommend against using the extra method.
So I want to know if there a better way to do this, using "pure" django ORM if possible.