2015-06-06 00:37:48 +08:00
|
|
|
from django.test import modify_settings
|
|
|
|
|
|
|
|
from . import PostgreSQLTestCase
|
|
|
|
from .models import CharFieldModel, TextFieldModel
|
|
|
|
|
2019-11-19 21:59:06 +08:00
|
|
|
try:
|
2020-07-24 14:25:47 +08:00
|
|
|
from django.contrib.postgres.search import (
|
2021-09-15 18:57:49 +08:00
|
|
|
TrigramDistance,
|
|
|
|
TrigramSimilarity,
|
2022-06-17 15:44:03 +08:00
|
|
|
TrigramStrictWordDistance,
|
|
|
|
TrigramStrictWordSimilarity,
|
2021-09-15 18:57:49 +08:00
|
|
|
TrigramWordDistance,
|
|
|
|
TrigramWordSimilarity,
|
2020-07-24 14:25:47 +08:00
|
|
|
)
|
2019-11-19 21:59:06 +08:00
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
2015-06-06 00:37:48 +08:00
|
|
|
|
|
|
|
@modify_settings(INSTALLED_APPS={"append": "django.contrib.postgres"})
|
|
|
|
class TrigramTest(PostgreSQLTestCase):
|
|
|
|
Model = CharFieldModel
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
cls.Model.objects.bulk_create(
|
|
|
|
[
|
|
|
|
cls.Model(field="Matthew"),
|
|
|
|
cls.Model(field="Cat sat on mat."),
|
|
|
|
cls.Model(field="Dog sat on rug."),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_trigram_search(self):
|
2022-09-24 18:29:58 +08:00
|
|
|
self.assertQuerySetEqual(
|
2015-06-06 00:37:48 +08:00
|
|
|
self.Model.objects.filter(field__trigram_similar="Mathew"),
|
|
|
|
["Matthew"],
|
|
|
|
transform=lambda instance: instance.field,
|
|
|
|
)
|
|
|
|
|
2021-09-15 18:57:49 +08:00
|
|
|
def test_trigram_word_search(self):
|
|
|
|
obj = self.Model.objects.create(
|
|
|
|
field="Gumby rides on the path of Middlesbrough",
|
|
|
|
)
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
self.Model.objects.filter(field__trigram_word_similar="Middlesborough"),
|
|
|
|
[obj],
|
|
|
|
)
|
2022-06-17 15:44:03 +08:00
|
|
|
self.assertSequenceEqual(
|
|
|
|
self.Model.objects.filter(field__trigram_word_similar="Middle"),
|
|
|
|
[obj],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_trigram_strict_word_search_matched(self):
|
|
|
|
obj = self.Model.objects.create(
|
|
|
|
field="Gumby rides on the path of Middlesbrough",
|
|
|
|
)
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
self.Model.objects.filter(
|
|
|
|
field__trigram_strict_word_similar="Middlesborough"
|
|
|
|
),
|
|
|
|
[obj],
|
|
|
|
)
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
self.Model.objects.filter(field__trigram_strict_word_similar="Middle"),
|
|
|
|
[],
|
|
|
|
)
|
2021-09-15 18:57:49 +08:00
|
|
|
|
2015-06-06 00:37:48 +08:00
|
|
|
def test_trigram_similarity(self):
|
|
|
|
search = "Bat sat on cat."
|
2022-05-19 15:26:48 +08:00
|
|
|
# Round result of similarity because PostgreSQL uses greater precision.
|
2022-09-24 18:29:58 +08:00
|
|
|
self.assertQuerySetEqual(
|
2015-06-06 00:37:48 +08:00
|
|
|
self.Model.objects.filter(
|
|
|
|
field__trigram_similar=search,
|
|
|
|
)
|
|
|
|
.annotate(similarity=TrigramSimilarity("field", search))
|
|
|
|
.order_by("-similarity"),
|
|
|
|
[("Cat sat on mat.", 0.625), ("Dog sat on rug.", 0.333333)],
|
2019-11-09 04:20:13 +08:00
|
|
|
transform=lambda instance: (instance.field, round(instance.similarity, 6)),
|
2015-06-06 00:37:48 +08:00
|
|
|
ordered=True,
|
|
|
|
)
|
|
|
|
|
2021-09-15 18:57:49 +08:00
|
|
|
def test_trigram_word_similarity(self):
|
|
|
|
search = "mat"
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
self.Model.objects.filter(
|
|
|
|
field__trigram_word_similar=search,
|
|
|
|
)
|
|
|
|
.annotate(
|
|
|
|
word_similarity=TrigramWordSimilarity(search, "field"),
|
|
|
|
)
|
|
|
|
.values("field", "word_similarity")
|
|
|
|
.order_by("-word_similarity"),
|
|
|
|
[
|
|
|
|
{"field": "Cat sat on mat.", "word_similarity": 1.0},
|
|
|
|
{"field": "Matthew", "word_similarity": 0.75},
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2022-06-17 15:44:03 +08:00
|
|
|
def test_trigram_strict_word_similarity(self):
|
|
|
|
search = "matt"
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
self.Model.objects.filter(field__trigram_word_similar=search)
|
|
|
|
.annotate(word_similarity=TrigramStrictWordSimilarity(search, "field"))
|
|
|
|
.values("field", "word_similarity")
|
|
|
|
.order_by("-word_similarity"),
|
|
|
|
[
|
|
|
|
{"field": "Cat sat on mat.", "word_similarity": 0.5},
|
|
|
|
{"field": "Matthew", "word_similarity": 0.44444445},
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2015-06-06 00:37:48 +08:00
|
|
|
def test_trigram_similarity_alternate(self):
|
2022-05-19 15:26:48 +08:00
|
|
|
# Round result of distance because PostgreSQL uses greater precision.
|
2022-09-24 18:29:58 +08:00
|
|
|
self.assertQuerySetEqual(
|
2015-06-06 00:37:48 +08:00
|
|
|
self.Model.objects.annotate(
|
|
|
|
distance=TrigramDistance("field", "Bat sat on cat."),
|
|
|
|
)
|
|
|
|
.filter(distance__lte=0.7)
|
|
|
|
.order_by("distance"),
|
|
|
|
[("Cat sat on mat.", 0.375), ("Dog sat on rug.", 0.666667)],
|
2019-11-09 04:20:13 +08:00
|
|
|
transform=lambda instance: (instance.field, round(instance.distance, 6)),
|
2015-06-06 00:37:48 +08:00
|
|
|
ordered=True,
|
|
|
|
)
|
|
|
|
|
2021-09-15 18:57:49 +08:00
|
|
|
def test_trigram_word_similarity_alternate(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
self.Model.objects.annotate(
|
|
|
|
word_distance=TrigramWordDistance("mat", "field"),
|
|
|
|
)
|
|
|
|
.filter(
|
|
|
|
word_distance__lte=0.7,
|
|
|
|
)
|
|
|
|
.values("field", "word_distance")
|
|
|
|
.order_by("word_distance"),
|
|
|
|
[
|
|
|
|
{"field": "Cat sat on mat.", "word_distance": 0},
|
|
|
|
{"field": "Matthew", "word_distance": 0.25},
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2022-06-17 15:44:03 +08:00
|
|
|
def test_trigram_strict_word_distance(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
self.Model.objects.annotate(
|
|
|
|
word_distance=TrigramStrictWordDistance("matt", "field"),
|
|
|
|
)
|
|
|
|
.filter(word_distance__lte=0.7)
|
|
|
|
.values("field", "word_distance")
|
|
|
|
.order_by("word_distance"),
|
|
|
|
[
|
|
|
|
{"field": "Cat sat on mat.", "word_distance": 0.5},
|
|
|
|
{"field": "Matthew", "word_distance": 0.5555556},
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2015-06-06 00:37:48 +08:00
|
|
|
|
|
|
|
class TrigramTextFieldTest(TrigramTest):
|
|
|
|
"""
|
|
|
|
TextField has the same behavior as CharField regarding trigram lookups.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2015-06-06 00:37:48 +08:00
|
|
|
Model = TextFieldModel
|