Closed AlviseSembenico closed 3 years ago
django-elasticsearch-dsl has to manage data from end to end. If your use case is to query an Elasticsearch with already existing data, this is not the right tool (elasticsearch-dsl, the library is the right tool).
In you Document you defined the "id" field, while django-elasticsearch-dsl, use the _id
field, and the one you got in your Elasticsearch instance wont be compatible.
Hi, I'm currently facing the issue of not being able to retrieve the Id of a document properly. This is my code `class NewsDocument(Document):
`class NewsArticle(models.Model):
A sample of the data in the ES
{ "_index" : "neews", "_type" : "_doc", "_id" : "3DyhvHEBvFehRasu8OXt", "_version" : 1, "_seq_no" : 311, "_primary_term" : 1, "found" : true, "_source" : { "column1" : 311, "summary" : "", "keywords" : "", "title" : "Cards for electric car", "url" : "https://nltimes.nlt", "@timestamp" : "2019-12-12T01:00:00.000+01:00", "categories" : "['technology']", "text" : "Card holders can also check with most providers where and when their cards were used.", "publish_date" : "2019-12-12" } }
the full trace `Traceback (most recent call last): File "C:\Users\alvis\Documents\Intrical-backend\env\lib\site-packages\elasticsearch_dsl\utils.py", line 127, in getattr return self.getitem(attr_name) File "C:\Users\alvis\Documents\Intrical-backend\env\lib\site-packages\elasticsearch_dsl\utils.py", line 140, in getitem return _wrap(self.d[key]) KeyError: 'id'
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "C:\Users\alvis\Documents\Intrical-backend\env\lib\site-packages\rest_framework\fields.py", line 454, in get_attribute return get_attribute(instance, self.source_attrs) File "C:\Users\alvis\Documents\Intrical-backend\env\lib\site-packages\rest_framework\fields.py", line 94, in get_attribute instance = getattr(instance, attr) File "C:\Users\alvis\Documents\Intrical-backend\env\lib\site-packages\elasticsearch_dsl\utils.py", line 129, in getattr raise AttributeError( AttributeError: 'Hit' object has no attribute 'id'
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "C:\Users\alvis\Documents\Intrical-backend\env\lib\site-packages\django\core\handlers\exception.py", line 34, in inner response = get_response(request) File "C:\Users\alvis\Documents\Intrical-backend\env\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response response = self.process_exception_by_middleware(e, request) File "C:\Users\alvis\Documents\Intrical-backend\env\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response response = wrapped_callback(request, *callback_args, callback_kwargs) File "C:\Users\alvis\Documents\Intrical-backend\env\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view return view_func(*args, *kwargs) File "C:\Users\alvis\Documents\Intrical-backend\env\lib\site-packages\rest_framework\viewsets.py", line 114, in view return self.dispatch(request, args, kwargs) File "C:\Users\alvis\Documents\Intrical-backend\env\lib\site-packages\rest_framework\views.py", line 505, in dispatch response = self.handle_exception(exc) File "C:\Users\alvis\Documents\Intrical-backend\env\lib\site-packages\rest_framework\views.py", line 465, in handle_exception self.raise_uncaught_exception(exc) File "C:\Users\alvis\Documents\Intrical-backend\env\lib\site-packages\rest_framework\views.py", line 476, in raise_uncaught_exception raise exc File "C:\Users\alvis\Documents\Intrical-backend\env\lib\site-packages\rest_framework\views.py", line 502, in dispatch response = handler(request, *args, **kwargs) File "C:\Users\alvis\Documents\Intrical-backend\env\lib\site-packages\rest_framework\mixins.py", line 50, in list return Response(serializer.data) File "C:\Users\alvis\Documents\Intrical-backend\env\lib\site-packages\rest_framework\serializers.py", line 762, in data ret = super().data File "C:\Users\alvis\Documents\Intrical-backend\env\lib\site-packages\rest_framework\serializers.py", line 260, in data self._data = self.to_representation(self.instance) File "C:\Users\alvis\Documents\Intrical-backend\env\lib\site-packages\rest_framework\serializers.py", line 679, in to_representation return [ File "C:\Users\alvis\Documents\Intrical-backend\env\lib\site-packages\rest_framework\serializers.py", line 680, in
self.child.to_representation(item) for item in iterable
File "C:\Users\alvis\Documents\Intrical-backend\env\lib\site-packages\rest_framework\serializers.py", line 507, in to_representation
def to_representation(self, instance):
File "C:\Users\alvis\Documents\Intrical-backend\env\lib\site-packages\rest_framework\fields.py", line 487, in get_attribute
raise type(exc)(msg)
AttributeError: Got AttributeError when attempting to get a value for field
id
on serializerNewsDocumentSerializer
. The serializer field might be named incorrectly and not match any attribute or key on theHit
instance. Original exception text was: 'Hit' object has no attribute 'id'.`I have really no idea how to tackle this since my code is so minimal. Thank you for the support!