Skip to content

Commit

Permalink
Integrates Document with search form
Browse files Browse the repository at this point in the history
  • Loading branch information
jlariza committed May 18, 2024
1 parent bd35144 commit ab2ff7c
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion django_project/opensearch_workshop/pokemons/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from .forms import SearchPokemonForm
from .models import Pokemon
from .documents import PokemonDocument


# Create your views here.
Expand All @@ -24,5 +25,20 @@ def get_queryset(self) -> QuerySet[Any]:
queryset = super().get_queryset()
if form.is_valid():
data = form.cleaned_data
queryset = queryset.filter(name__icontains=data["name"])
name = data["name"]
os_query = (
PokemonDocument.search()
.filter(
"query_string",
query=f"*{name}*",
fields=[
"name",
],
)
.sort("pokemon_id")
# los resultados de opensearch se deben paginar
# pero está fuera del scope de este taller
.extra(size=1000, track_total_hits=True)
)
queryset = os_query.to_queryset()
return queryset

0 comments on commit ab2ff7c

Please sign in to comment.