2014-10-01 70 views
0

我有兩個問題,建立草堆/ Django的/ elasticsearch草堆Django的Elasticsearch拼寫

  1. 我從來沒有從索引字段,如結果indexes.CharField(model_attr ='title')沒有得到我的結果。只有當我將{{object.title}}放入我的txt模板中時,纔會得到匹配標題的結果

  2. 如果我的標題是'foo',我從來沒有得到'fo'的結果,而我確實設置了INCLUDE_SPELLING設置在我的後端設置爲True。

該文檔沒有說明關於這些情況的任何特殊情況,我的設置是根據乾草堆文檔,我錯過了什麼?

我的指數:

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, 
    }, 
} 

回答

0

1)如果你有use_template=True只有你放什麼template文件,以便把所有你想在那裏建立索引的字段索引。 2)您是否在添加INCLUDE_SPELLING後刷新了索引?

0

確保啓用了拼寫檢查組件。

首先要做的是在您的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方法應返回相應的值。