2013-05-11 35 views
1

數據庫中的對象被命名爲newsnews testElasticsearch和auto_query

class ItemIndex(indexes.SearchIndex, indexes.Indexable): 
    text = indexes.CharField(document=True) 
    name = indexes.CharField(model_attr='name') 
    name_alt = indexes.CharField(model_attr='name_alt') 

    def get_model(self): 
     return Serial 

>>> from haystack.query import SearchQuerySet 
>>> sqs = SearchQuerySet().all() 
>>> sqs.count() 
4 
>>> SearchQuerySet().auto_query('new') # not working all query! 
[] 

如果使用haystack.backends.simple_backend.SimpleEngine其工作。

的Django == 1.5.1

Elasticsearch == 0.90

Django的乾草堆==主(2.0)

爲什麼????

+0

索引了Elasticsearch嗎?您的問題指出這些對象位於數據庫中,但您只能在搜索引擎編制索引後才能查詢搜索引擎中的對象。 – bennylope 2013-05-12 00:37:38

+0

如何檢查? './manage.py rebuild_index'不會給出錯誤。 – Silent 2013-05-12 04:25:22

+0

'curl -XGET http:// localhost:9200/haystack/modelresult/_search?q = new' work! '{「take」:2,「timed_out」:false,「_ shards」:{「total」:5,「success」:5,「failed」:0},「hits」:{「total」 「max_score」:0.11506981,「hits」:[{「_ index」:「haystack」,「_ type」:「modelresult」,「_ id」:「serials.serial.2」,「_ score」:0.11506981,「_source」: {「django_id」:「2」,「name」:「News」,「text」:null,「genres」:[1],「django_ct」:「serials.serial」,「id」:「serials.serial。 2「,」name_alt「:」「}}]}}'SearchQuerySet()。auto_query('new') - 不起作用 – Silent 2013-05-12 05:16:56

回答

1

它看起來不像是在填充所有導入文檔字段。

SearchIndex類有以下字段:

text = indexes.CharField(document=True) 
name = indexes.CharField(model_attr='name') 
name_alt = indexes.CharField(model_attr='name_alt') 

您已經定義了數據源namename_alt但不是text。命令行搜索的輸出顯示該字段在搜索索引中爲空。您有幾種選擇:

  • 填充該字段從模型屬性
  • 使用prepare_FOO法制備的內容該字段
  • 使用模板,使用use_template argumenttext領域,包括任何與該模板中的所有內容

現在後續問題是爲什麼auto_query失敗,但基本捲曲查詢工作?因爲auto_query is searching the content - 文件 - 這是缺少。

+0

很奇怪。關於創建prepare_content在文檔中沒有說什麼。如果你使用SimpleEngine,它爲什麼會起作用? – Silent 2013-05-13 05:20:12

+0

「簡單的後端通過數據庫本身進行非常基本的匹配,不推薦用於生產,但會返回結果。」 https://django-haystack.readthedocs.org/en/latest/tutorial.html?highlight=SimpleEngine#simple 搜索引擎是另一個數據存儲。 Haystack爲您提供了將數據映射到搜索索引,將其添加到搜索索引並查詢的工具 - 但您仍需準備數據並將其移入索引。 – bennylope 2013-05-13 15:13:33