I've implemented Trigram Similarity search with annotate which gives me exactly what I want in terms of results; but I have 220,000 records in my database and the search takes 5+ seconds per query which is too long.
The search is with 3 columns and one of those invokes a join. How do I add an index or the equivalent SearchVectorField but for Trigram to speed up my query?
See current code below:
trig_vector = (
TrigramSimilarity("make__name", text)
+ TrigramSimilarity("model", text)
+ TrigramSimilarity("specification", text)
)
query_set = (
cls.objects.annotate(similarity=trig_vector)
.filter(similarity__gt=0.5)
.order_by("-similarity")
)
I have tried making my own index from various posts but I'm not too familiar with index's and each one I've implemented hasn't has an effect on the query time.