Created
July 18, 2019 10:06
-
-
Save vgk77/193e15f08994b69fddaa7bb382d461f5 to your computer and use it in GitHub Desktop.
Example with trigramm similarity function #django #python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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