1

我刚开始学习在 Rails 中使用 Sunspot / Solr。

我跟着这个 RailsCast http://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 的工作原理?谢谢!

4

1 回答 1

1

Solr 模式定义了defaultQuery在未q提供参数或其值为空字符串时运行的 a。

太阳黑子的defaultQuery*:*(匹配所有文档查询);此外,Sunspot 本身更明确地发送一个*:*whenfulltext提供了一个空字符串。

如果在这种情况下您不想要匹配所有文档的查询,最好在控制器中检查用户提供的查询并完全跳过对 Solr 的调用。

这实际上正是您在示例中所做的,所以我可能不清楚真正的问题。你说,“我想要的行为是不返回任何东西”,然后描述你的条件,“在这个条件下,什么都不返回。” 好像你已经解决了自己的问题?


编辑,澄清:

除非这是一个错字,否则您可能想要更改if params[:search] = ""if params[:search] == "". 注意相等与赋值运算符。

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

于 2013-01-18T13:36:37.987 回答