我目前拥有的代码可以按单词和确切的字符串进行搜索:
def search(request):
query = request.GET.get('q', '')
if query:
qset = (
Q(Topic_id__Name__icontains=query) |
Q(Question_id__Statement__icontains=query)
)
results = Response.objects.filter(qset).distinct()
else:
results = []
return render_to_response("app/search.html", {
"results": results,
"query": query
})
我想要的是,如果有人要搜索“我们的安全和数据策略是什么”,搜索将逐个选择每个单词并进行搜索,例如“什么”,“是”,“我们的”,“安全性”,“和”,“数据”。
我尝试通过拆分字符串来实现for循环,但是每当我进行更改时,我的网站就会崩溃:
def search(request):
query = request.GET.get('q', '')
if query:
for term in query.split():
qset |= Q(Topic_id__Name__icontains=term) |
Q(Question_id__Statement__icontains=term)
results = Response.objects.filter(qset).distinct()
else: results = []
return render_to_response("app/search.html", { "results": results, "query": query }) – Lloyd 8 mins ago