added multi-language support to FTS
This commit is contained in:
parent
556f74cc2b
commit
4e94e26b7c
@ -3,22 +3,36 @@ from django.contrib.postgres.search import (
|
|||||||
SearchQuery, SearchRank, SearchVector, TrigramSimilarity,
|
SearchQuery, SearchRank, SearchVector, TrigramSimilarity,
|
||||||
)
|
)
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from django.utils import translation
|
||||||
|
|
||||||
|
DICTIONARY = {
|
||||||
|
# TODO find custom dictionaries - maybe from here https://www.postgresql.org/message-id/CAF4Au4x6X_wSXFwsQYE8q5o0aQZANrvYjZJ8uOnsiHDnOVPPEg%40mail.gmail.com
|
||||||
|
# 'hy': 'Armenian',
|
||||||
|
# 'ca': 'Catalan',
|
||||||
|
# 'cs': 'Czech',
|
||||||
|
'nl': 'dutch',
|
||||||
|
'en': 'english',
|
||||||
|
'fr': 'french',
|
||||||
|
'de': 'german',
|
||||||
|
'it': 'italian',
|
||||||
|
# 'lv': 'Latvian',
|
||||||
|
'es': 'spanish',
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
# TODO add search highlighting
|
# TODO add search highlighting
|
||||||
# TODO add language support
|
# TODO add language support
|
||||||
# TODO add schedule index rebuild
|
# TODO add schedule index rebuild
|
||||||
# TODO add admin function to rebuild index
|
|
||||||
class RecipeSearchManager(models.Manager):
|
class RecipeSearchManager(models.Manager):
|
||||||
|
|
||||||
def search(self, search_text, space):
|
def search(self, search_text, space):
|
||||||
|
language = DICTIONARY.get(translation.get_language(), 'simple')
|
||||||
search_query = SearchQuery(
|
search_query = SearchQuery(
|
||||||
search_text, config='english'
|
search_text, config=language
|
||||||
)
|
)
|
||||||
search_vectors = (
|
search_vectors = (
|
||||||
SearchVector('search_vector')
|
SearchVector('search_vector')
|
||||||
+ SearchVector(StringAgg('steps__ingredients__food__name', delimiter=' '), weight='B', config='english')
|
+ SearchVector(StringAgg('steps__ingredients__food__name', delimiter=' '), weight='B', config=language)
|
||||||
+ SearchVector(StringAgg('keywords__name', delimiter=' '), weight='B', config='english'))
|
+ SearchVector(StringAgg('keywords__name', delimiter=' '), weight='B', config=language))
|
||||||
search_rank = SearchRank(search_vectors, search_query)
|
search_rank = SearchRank(search_vectors, search_query)
|
||||||
# the results from trigram were really, really bad
|
# the results from trigram were really, really bad
|
||||||
# trigram = (
|
# trigram = (
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
from django.contrib.postgres.search import SearchVector
|
from django.contrib.postgres.search import SearchVector
|
||||||
from django.db.models.signals import post_save
|
from django.db.models.signals import post_save
|
||||||
from django.dispatch import receiver
|
from django.dispatch import receiver
|
||||||
|
from django.utils import translation
|
||||||
|
|
||||||
from cookbook.models import Recipe
|
from cookbook.models import Recipe
|
||||||
|
from cookbook.managers import DICTIONARY
|
||||||
|
|
||||||
|
|
||||||
@receiver(post_save, sender=Recipe)
|
@receiver(post_save, sender=Recipe)
|
||||||
@ -14,9 +16,10 @@ def update_recipe_search_vector(sender, instance=None, created=False, **kwargs):
|
|||||||
if hasattr(instance, '_dirty'):
|
if hasattr(instance, '_dirty'):
|
||||||
return
|
return
|
||||||
|
|
||||||
|
language = DICTIONARY.get(translation.get_language(), 'simple')
|
||||||
instance.search_vector = (
|
instance.search_vector = (
|
||||||
SearchVector('name', weight='A', config='english')
|
SearchVector('name', weight='A', config=language)
|
||||||
+ SearchVector('description', weight='C', config='english')
|
+ SearchVector('description', weight='C', config=language)
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
Loading…
Reference in New Issue
Block a user