我相信我需要通过Django URL代理我的ElasticSearch连接,以便按用户令牌进行过滤。
因此,我想使用localhost:9200/_search
。
localhost:8000/myapi/elastic/_search
我不确定如何连接它们。我尝试过使用序列化器/视图设置
class TaskIndex(DocType):
title = String()
class Meta:
index = 'task-index'
# Bulk indexing function, run in shell
def bulk_indexing():
TaskIndex.init()
es = Elasticsearch()
bulk(client=es, actions=(b.indexing() for b in models.Task.objects.all().iterator()))
# Simple search function
def _search(title):
s = Search().filter('term', title=title.text)
response = s.execute()
return response
来自myapp.search import TaskIndex
class ElasticSerializer(serializers.ModelSerializer):
class Meta:
model = TaskIndex
class ElasticViewSet(viewsets.ModelViewSet):
queryset = TaskIndex.objects.none()
serializer_class = ElasticSerializer
router.register(r'elastic', ElasticViewSet)
答案 0 :(得分:0)
所以这就是我能够实现它的方式。非常简单的方法:
from services import elastic_result
class ElasticView(APIView):
permission_classes=[]
def post(self, title):
_search = elastic_result(id, title)
return _search
pass
import requests
import json
def elastic_result(id, request):
requestdata = json.loads(request.body)
r = requests.post('http://localhost:9200/_search', json=requestdata)
items = r.json()
return Response(items)
import ElasticView
url(r'^elastic/_search', ElasticView.as_view()