简体   繁体   English

django-haystack自动完成不能正常工作

[英]django-haystack autocomplete not working as it should

I am using: 我在用:

  • Django : 1.7.1
  • django-haystack : 2.4.1
  • and elastic search 2.x as engine elastic search 2.x作为引擎

Index Class 指数类

class WarehouseIndex(SearchIndex, Indexable):
    """
    SearchIndex Class that stored indexes for Model Warehouse
    """
    text = CharField(document=True, use_template=True)
    search_auto = EdgeNgramField()
    search_auto2 = NgramField()
    ....

   def get_model(self):
        return WareHouse

    def prepare(self, obj):
        self.prepared_data = super(WarehouseIndex, self).prepare(obj)
        search_auto = [obj.name, obj.sublocality, 
                       obj.locality, obj.city]
        self.prepared_data['search_auto'] = ' '.join(
            [x.lower() for x in search_auto if x is not None])
        self.prepared_data['search_auto2'] = ' '.join(
            [x.lower() for x in search_auto if x is not None])
        return self.prepared_data

I have build the index using rebuild_index and running the following code in shell 我使用rebuild_index构建索引并在shell中运行以下代码

>>> from haystack.query import SearchQuerySet, SQ
>>> sqs = SearchQuerySet().models(WareHouse)

>>> sqs.filter(search_auto='pond') # query 1
[]
>>> sqs.filter(search_auto2='pond') # query 2
[]
>>> sqs.filter(search_auto__startswith='pond') # query 3
[<SearchResult: base.warehouse (pk=u'1')>, <SearchResult: base.warehouse (pk=u'22')> ...]

>>> sqs.filter(search_auto2__startswith='pond') # # query 4
[<SearchResult: base.warehouse (pk=u'1')>, <SearchResult: base.warehouse (pk=u'22')> ...]

As you can see, query 1 and 2 returns "No result" query 3 and 4 returns "same result" even when seach_auto and search_auto2 are both different of field type EdgeNgramField and NgramField. 如您所见,查询1和2返回“无结果”查询3和4返回“相同结果”,即使seach_auto和search_auto2都是字段类型EdgeNgramField和NgramField都不同。

EDIT: Index mapping , Index Setting 编辑: 索引映射索引设置

EDIT: on futhere inspection I have noticed that haystack is treating my EdgeNgramField and NgramField as CharField - here are the term vectors , both are same 编辑:在futhere检查我注意到haystack正在将我的EdgeNgramField和NgramField视为CharField - 这里是术语向量 ,两者都是相同的

EDIT: On further inspection I found haystack/backend/elasticsearch_backend.py was silently parsing elasticsearch.exceptions.RequestError ( full trace here ) when trying to setup the mapping for the index. 编辑:进一步检查我发现haystack/backend/elasticsearch_backend.py在尝试设置索引的映射时默默地解析elasticsearch.exceptions.RequestError此处为完整跟踪 )。

why is my index not getting created properly? 为什么我的索引没有正确创建? what am I doing wrong? 我究竟做错了什么?

The RequestError you linked to indeed provides a useful hint. 您链接的RequestError确实提供了有用的提示。

You seem to be using ES 2.x and Haystack uses the _boost meta field which has been removed starting with ES 2.0. 您似乎使用ES 2.x而Haystack使用从ES 2.0开始删除_boost元字段。

You can see that there is an issue ( #1247 ) about making Haystack work with ES 2.0 and it is still open. 您可以看到有一个问题( #1247 )关于使Haystack与ES 2.0一起工作并且它仍然是开放的。

What you can do at this point is either to install ES 1.7.5 instead and it will work. 你现在可以做的是安装ES 1.7.5而不是它可以工作。

Or wait until the fix gets released in a new Haystack version. 或者等到修复程序在新的Haystack版本中发布。

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

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