Skip to content

Instantly share code, notes, and snippets.

@vgk77
Created July 18, 2019 10:06
Show Gist options
  • Select an option

  • Save vgk77/193e15f08994b69fddaa7bb382d461f5 to your computer and use it in GitHub Desktop.

Select an option

Save vgk77/193e15f08994b69fddaa7bb382d461f5 to your computer and use it in GitHub Desktop.
Example with trigramm similarity function #django #python
from django.contrib.postgres.search import TrigramSimilarity
def find_product_using_trigram_similarity(
pricelist_product: str, min_similarity=0.3
) -> Optional[Product]:
"""Find the most similar product to the pricelist product
using trigram similarity search.
"""
return (
Product.objects.annotate(
similarity=TrigramSimilarity("name", pricelist_product)
)
.filter(similarity__gt=min_similarity)
.order_by("-similarity")
.first()
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment