Open Prakash2403 opened 3 years ago
Leaving this note here for anyone trying to get more_like_this working with Elasticsearch 7 or higher. I could not get it to work in the AWS version of Elasticsearch. The fix proposed by @Prakash2403 works, my solution was to use a local version of the class:-
from rest_framework.decorators import action
from elasticsearch_dsl.query import MoreLikeThis
from rest_framework.response import Response
import copy
"""
The current version of django-elasticsearch-dsl-drf will not work with Elasticsearch 7 or higher more_like_this
reason being that the _type parameter is deprecated. So we copy their code and fix it locally
"""
class MoreLikeThisMixin(object):
"""More-like-this mixin."""
@action(detail=True)
def more_like_this(self, request, pk=None, id=None):
"""More-like-this functionality detail view.
:param request:
:return:
"""
if 'view' in request.parser_context:
view = request.parser_context['view']
kwargs = copy.copy(getattr(view, 'more_like_this_options', {}))
id_ = pk if pk else id
queryset = self.filter_queryset(self.get_queryset())
fields = kwargs.pop('fields', [])
if fields:
queryset = queryset.query(
MoreLikeThis(
fields=fields,
like={
'_id': "{}".format(id_),
'_index': "{}".format(self.index)
},
**kwargs
)
).sort('_score')
else:
queryset = queryset.query(
MoreLikeThis(
like={
'_id': "{}".format(id_),
'_index': "{}".format(self.index)
},
**kwargs
)
).sort('_score')
# Standard list-view implementation
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)
serializer = self.get_serializer(queryset, many=True)
return Response(serializer.data)
Describe the bug _type parameter is deprecated in
MoreLikeThis
query in the latest version of ES. I get the following deprecation warning after I query atmore_like_this
endpointEnvironment
Python environment:
pip list
python --version
3.9.6
Which version of Elasticsearch are you using? 7.12.1
To Reproduce Steps to reproduce the behavior:
ViewSet
and inheritMoreLikeThisMixin
more_like_this
endpointExpected behavior Deprecation warnings shouldn't appear
@barseghyanartur I've fixed it on my fork. If you feel that this should be merged here, kindly let me know. I'll make a PR