Issue with Highlight - KeyError: 0
I get the error below when I add HaystackHighlightFilter to a faceted viewset and use face url like
http://127.0.0.1:8002/api/v1/search/facets/?first_name=judith¶ms=u_type_exact%3AStudent When I check the to_representation(self, instance) and print(instance.highlighted) here is the structure of the instance.highlighted
{'text': ['<em>Judith</em>\nAmaefula\nSaint Hopkins\nMechanical Engineering\nMedicine']}
Is there an update that addresses this issues?
File "/Users/ebuka/.virtualenvs/university/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner response = get_response(request) File "/Users/ebuka/.virtualenvs/university/lib/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response response = self.process_exception_by_middleware(e, request) File "/Users/ebuka/.virtualenvs/university/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/Users/ebuka/.virtualenvs/university/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view return view_func(*args, **kwargs) File "/Users/ebuka/.virtualenvs/university/lib/python3.6/site-packages/rest_framework/viewsets.py", line 95, in view return self.dispatch(request, *args, **kwargs) File "/Users/ebuka/.virtualenvs/university/lib/python3.6/site-packages/rest_framework/views.py", line 494, in dispatch response = self.handle_exception(exc) File "/Users/ebuka/.virtualenvs/university/lib/python3.6/site-packages/rest_framework/views.py", line 454, in handle_exception self.raise_uncaught_exception(exc) File "/Users/ebuka/.virtualenvs/university/lib/python3.6/site-packages/rest_framework/views.py", line 491, in dispatch response = handler(request, *args, **kwargs) File "/Users/ebuka/dev/university/core/api.py", line 46, in facets return Response(serializer.data) File "/Users/ebuka/.virtualenvs/university/lib/python3.6/site-packages/rest_framework/serializers.py", line 537, in data ret = super(Serializer, self).data File "/Users/ebuka/.virtualenvs/university/lib/python3.6/site-packages/rest_framework/serializers.py", line 262, in data self._data = self.to_representation(self.instance) File "/Users/ebuka/.virtualenvs/university/lib/python3.6/site-packages/rest_framework/serializers.py", line 504, in to_representation ret[field.field_name] = field.to_representation(attribute) File "/Users/ebuka/.virtualenvs/university/lib/python3.6/site-packages/rest_framework/fields.py", line 1816, in to_representation return method(value) File "/Users/ebuka/.virtualenvs/university/lib/python3.6/site-packages/drf_haystack/serializers.py", line 411, in get_objects return serializer.data File "/Users/ebuka/.virtualenvs/university/lib/python3.6/site-packages/rest_framework/serializers.py", line 742, in data ret = super(ListSerializer, self).data File "/Users/ebuka/.virtualenvs/university/lib/python3.6/site-packages/rest_framework/serializers.py", line 262, in data self._data = self.to_representation(self.instance) File "/Users/ebuka/.virtualenvs/university/lib/python3.6/site-packages/rest_framework/serializers.py", line 660, in to_representation self.child.to_representation(item) for item in iterable File "/Users/ebuka/.virtualenvs/university/lib/python3.6/site-packages/rest_framework/serializers.py", line 660, in <listcomp> self.child.to_representation(item) for item in iterable File "/Users/ebuka/.virtualenvs/university/lib/python3.6/site-packages/drf_haystack/serializers.py", line 242, in to_representation ret["highlighted"] = instance.highlighted[0] KeyError: 0
Hey, thanks for the report. Don't think this has been reported before. May I ask what version you're using for Django, DRF, Haystack and search engine backend?
Hey below are my configurations
drf-haystack==1.8.1 Django==2.0.2 djangorestframework==3.7.7 django-haystack==2.8.1
For anyone else facing the same problem. I override the to_representation() in my serializer class like this
def to_representation(self, instance):
"""
If we have a serializer mapping, use that. Otherwise, use standard serializer behavior
Since we might be dealing with multiple indexes, some fields might
not be valid for all results. Do not render the fields which don't belong
to the search result.
"""
if self.Meta.serializers:
ret = self.multi_serializer_representation(instance)
else:
ret = super(HaystackSerializer, self).to_representation(instance)
prefix_field_names = len(getattr(self.Meta, "index_classes")) > 1
current_index = self._get_index_class_name(type(instance.searchindex))
for field in self.fields.keys():
orig_field = field
if prefix_field_names:
parts = field.split("__")
if len(parts) > 1:
index = parts[0][1:] # trim the preceding '_'
field = parts[1]
if index == current_index:
ret[field] = ret[orig_field]
del ret[orig_field]
elif field not in chain(instance.searchindex.fields.keys(), self._declared_fields.keys()):
del ret[orig_field]
# include the highlighted field in either case
if getattr(instance, "highlighted", None):
ret["highlighted"] = instance.highlighted.get("text")[0]
return ret
Thanks! I'll see if I can squeeze in some time to make a fix out of this!