Django 搜索包含空格的字符串

发布于 2024-09-08 06:12:17 字数 639 浏览 7 评论 0原文

我有一个按名称搜索函数,如果搜索与名字或姓氏匹配,它应该返回一个人的名字。问题是,如果我搜索像“名字姓氏”这样的字符串,它找不到匹配的名字(猜测是因为单词之间有空格)。我应该做什么才能使搜索工作正常进行? 另外,如果我想使用相同的搜索用户名(位于另一个表中)进行搜索,我该怎么做? 多谢!

我的代码:

 def search(request):
    query = request.GET.get('q', '')
    if query:
        qset1 = (
            Q(first_name__icontains=query) |
            Q(last_name__icontains=query) 
                 )
        results = UserProfile.objects.filter(qset1).distinct()
    else:
        results = []

    return render_to_response("search/searchName.html", {

    'results': results,     
    'query': query},
    context_instance=RequestContext(request))

I have a seach by name function, which should return the name of one person, if the search matches the first name or the last name. The problem is that if i search for strings like 'firstname lastname' it doesn't find the name that matches (guess it's because of the space between the words) . What should i do for the search to work?
Also, if i want to search with the same search the username (wich is in another table) how can i do it?
Thanks a lot!

my code:

 def search(request):
    query = request.GET.get('q', '')
    if query:
        qset1 = (
            Q(first_name__icontains=query) |
            Q(last_name__icontains=query) 
                 )
        results = UserProfile.objects.filter(qset1).distinct()
    else:
        results = []

    return render_to_response("search/searchName.html", {

    'results': results,     
    'query': query},
    context_instance=RequestContext(request))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟花易冷人易散 2024-09-15 06:12:17

现在,您的解析根本不会拆分查询,您可以这样做,循环遍历结果,并为自己构建一个 Q 对象以放入查询中。

import operator
queries = request.GET.get('q').split()
qset1 =  reduce(operator.__or__, [Q(first_name__icontains=query) | Q(last_name__icontains=query) for query in queries])

results = UserProfile.objects.filter(qset1).distinct()

这基本上要做的就是分割查询,并循环遍历每个单词,同时完全执行您已经在做的事情。因此,如果他们搜索“Frank Podolski”,它将检查“Frank”是否在名字、姓氏中,然后检查“Podolski”是否也在名字、姓氏中,而不是整个查询中。

当然,您还可以采取更多措施来改进搜索,例如词干提取、删除停用词等,但这超出了这个问题的上下文。

Right now your parsing doesn't split the query up at all, you could do that, loop through the results, and build yourself a Q object to place into your query.

import operator
queries = request.GET.get('q').split()
qset1 =  reduce(operator.__or__, [Q(first_name__icontains=query) | Q(last_name__icontains=query) for query in queries])

results = UserProfile.objects.filter(qset1).distinct()

What this basically will do is split the query up, and loop through each word while doing exactly what you're already doing. So if they search for "Frank Podolski" it will check that "Frank" is in the firstname, lastname, and then if "Podolski" is also in the firstname, lastname, rather than the query as a whole.

Of course, there is a lot more you can do to improve search like stemming, removing stop words, etc but that's out of the context of this question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文