From 2d4b53fbbf79e8edba497a08e9608b5747e87e0f Mon Sep 17 00:00:00 2001
From: Raphael Odini <raphodn@users.noreply.github.com>
Date: Mon, 25 Sep 2023 12:03:06 +0200
Subject: [PATCH] Add siret & sectors. Improve a bit

---
 .../commands/set_search_vector_field.py           | 15 ++++++++++++++-
 lemarche/siaes/models.py                          | 14 ++++++++++----
 2 files changed, 24 insertions(+), 5 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)