Improve search

Search in trader
Combine ranked and trigram
This commit is contained in:
Edgar P. Burkhart 2022-12-21 17:18:02 +01:00
parent 4b4c5f827d
commit 5953d80ff2
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
2 changed files with 5 additions and 9 deletions

View file

@ -13,6 +13,7 @@
<h1>{% translate "Transactions" %}</h1> <h1>{% translate "Transactions" %}</h1>
{% transaction_table transactions %} {% transaction_table transactions %}
{% if page_obj %}
<div class="pagination"> <div class="pagination">
<span class="step-links"> <span class="step-links">
{% if page_obj.has_previous %} {% if page_obj.has_previous %}
@ -30,4 +31,5 @@
{% endif %} {% endif %}
</span> </span>
</div> </div>
{% endif %}
{% endblock %} {% endblock %}

View file

@ -217,22 +217,16 @@ def search(request):
Transaction.objects.annotate( Transaction.objects.annotate(
rank=SearchRank( rank=SearchRank(
SearchVector("name", weight="A") SearchVector("name", weight="A")
+ SearchVector("description", weight="C"), + SearchVector("description", weight="B")
+ SearchVector("trader", weight="B"),
SearchQuery(_search, search_type="websearch"), SearchQuery(_search, search_type="websearch"),
), ),
similarity=TrigramSimilarity("name", _search), similarity=TrigramSimilarity("name", _search),
) )
.filter(models.Q(rank__gte=0.1) | models.Q(similarity__gte=0.1)) .filter(models.Q(rank__gte=0.1) | models.Q(similarity__gte=0.3))
.order_by("-rank", "-date") .order_by("-rank", "-date")
) )
if _transactions.count() == 0:
_transactions = (
Transaction.objects.annotate(rank=TrigramSimilarity("name", _search))
.filter(rank__gte=0.1)
.order_by("-rank", "-date")
)
return render( return render(
request, request,
"main/transactions.html", "main/transactions.html",