Skip to content

Commit

Permalink
Add siret & sectors. Improve a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Sep 25, 2023
1 parent ea6a841 commit 2d4b53f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
15 changes: 14 additions & 1 deletion lemarche/siaes/management/commands/set_search_vector_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand All @@ -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",
Expand All @@ -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(
Expand Down
14 changes: 10 additions & 4 deletions lemarche/siaes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
Expand All @@ -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)
Expand Down

0 comments on commit 2d4b53f

Please sign in to comment.