I'm using elasticsearch_dsl
which works great.
However, I would like the results to filter according the the user token that gets sent.
I tried using rest_frameworks' filters, but had no success with it.
What is the right way to achieve this?
http://localhost:9200/_search
class Task(models.Model):
title = models.CharField("Title", max_length=10000, blank=True)
owner = models.ForeignKey('auth.User', blank=True, null=True)
from rest_framework import filters
connections.create_connection()
class TaskIndex(DocType):
title = String()
class Meta:
index = 'task-index'
def filter_queryset(self, request, queryset, view):
return queryset.filter(owner=request.user)
def bulk_indexing():
TaskIndex.init()
es = Elasticsearch()
bulk(client=es, actions=(b.indexing() for b in models.Task.objects.all().iterator()))
def _search(title):
s = Search().filter('term', title=title.text)
response = s.execute()
return response
Just modify the _search
function to filter also by user. I don't know in what format do you store users in ES, but it should be something like:
from elasticsearch_dsl.query import Q
def _search(title, user):
s = Search().query('bool', must=[
Q('term', title=title.text),
Q('match', owner=user.pk),
])
return s.execute()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.