2013-01-17 59 views
1

我剛開始學習使用Sunspot/Solr與Rails。如何防止太陽黑子,Solr返回所有記錄時參數[:搜索] =「」

我跟着這個RailsCast http://railscasts.com/episodes/278-search-with-sunspot

當我有沒有搜索查詢和搜索領域是空白的,Solr的默認情況下,返回從文章模型的所有結果。

這裏是我的ArticlesController#指數

def index 
    @search = Article.search do 
     fulltext params[:search] 
    end 
    @articles = Article.all 
    @search_results = @search.results 
end 

當在搜索欄中輸入任何PARAMS,公正 「的」 控制檯輸出以下:

Started GET "/articles?utf8=%E2%9C%93&search=" for 127.0.0.1 at 2013-01-17 12:28:09 -0800 
    Processing by ArticlesController#index as HTML 
    Parameters: {"utf8"=>"✓", "search"=>""} 
    SOLR Request (17.0ms) [ path=#<RSolr::Client:0x007f9b5a6643e0> parameters={data: fq=type%3AArticle&start=0&rows=30&q=%2A%3A%2A, method: post, params: {:wt=>:ruby}, query: wt=ruby, headers: {"Content-Type"=>"application/x-www-form-urlencoded; charset=UTF-8"}, path: select, uri: http://localhost:8982/solr/select?wt=ruby, open_timeout: , read_timeout: } ] 
    Article Load (0.4ms) SELECT "articles".* FROM "articles" 
    Article Load (0.4ms) SELECT "articles".* FROM "articles" WHERE "articles"."id" IN (1, 2, 3, 4, 5, 6, 7, 8) 
Rendered articles/index.html.erb within layouts/application (7.6ms) 
Completed 200 OK in 74ms (Views: 10.8ms | ActiveRecord: 0.8ms | Solr: 17.0ms) 
[2013-01-17 12:28:09] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true 

我想要的行爲是除非給出搜索詞,否則不予退還。 我試圖修改ArticlesController#指標如下:

def index 
    if params[:search] = "" 
     @search_results = [] 

    else 
     @search = Article.search do 
     fulltext params[:search] 
     end 
     @search_results = @search.results 

    end 
    @articles = Article.all 
    end 

有了這個條件,當我在搜索欄中輸入任何內容,使param[:search] = "",從Solr的,這是期望的行爲返回任何內容。

Started GET "/articles?utf8=%E2%9C%93&search=" for 127.0.0.1 at 2013-01-18 12:02:52 -0800 
    Processing by ArticlesController#index as HTML 
    Parameters: {"utf8"=>"✓", "search"=>""} 
    Article Load (0.5ms) SELECT "articles".* FROM "articles" 
Rendered articles/index.html.erb within layouts/application (11.1ms) 
Completed 200 OK in 23ms (Views: 14.1ms | ActiveRecord: 0.5ms | Solr: 0.0ms) 
[2013-01-18 12:02:52] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true 

然而,當我輸入搜索詞「紅寶石」,使param[:search] = "ruby",不返回任何結果和Solr不被所有調用。

Started GET "/articles?utf8=%E2%9C%93&search=ruby" for 127.0.0.1 at 2013-01-17 12:37:58 -0800 
    Processing by ArticlesController#index as HTML 
    Parameters: {"utf8"=>"✓", "search"=>"ruby"} 
    Article Load (0.5ms) SELECT "articles".* FROM "articles" 
Rendered articles/index.html.erb within layouts/application (11.5ms) 
Completed 200 OK in 23ms (Views: 14.5ms | ActiveRecord: 0.5ms | Solr: 0.0ms) 
[2013-01-17 12:37:58] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true 

我的條件邏輯有問題嗎?或者,我是否錯過了Solr的工作原理?謝謝!

+0

爲什麼你在進行此調用:'@articles = Article.all'? – jdl

+0

我希望有兩個實例變量可用於Article#index ...用戶輸入搜索詞時,「@ articles」顯示搜索結果的所有文章和「@ search_results」。它的工作正常,除了即使搜索字段爲空且「params [:search] =」「''填充了@搜索結果'' –

回答

1

Solr架構定義了defaultQuery,在沒有提供q參數或者其值爲空字符串時運行。

太陽黑子的defaultQuery*:*(match-all-docs查詢);此外,當fulltext被提供空字符串時,太陽黑子本身更明確地發送*:*

如果您不想在此情況下使用匹配全部文檔查詢,則最好在控制器中檢查用戶提供的查詢,並完全跳過對Solr的調用。

這實際上正是你在你的例子中所做的,所以我對於真正的問題可能還不清楚。你說,「我想要的行爲是沒有回報的,」然後,描述你的條件,「有了這個條件,什麼都不會返回。」似乎你已經解決了你自己的問題?


編輯,上澄清:

除非這是一個錯字,你可能想改變if params[:search] = ""if params[:search] == ""。請注意等式與賦值運算符。

(或者更好的是:params[:search].blank?

+0

感謝您澄清Solr默認模式。我的問題需要澄清。問題是,在檢查控制器中的用戶提供的查詢之後,如果沒有根據需要提供參數,它會完全跳過Solr;然而,當我嘗試一個應該返回結果的參數時,對Solr的調用仍然被忽略。有條件嗎?或者我誤解了Solr的一些事情?感謝您的幫助 –

+0

是'params [:search] =「」'錯字?看我的編輯。 –

+0

感謝尼克,這很簡單,因爲缺少== –