通过Django代理我的弹性搜索连接进行过滤

时间:2017-03-14 23:32:44

标签: django elasticsearch elasticsearch-dsl

我相信我需要通过Django URL代理我的ElasticSearch连接,以便按用户令牌进行过滤。

因此,我想使用localhost:9200/_search

而不是通过localhost:8000/myapi/elastic/_search

我不确定如何连接它们。我尝试过使用序列化器/视图设置

的myapp / search.py​​

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

API / serializers.py

来自myapp.search import TaskIndex

class ElasticSerializer(serializers.ModelSerializer):
    class Meta:
        model = TaskIndex

API / views.py

class ElasticViewSet(viewsets.ModelViewSet):
    queryset = TaskIndex.objects.none()
    serializer_class = ElasticSerializer

API / urls.py

router.register(r'elastic', ElasticViewSet)

1 个答案:

答案 0 :(得分:0)

所以这就是我能够实现它的方式。非常简单的方法:

views.py

from services import elastic_result

class ElasticView(APIView):
    permission_classes=[]
    def post(self, title):
        _search = elastic_result(id, title)
        return _search
        pass

services.py

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)

urls.py

import ElasticView
url(r'^elastic/_search', ElasticView.as_view()