Reputation: 1950
I believe I need to proxy my ElasticSearch connection via a Django URL in order to do filtering by user token.
So instead of going via localhost:9200/_search
, I want to use localhost:8000/myapi/elastic/_search
.
I am unsure how to connect them. I've tried using a serializers/views setup
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
from 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)
Upvotes: 0
Views: 380
Reputation: 1950
So this is how I was able to achieve it. Really simple method:
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()
Upvotes: 1