我使用elasticsearch_dsl
效果很好。
但是,我希望结果根据发送的用户令牌进行过滤。
我尝试过使用rest_frameworks'过滤器,但没有成功。
实现这一目标的正确方法是什么?
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
答案 0 :(得分:-1)
只需修改_search
功能,即可按用户进行过滤。我不知道你在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()