Django haystack elasticsearch autocomplete不起作用

时间:2016-05-02 19:42:05

标签: django elasticsearch autocomplete django-haystack

在数据库中,我的产品名称为:“产品1”,“产品2”等。搜索“Produc”应返回许多值,但我什么也得不到。 我的索引:

from haystack import indexes

from .models import Product


class ProductIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True)
    id = indexes.IntegerField(model_attr='id')
    title = indexes.CharField(model_attr='title')
    slug = indexes.CharField(model_attr='slug')
    description = indexes.CharField(model_attr='description')
    content_auto = indexes.NgramField(model_attr='title')

    def get_model(self):
        return Product

    def index_queryset(self, using=None):
        return self.get_model().objects.all()
来自shell的

结果:

In [3]: SearchQuerySet().autocomplete(content_auto='produc')
Out[3]: []

In [4]: SearchQuerySet().autocomplete(content_auto='product')
Out[4]: [<SearchResult: catalogue.product (pk=u'4')>, <SearchResult:      
catalogue.product (pk=u'9')>, <SearchResult: catalogue.product  
(pk=u'11')>,  <SearchResult: catalogue.product (pk=u'16')>,   
<SearchResult: catalogue.product (pk=u'23')>, <SearchResult:  
catalogue.product (pk=u'28')>, <SearchResult: catalogue.product  
(pk=u'30')>, <SearchResult: catalogue.product (pk=u'35')>, <SearchResult: 
catalogue.product (pk=u'42')>, <SearchResult: catalogue.product 
(pk=u'47')>, <SearchResult: catalogue.product (pk=u'54')>, <SearchResult: 
catalogue.product (pk=u'59')>, <SearchResult: catalogue.product 
(pk=u'61')>, <SearchResult: catalogue.product (pk=u'66')>, <SearchResult: 
catalogue.product (pk=u'73')>, <SearchResult: catalogue.product 
(pk=u'78')>, <SearchResult: catalogue.product (pk=u'80')>, <SearchResult: 
catalogue.product (pk=u'85')>, <SearchResult: catalogue.product 
(pk=u'92')>, u'...(remaining elements truncated)...']

1 个答案:

答案 0 :(得分:0)

如果您刚刚添加了content_auto字段,则可能需要运行manage.py rebuild_index来创建更新后的索引。