Skip to content

Commit

Permalink
Updates pokemon list view to integrate nested fields to search
Browse files Browse the repository at this point in the history
  • Loading branch information
jlariza committed May 21, 2024
1 parent 27c29a2 commit 7b9a292
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions django_project/opensearch_workshop/pokemons/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
from django.db.models.query import QuerySet
from django.views.generic import ListView
from django.views.generic.edit import FormMixin
from opensearchpy.helpers.query import Q

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


# Create your views here.
Expand All @@ -26,15 +27,37 @@ def get_queryset(self) -> QuerySet[Any]:
if form.is_valid():
data = form.cleaned_data
name = data["name"]
query = Q(
"bool",
should=[
Q("query_string", query=f"*{name}*", fields=["name"]),
Q(
"nested",
path="types",
query=Q(
"query_string", query=f"*{name}*", fields=["types.name"]
),
),
Q(
"nested",
path="moves",
query=Q(
"query_string", query=f"*{name}*", fields=["moves.name"]
),
),
Q(
"nested",
path="abilities",
query=Q(
"query_string", query=f"*{name}*", fields=["abilities.name"]
),
),
],
minimum_should_match=1,
)
os_query = (
PokemonDocument.search()
.filter(
"query_string",
query=f"*{name}*",
fields=[
"name",
],
)
.query(query)
.sort("pokemon_id")
# los resultados de opensearch se deben paginar
# pero está fuera del scope de este taller
Expand Down

0 comments on commit 7b9a292

Please sign in to comment.