私は、Sunspot/SolrをRailsで使用する方法を学び始めました。
私はこのRailsCasthttp://railscasts.com/episodes/278-search-with-sunspotをフォローしまし た
検索クエリがなく、検索フィールドが空白の場合、SolrはデフォルトでArticleモデルからすべての結果を返します。
これが私のArticlesController#indexです
def index
@search = Article.search do
fulltext params[:search]
end
@articles = Article.all
@search_results = @search.results
end
パラメータを入力せず、検索バーに「」だけを入力すると、コンソールは次のように出力します。
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#indexを次のように変更してみました。
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
ただし、検索語「ruby」を入力すると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の動作について何かが足りませんか?ありがとう!