简体   繁体   English

干草堆搜索无法使用自动完成功能

[英]Haystack search is not working with autocomplete

I am trying to use django-haystack for search functionality in our product. 我想在我们的产品中使用django-haystack进行搜索功能。 When user enters a any search keyword(ie 'computer'), it should search for this keyword against multiple fields. 当用户输入任何搜索关键字(即“计算机”)时,它应该针对多个字段搜索此关键字。 Eg when a user enters "Computer" it should find the objects where "Computer" is present in any of the field in that object. 例如,当用户输入“计算机”时,它应该在该对象的任何字段中找到“计算机”所在的对象。 But there is a problem. 但有一个问题。 If a user enters only "Comp" into search field, it isn't finding objects at all. 如果用户仅在搜索字段中输入“Comp”,则根本找不到对象。

So I have tried to use autocomplete. 所以我尝试使用自动完成功能。 With autocomplete I was able to achieve this for only one field. 通过自动完成功能,我只能在一个字段中实现此功能。 For other fields again its failing return result. 对于其他领域,其失败的返回结果。

I am having haystack with whoosh as backend. 我正在吃干草堆作为后端。

Environment Detail: 环境细节:
Django v1.5.3 Django v1.5.3
Haystack v2.1.0 干草堆v2.1.0
Whoosh v2.5.3 飞快移动v2.5.3

Following is defined in my search_index.py file. 以下是在我的search_index.py文件中定义的。

class Message_Index(indexes.SearchIndex, indexes.Indexable):
    text = indexes.EdgeNgramField(document=True, use_template=True)
    message = indexes.CharField(model_attr='messagetext', null=True)
    forum = indexes.CharField(model_attr='forum', null=True)
    status = indexes.CharField(model_attr='status', null=True)
    tags = indexes.CharField(model_attr='tags', null=True)
    author_name = indexes.EdgeNgramField(null=True)
    author_number = indexes.EdgeNgramField(null=True)
    message_date = indexes.DateTimeField(null=True)

    def get_model(self):
        return Message

    def index_queryset(self, using=None):
        return self.get_model().objects.all()

    def prepare_author_name(self, obj):
        return obj.message.user.name

    def prepare_author_number(self, obj):
        return obj.message.user.number

    def prepare_message_date(self, obj):
        return obj.message.date

    def prepare_tags(self, obj):
        return [tag.tag for tag in obj.tags.all()]

    def prepare_message_thread(self, obj):
        return obj.message.thread

I am trying to search in following way: 我试图按以下方式搜索:

when I am trying to search with search_keyword = 'user1', its returning the correct result, but when I am entering search_keyword = '223', its not returning any result. 当我尝试使用search_keyword ='user1'进行搜索时,它会返回正确的结果,但是当我输入search_keyword ='223'时,它不返回任何结果。

results_by_name = results.autocomplete(author_name=search_keyword)
results_by_number = results.autocomplete(author_number=search_keyword)

if results_by_name.count() > 0:
    combined_resultsets(results,results_by_name,'message_date')
elif results_by_number.count() > 0:
    combined_resultsets(results,results_by_number,'message_date')

combined_resultsets is function and defined as below: combined_resultsets是函数,定义如下:

def combined_resultsets(resultset1, resultset2, sortby):
    if sortby is not None:
        resultset1 = sorted(chain(resultset1, resultset2),key=attrgetter(sortby))
    else:
        resultset1 = chain(resultset1, resultset2)

Could someone help me with this? 有人可以帮我吗?

please setting your url project to autocomplete method on view.py application root. 请将您的url项目设置为view.py应用程序根目录上的自动完成方法。 you can follow documentation haystack on Autocomplete , and please check your template indexes. 您可以在自动填充上关注文档haystack,请检查您的模板索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM