nanterre: utilise Case/When pour calculer le score de correspondance (fixes #22518)

Afin d'éviter de calculer un score sur un nom complet ne contenant qu'un
prénom.
This commit is contained in:
Benjamin Dauvergne 2018-03-14 13:30:22 +01:00 committed by Thomas NOEL
parent 89ec65e116
commit f990159355
1 changed files with 11 additions and 4 deletions

View File

@ -36,7 +36,7 @@ import psycopg2
from django.conf import settings
from django.contrib.postgres.search import TrigramDistance
from django.db import connection
from django.db.models import Q, F, Value, ExpressionWrapper, CharField
from django.db.models import Q, F, Value, ExpressionWrapper, CharField, When, Case
from django.db.models.functions import Least, Greatest, Coalesce, Concat
from django.db import transaction
from django.utils.timezone import now, make_aware
@ -340,9 +340,16 @@ class PersonSearch(object):
self.add_filter('name', q)
# Compute similarity score
for expression in (fullname_naissance, fullname_usage):
self.name_similarities.append(
Value(1.0) - TrigramDistance(expression, self.luv(fullname)))
self.name_similarities.append(
Value(1.0) - Case(
When(content__nom_de_naissance='',
then=Value(1.0)),
default=TrigramDistance(fullname_naissance, self.luv(fullname))))
self.name_similarities.append(
Value(1.0) - Case(
When(content__nom_d_usage='',
then=Value(1.0)),
default=TrigramDistance(fullname_usage, self.luv(fullname))))
return self
def search_names(self, names):