[英]Elastic search result filter by user
I'm using elasticsearch_dsl
which works great. 我正在使用
elasticsearch_dsl
,效果很好。
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. 我尝试使用rest_frameworks的过滤器,但没有成功。
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. 只需修改
_search
函数即可按用户过滤。 I don't know in what format do you store users in ES, but it should be something like: 我不知道您在ES中以哪种格式存储用户,但是应该是这样的:
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.