Add slug generation and extend search results handling
Introduced slug creation using `slugify` for improved usability in search results. Extended the search results structure to include a new "posts" category alongside "products", "categories", and "brands". This enhances the flexibility and scope of the search functionality.
This commit is contained in:
parent
b4ef65314d
commit
8f55bece81
1 changed files with 8 additions and 4 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.http import Http404
|
from django.http import Http404
|
||||||
|
from django.utils.text import slugify
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
from django_elasticsearch_dsl import fields
|
from django_elasticsearch_dsl import fields
|
||||||
from elasticsearch import NotFoundError
|
from elasticsearch import NotFoundError
|
||||||
|
|
@ -63,16 +64,19 @@ def process_query(query: str = ""):
|
||||||
|
|
||||||
response = search.execute()
|
response = search.execute()
|
||||||
|
|
||||||
results = {"products": [], "categories": [], "brands": []}
|
results = {"products": [], "categories": [], "brands": [], "posts": []}
|
||||||
for hit in response.hits:
|
for hit in response.hits:
|
||||||
obj_uuid = getattr(hit, "uuid", hit.meta.id)
|
obj_uuid = getattr(hit, "uuid", hit.meta.id)
|
||||||
obj_name = getattr(hit, "name", "N/A")
|
obj_name = getattr(hit, "name", "N/A")
|
||||||
|
obj_slug = getattr(hit, "slug", slugify(hit.name))
|
||||||
if hit.meta.index == "products":
|
if hit.meta.index == "products":
|
||||||
results["products"].append({"uuid": obj_uuid, "name": obj_name})
|
results["products"].append({"uuid": obj_uuid, "name": obj_name, "slug": obj_slug})
|
||||||
elif hit.meta.index == "categories":
|
elif hit.meta.index == "categories":
|
||||||
results["categories"].append({"uuid": obj_uuid, "name": obj_name})
|
results["categories"].append({"uuid": obj_uuid, "name": obj_name, "slug": obj_slug})
|
||||||
elif hit.meta.index == "brands":
|
elif hit.meta.index == "brands":
|
||||||
results["brands"].append({"uuid": obj_uuid, "name": obj_name})
|
results["brands"].append({"uuid": obj_uuid, "name": obj_name, "slug": obj_slug})
|
||||||
|
elif hit.meta.index == "posts":
|
||||||
|
results["posts"].append({"uuid": obj_uuid, "name": obj_name, "slug": obj_slug})
|
||||||
|
|
||||||
return results
|
return results
|
||||||
except NotFoundError:
|
except NotFoundError:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue