Skip to content

Commit

Permalink
Show help when algorithm is switched in search
Browse files Browse the repository at this point in the history
  • Loading branch information
medihack committed Feb 7, 2024
1 parent 81ff9e8 commit ccb1ee6
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 35 deletions.
2 changes: 1 addition & 1 deletion radis/search/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class SearchHandler(NamedTuple):


def register_search_handler(
name: str,
name: str, # TODO: may rename to algorithm
searcher: Searcher,
template_name: str,
) -> None:
Expand Down
3 changes: 2 additions & 1 deletion radis/search/templates/search/_search_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
margin-left: -40px;
padding: 0;
z-index: 100">{% bootstrap_icon "x" 20 %}</button>
<select hx-get=""
<select hx-get="{% url "search_help" %}"
hx-trigger="change"
hx-target="#search-panel"
name="algorithm"
class="form-select"
style="max-width: 200px"
Expand Down
6 changes: 6 additions & 0 deletions radis/search/templates/search/_search_help.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div class="card mt-3">
<div class="card-body">
<h5 class="card-title">{{ selected_algorithm }}</h5>
{% include help_template_name %}
</div>
</div>
20 changes: 10 additions & 10 deletions radis/search/templates/search/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ <h4 class="mb-3">Search Reports</h4>
{% endblock heading %}
{% block content %}
{% include "search/_search_form.html" %}
{% if not selected_algorithm %}
<div class="alert alert-danger d-flex align-items-center mt-3"
role="alert">
{% bootstrap_icon "exclamation-triangle-fill" %}
<div class="ms-2">Setup of RADIS is incomplete. No search algorithms are registered.</div>
</div>
{% else %}
{% if query %}
<div id="search-panel">
{% if not selected_algorithm %}
<div class="alert alert-danger d-flex align-items-center mt-3"
role="alert">
{% bootstrap_icon "exclamation-triangle-fill" %}
<div class="ms-2">Setup of RADIS is incomplete. No search algorithms are registered.</div>
</div>
{% elif query %}
{% include "search/_search_results.html" %}
{% else %}
<div class="mt-3">{% include help_template_name %}</div>
{% include "search/_search_help.html" %}
{% endif %}
{% endif %}
</div>
{% endblock content %}
3 changes: 2 additions & 1 deletion radis/search/urls.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from django.urls import path
from rest_framework.urlpatterns import format_suffix_patterns

from .views import SearchView
from .views import HelpView, SearchView

urlpatterns = [
path("", SearchView.as_view(), name="search"),
path("help", HelpView.as_view(), name="search_help"),
]

urlpatterns = format_suffix_patterns(urlpatterns)
21 changes: 20 additions & 1 deletion radis/search/views.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from typing import Any

from django.contrib.auth.mixins import LoginRequiredMixin
from django.core.exceptions import BadRequest
from django.core.exceptions import BadRequest, ValidationError
from django.core.paginator import Paginator
from django.shortcuts import render
from django.views import View

from radis.core.mixins import HtmxOnlyMixin
from radis.core.types import AuthenticatedRequest

from .serializers import SearchParamsSerializer
Expand Down Expand Up @@ -58,3 +59,21 @@ def get(self, request: AuthenticatedRequest, *args, **kwargs):
context["documents"] = result.documents

return render(request, "search/search.html", context)


class HelpView(LoginRequiredMixin, HtmxOnlyMixin, View):
def get(self, request: AuthenticatedRequest, *args, **kwargs):
algorithm = request.GET.get("algorithm", "")
search_handler = search_handlers.get(algorithm)

if not search_handler:
raise ValidationError(f"Invalid search algorithm: {algorithm}")

return render(
request,
"search/_search_help.html",
{
"selected_algorithm": algorithm,
"help_template_name": search_handler.template_name,
},
)
7 changes: 6 additions & 1 deletion radis/vespa/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def register_app():
fetch_document,
update_document,
)
from .utils.search_methods import search_bm25
from .utils.search_methods import search_bm25, search_hybrid

def handle_report(event_type: ReportEventType, report: Report):
# Sync reports with Vespa
Expand All @@ -48,3 +48,8 @@ def search_vespa_bm25(search: Search) -> SearchResult:
return search_bm25(search.query, search.offset, search.page_size)

register_search_handler("Vespa BM25", search_vespa_bm25, "vespa/_bm25_help.html")

def search_vespa_hybrid(search: Search) -> SearchResult:
return search_hybrid(search.query, search.offset, search.page_size)

register_search_handler("Vespa Hybrid", search_vespa_hybrid, "vespa/_hybrid_help.html")
35 changes: 15 additions & 20 deletions radis/vespa/templates/vespa/_bm25_help.html
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
<div class="card">
<div class="card-body">
<h5 class="card-title">Vespa BM25 Help</h5>
<p class="card-text">
Vespa ist a modern full text search database and BM25 is a simple search
ranking algorithm based on the query terms appearing in each document.
</p>
<p class="card-text">
{# https://docs.vespa.ai/en/reference/simple-query-language-reference.html #}
The following rules apply:
<ul>
<li>Search is case-insensitive.</li>
<li>All terms in the query must be matched by a document (implicit AND query).</li>
<li>term1 OR term2 (capital OR) means that either term1 or term2 must match.</li>
<li>Terms surrounded by quotes are search as exact phrase, e.g. "lung disease".</li>
<li>+ in front of a term means that the exact term must match.</li>
</ul>
</p>
</div>
</div>
<p class="card-text">
Vespa ist a modern full text search database and BM25 is a simple search
ranking algorithm based on the query terms appearing in each document.
</p>
<p class="card-text">
{# https://docs.vespa.ai/en/reference/simple-query-language-reference.html #}
The following rules apply:
<ul>
<li>Search is case-insensitive.</li>
<li>All terms in the query must be matched by a document (implicit AND query).</li>
<li>term1 OR term2 (capital OR) means that either term1 or term2 must match.</li>
<li>Terms surrounded by quotes are search as exact phrase, e.g. "lung disease".</li>
<li>+ in front of a term means that the exact term must match.</li>
</ul>
</p>
1 change: 1 addition & 0 deletions radis/vespa/templates/vespa/_hybrid_help.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TODO: Hybrid Help
8 changes: 8 additions & 0 deletions radis/vespa/utils/search_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,11 @@ def search_bm25(query: str, offset: int, page_size: int) -> SearchResult:
coverage=response.json["root"]["coverage"]["coverage"],
documents=[document_from_vespa_response(hit) for hit in response.hits],
)


def search_hybrid(query: str, offset: int, page_size: int) -> SearchResult:
return SearchResult(
total_count=0,
coverage=0,
documents=[],
)

0 comments on commit ccb1ee6

Please sign in to comment.