2013-10-21 17 views
1

我今天學到了Sunspot_rails項目,如何自動重新索引黑子on Rails的,當數據庫中更改3

參考我的演示片段:http://youtu.be/qmtbDg4VtOw

,每當添加一個新的項目進入數據庫,

我們應該做

rake sunspot:reindex 

這樣我們就可以通過太陽黑子搜索來搜索新增加的日期。

是那裏

自動

運行

rake sunspot:reindex 

更好的辦法當數據庫發生變化?

在此先感謝

PS:如果運行耙太陽黑子:在命令行重新索引,它會顯示確認prompt.Can我禁用該選項。 [本本] $耙太陽黑子:REINDEX

*Note: the reindex task will remove your current indexes and start from scratch. 
If you have a large dataset, reindexing can take a very long time, possibly weeks. 
This is not encouraged if you have anywhere near or over 1 million rows. 
Are you sure you want to drop your indexes and completely reindex? (y/n) 


# Sunspot 
    searchable do 
    text :name 
    text :author 
    text :comment 
    text :sale_type 
    text :category 
    # text :isbn 
    end 

sunspot.yml

[config] $ cat sunspot.yml 
production: 
    solr: 
    hostname: localhost 
    port: 8983 
    log_level: WARNING 
    # read_timeout: 2 
    # open_timeout: 0.5 

development: 
    solr: 
    hostname: localhost 
    port: 8982 
    log_level: INFO 

test: 
    solr: 
    hostname: localhost 
    port: 8981 
    log_level: WARNING 
+0

默認情況下,新數據應添加到Sunspot添加的'save'回調中的索引(就像@MauricioLinhares寫的),如果您收到不同的行爲,請粘貼您的「searchable」塊。 – zrl3dx

+0

你的'config/sunspot.yml'文件中有'auto_commit_after_request:false'行添加到你的環境中了嗎?編輯:關於沉默布爾提示:嘗試'rake sunspot:reindex [,, true]此**應該執行reindex而不要求您確認,但這取決於您的'sunspot_solr' gem版本(或者您可以覆蓋該具體任務)。 – zrl3dx

+0

我在sunspot.yml中沒有auto_commit_after_request – newBike

回答

0

默認情況下,sunspot您更新上Solr模型,只要你把它保存到數據庫中,你不必手動重新索引任何模型。

rake sunspot:reindex命令只應在您更改由模型索引的字段或當您更改Solr上的schema.xml文件時運行。

+0

哎呀,我希望如此,但就我而言,你錯了。我必須重新編制索引,以便可以搜索新增的數據。我附上20秒的剪輯來顯示。 – newBike

相關問題