From 85337217e6266bce11fe02ba72b54cdef9a19e35 Mon Sep 17 00:00:00 2001 From: Raphael Odini Date: Mon, 25 Sep 2023 12:03:06 +0200 Subject: [PATCH] Add siret & sectors. Improve a bit. Add info --- .../commands/set_search_vector_field.py | 15 ++++- lemarche/siaes/models.py | 14 ++-- lemarche/templates/siaes/search_results.html | 64 +++++-------------- 3 files changed, 39 insertions(+), 54 deletions(-) diff --git a/lemarche/siaes/management/commands/set_search_vector_field.py b/lemarche/siaes/management/commands/set_search_vector_field.py index 1691048ce..1117f5302 100644 --- a/lemarche/siaes/management/commands/set_search_vector_field.py +++ b/lemarche/siaes/management/commands/set_search_vector_field.py @@ -16,7 +16,7 @@ def handle(self, *args, **options): self.stdout_info("-" * 80) self.stdout_info("Reseting search_vector field...") progress = 0 - for siae in Siae.objects.prefetch_related("offers", "labels").all(): + for siae in Siae.objects.prefetch_related("sectors", "offers", "labels").all(): siae_search_vector = ( SearchVector( Value(siae.name, output_field=models.CharField()), @@ -28,6 +28,11 @@ def handle(self, *args, **options): # weight="A", # config="french", ) + + SearchVector( + Value(siae.siret, output_field=models.CharField()), + # weight="A", + # config="french", + ) + SearchVector( Value(siae.kind, output_field=models.CharField()), # weight="A", @@ -39,6 +44,14 @@ def handle(self, *args, **options): config="french", ) ) + if siae.sectors: + siae_search_vector += SearchVector( + Value( + " ".join(str(sector.name) for sector in siae.sectors.all()), + ), + # weight="A", + config="french", + ) if siae.offers: siae_search_vector += SearchVector( Value( diff --git a/lemarche/siaes/models.py b/lemarche/siaes/models.py index 7dbf79cef..b7c0672f7 100644 --- a/lemarche/siaes/models.py +++ b/lemarche/siaes/models.py @@ -195,7 +195,9 @@ def filter_full_text(self, search_string): search_vector = ( SearchVector("name") + SearchVector("brand") + + SearchVector("siret") + SearchVector("description", config="french") + + SearchVector("sectors__name", config="french") + SearchVector("offers__name", config="french") + SearchVector("labels__name", config="french") ) @@ -204,14 +206,18 @@ def filter_full_text(self, search_string): return self.annotate(rank=SearchRank(search_vector, search_query)).filter(rank__gte=0.01) def filter_full_text_on_search_vector_field(self, search_string): - # SearchQuery uses 'AND' by default. Change to 'OR'. + # SearchQuery uses 'AND' by default. Change to 'OR' (and add full search_string as default) search_string_list = search_string.split(" ") - filters = SearchQuery(search_string_list[0], config="french") + filters = SearchQuery(search_string, config="french") if len(search_string_list) > 1: - for search_term in search_string_list[1:]: + for search_term in search_string_list: filters |= SearchQuery(search_term, config="french") - return self.filter(search_vector=filters).annotate(rank=SearchRank(F("search_vector"), filters)) + return ( + self.filter(search_vector=filters) + .annotate(rank=SearchRank(F("search_vector"), filters)) + .filter(rank__gte=0.01) + ) def filter_sectors(self, sectors): return self.filter(sectors__in=sectors) diff --git a/lemarche/templates/siaes/search_results.html b/lemarche/templates/siaes/search_results.html index ba397c3b9..a6073943d 100644 --- a/lemarche/templates/siaes/search_results.html +++ b/lemarche/templates/siaes/search_results.html @@ -42,17 +42,6 @@ Recherche par critères -