Skip to content

Commit

Permalink
Add siret & sectors. Improve a bit. Add info
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Sep 25, 2023
1 parent ea6a841 commit 8533721
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 54 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
64 changes: 15 additions & 49 deletions lemarche/templates/siaes/search_results.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,6 @@
Recherche par critères
</a>
</li>
<li class="nav-item" role="presentation">
<a class="nav-link"
id="search-text-tab"
data-toggle="tab"
href="#search-text"
role="tab"
aria-controls="search-text"
aria-selected="false">
Recherche par SIRET / nom
</a>
</li>
<li class="nav-item" role="presentation">
<a class="nav-link"
id="search-full-text-1-tab"
Expand Down Expand Up @@ -187,34 +176,6 @@
</div>
</form>
</div>
<div class="tab-pane fade"
id="search-text"
role="tabpanel"
aria-labelledby="search-text-tab">
<form method="GET"
action="{% url 'siae:search_results' %}"
id="text-search-form"
aria-label="Rechercher des prestataires de l'insertion et du handicap"
role="search">
{% bootstrap_form_errors form type="all" %}
<div class="row">
<div class="col-12 col-lg-8">
<div class="row">
<div class="col-12">{% bootstrap_field form.q %}</div>
</div>
</div>
<div class="col-12 col-lg-4">
<span class="mb-2 d-none d-md-inline-block">&nbsp;</span>
<button id="text-search-submit"
class="btn btn-primary btn-block btn-ico"
type="submit">
<span>Rechercher</span>
<i class="ri-search-line ri-lg"></i>
</button>
</div>
</div>
</form>
</div>
<div class="tab-pane fade"
id="search-full-text-1"
role="tabpanel"
Expand All @@ -230,6 +191,13 @@
<div class="row">
<div class="col-12">{% bootstrap_field form.search_1 %}</div>
</div>
<div class="alert alert-info">
<ul class="mb-0">
<li>Recherche effectuée en "live" (indéxation des champs à chaque recherche)</li>
<li>Recherche effectuée sur les champs suivants : name, brand, siret, sectors (name), offers (name), labels (name)</li>
<li>Recherche "AND" ; pas de poids ; filtre rank > 0.01</li>
</ul>
</div>
</div>
<div class="col-12 col-lg-4">
<span class="mb-2 d-none d-md-inline-block">&nbsp;</span>
Expand Down Expand Up @@ -258,6 +226,13 @@
<div class="row">
<div class="col-12">{% bootstrap_field form.search_2 %}</div>
</div>
<div class="alert alert-info">
<ul class="mb-0">
<li>Recherche effectuée sur le champ "search_vector" (indéxé de façon asynchrone)</li>
<li>Recherche effectuée sur les champs suivants : name, brand, siret, sectors (name), offers (name), labels (name)</li>
<li>Recherche "OR" ; pas de poids ; filtre rank > 0.01</li>
</ul>
</div>
</div>
<div class="col-12 col-lg-4">
<span class="mb-2 d-none d-md-inline-block">&nbsp;</span>
Expand Down Expand Up @@ -455,10 +430,6 @@ <h3 class="modal-title">Pour accéder à la recherche avancée multicritères, i
// first tab
let searchFilterTab = document.getElementById('search-filter-tab');
let searchFilterContent = document.getElementById('search-filter');
// second tab
let searchTextTab = document.getElementById('search-text-tab');
let searchTextContent = document.getElementById('search-text');
let qInput = document.getElementById('id_q');
// third tab
let searchFullText1Tab = document.getElementById('search-full-text-1-tab');
let searchFullText1Content = document.getElementById('search-full-text-1');
Expand All @@ -477,13 +448,8 @@ <h3 class="modal-title">Pour accéder à la recherche avancée multicritères, i
// init search form
if (search1Input.value) {
showSearchTextForm(searchFullText1Tab, searchFullText1Content);
}
else if (search2Input.value) {
} else if (search2Input.value) {
showSearchTextForm(searchFullText2Tab, searchFullText2Content);
}
else if (qInput.value) {
showSearchTextForm(searchTextTab, searchTextContent);
// hideSearchFilterForm(searchFilterTab, searchFilterContent);
} else {
showSearchFilterForm(searchFilterTab, searchFilterContent);
}
Expand Down

0 comments on commit 8533721

Please sign in to comment.