From c40a91fb67aa139c7637e020f3b23d3fff3499ca Mon Sep 17 00:00:00 2001 From: Robin Champenois Date: Mon, 28 Jun 2021 23:25:16 +0200 Subject: [PATCH] Better search --- avisstage/views_search.py | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/avisstage/views_search.py b/avisstage/views_search.py index 0e6a394..df5e75e 100644 --- a/avisstage/views_search.py +++ b/avisstage/views_search.py @@ -82,7 +82,6 @@ def cherche(**kwargs): 'type_stage', 'niveau_scol', 'structure', - 'date_*', "lieux.*^2", "auteur.nom^2", "thematiques^2", @@ -146,22 +145,30 @@ def cherche(**kwargs): if field_relevant('type_lieu'): filtres &= Q(lieux__type_lieu=kwargs["type_lieu"]) - - # Application - resultat = Stage.objects - if USE_ELASTICSEARCH and use_dsl: - resultat = dsl.to_queryset(True) + # Tri + tri = "pertinence" - #print(filtres) - resultat = resultat.filter(filtres).distinct() - tri = 'pertinence' - - if not use_dsl: - kwargs['tri'] = '-date_maj' - if field_relevant('tri') and kwargs['tri'] in ['-date_maj']: tri = kwargs['tri'] - resultat = resultat.order_by(tri) + + if not use_dsl: + tri = "-date_maj" + + # Application + resultat = Stage.objects.filter(filtres).distinct() + + if USE_ELASTICSEARCH and use_dsl: + dls_res = [s.meta.id for s in dsl.scan()[:50]] + resultat = resultat.filter(id__in=dsl_res) + + if tri == "pertinence": + resultat = resultat.order_by( + Case( + *[When(pk=pk, then=pos) for pos, pk in enumerate(pks)] + ) + ) + else: + resultat = resultat.order_by(tri) return resultat, tri