Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with Highlight - KeyError: 0 #126

Open
code-wizard opened this issue May 3, 2018 · 4 comments
Open

Issue with Highlight - KeyError: 0 #126

code-wizard opened this issue May 3, 2018 · 4 comments

Comments

@code-wizard
Copy link

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&params=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

@rhblind
Copy link
Owner

rhblind commented May 22, 2018

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?

@code-wizard
Copy link
Author

Hey below are my configurations

drf-haystack==1.8.1
Django==2.0.2
djangorestframework==3.7.7
django-haystack==2.8.1

@code-wizard
Copy link
Author

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

@rhblind
Copy link
Owner

rhblind commented May 23, 2018

Thanks! I'll see if I can squeeze in some time to make a fix out of this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants