Uden klassekoden er det svært at finde den bedre måde at optimere din forespørgsel på.
Du kan tilføje en Gin eller Gist indeks for at fremskynde trigram-ligheden.
Du kan bygge en annotering med SearchVector som nedenfor:
from django.contrib.postgres.aggregates import StringAgg
from django.contrib.postgres.search import SearchQuery, SearchVector
search_vectors = (
SearchVector('vision_expertise') +
SearchVector('bio_description') +
SearchVector(StringAgg('experiences__description', delimiter=' ')) +
SearchVector(StringAgg('educations__description', delimiter=' ')) +
SearchVector(StringAgg('publications__description', delimiter=' '))
)
Profile.objects.annotate(
search=search_vectors
).filter(
Q(search=SearchQuery(search_term)) |
Q(first_name__trigram_similar=search_term) |
Q(last_name__trigram_similar=search_term) |
Q(educations__degree__trigram_similar=search_term) |
Q(educations__field_of_study__trigram_similar=search_term) |
Q(educations__school__trigram_similar=search_term) |
Q(experiences__title__trigram_similar=search_term) |
Q(experiences__company__trigram_similar=search_term) |
Q(publications__title__trigram_similar=search_term) |
Q(certification__certification_name__trigram_similar=search_term) |
Q(certification__certification_authority__trigram_similar=search_term)
)
Du kan fremskynde fuldtekstsøgningen ved at bruge en SearchVectorField
For at finde ud af om fuldtekstsøgning og trigram kan du læse artiklen, jeg skrev om emnet:
"Full-Text Search in Django med PostgreSQL"