【问题标题】:Haystack Django Elasticsearch spellingHaystack Django Elasticsearch 拼写
【发布时间】:2014-11-26 11:33:58
【问题描述】:

我在设置 haystack/django/elasticsearch 时遇到了两个问题

  1. 我从来没有从索引字段中得到结果,例如index.CharField(model_attr='title') 没有得到我的结果。仅当我将 {{ object.title }} 放入我的 txt 模板中时,我才能获得匹配标题的结果

  2. 如果我的标题是“foo”,我永远不会得到“fo”的结果,而我的后端设置中确实将 INCLUDE_SPELLING 设置设置为 True。

文档没有说明这些情况的任何特别之处,我的设置是根据 haystack 文档,我缺少什么?

我的索引:

class FooIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    title = indexes.CharField(model_attr='title')  # never gets me results unless I add it to the template

    def get_model(self):
        return Foo

我的设置:

HAYSTACK_CONNECTIONS = {
    'default': {
        'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
        'URL': 'http://127.0.0.1:9200/',
        'INDEX_NAME': 'haystack',
        'INCLUDE_SPELLING': True,
    },
}

【问题讨论】:

    标签: django elasticsearch django-haystack


    【解决方案1】:

    确保您启用了拼写检查组件。

    首先要做的是在您的 SearchIndex 类上创建一个特殊字段,该字段镜像文本字段,但使用 FacetCharField。这会禁用 Solr 所做的后处理,这可能会打乱您的建议。建议如下:

    class MySearchIndex(indexes.SearchIndex, indexes.Indexable):
        text = indexes.CharField(document=True, use_template=True)
        # ... normal fields then...
        suggestions = indexes.FacetCharField()
    
    def prepare(self, obj):
        prepared_data = super(MySearchIndex, self).prepare(obj)
        prepared_data['suggestions'] = prepared_data['text']
        return prepared_data
    

    完成后,spelling_suggestions 方法应该返回适​​当的值。

    【讨论】:

      【解决方案2】:

      1) 如果你有use_template=True 只索引你放在template 文件中的内容,那么把你想要索引的所有字段都放在那里。 2)添加INCLUDE_SPELLING后是否刷新了索引?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-09
        • 1970-01-01
        • 2015-09-21
        • 2014-06-27
        相关资源
        最近更新 更多