[Postgres' part] The Postgres manual mentions this only briefly ( https://www.postgresql.org/docs/current/static/textsearch-controls.html#TEXTSEARCH-PARSING-QUERIES), but yes, it is possible, if you just need prefix matching:
test=# select to_tsvector('abcd') @@ to_tsquery('ab:*');
?column?
----------
t
(1 row)
test=# select to_tsvector('abcd') @@ to_tsquery('ac:*');
?column?
----------
f
(1 row)
And such query will utilize GIN index (I assume you have one).
[Django's part] I'm not Django user, so I made quick research and found that, unfortunately, Django uses plainto_tsquery()
function, not to_tsquery()
: https://docs.djangoproject.com/en/1.11/_modules/django/contrib/postgres/search/#SearchQuery
plainto_tsquery()
made for simplicity, when you use just plain text as an input – so it doesn't support advanced queries:
test=# select to_tsvector('abcd') @@ plainto_tsquery('ab:*');
?column?
----------
f
(1 row)
test=# select to_tsvector('abcd') @@ plainto_tsquery('ac:*');
?column?
----------
f
(1 row)
So in this case, I'd recommend you using plain SQL with to_tsquery()
. But you need to be sure you filtered out all special chars (like &
or |
) from your text input, otherwise to_tsquery()
will produce wrong results or even error. Or if you can, extend django.contrib.postgres.search with the ability to work with to_tsquery()
(this would be great contribution, btw).
Alternatives are: