【问题标题】:Sunspot/Solr : Searching with multiple fieldsSunspot/Solr : 搜索多个字段
【发布时间】:2011-10-30 20:35:36
【问题描述】:

在多个字段中使用 Sunspot/Solr 的方法是什么? 使用简单的表单即可正常工作,如此截屏视频中所述:http://railscasts.com/episodes/278-search-with-sunspot

你可以看到我的草稿不起作用:

product.rb

searchable do
  text :address
  text :model
  text :category
end

*products_controller.rb*

  def search
    @search = Sunspot.search(Product) do
      fulltext params[:address]
        with(:model, params[:model]) if params[:model].present?
        with(:category, params[:category]) if params[:category].present?
    end
    @products = @search.results
  end

products/search.html.erb

  <%= form_tag products_search_path, :method => :get do %>
    <div class="field">
        <%= label_tag "Address ?" %>
        <%= select_tag :address, "<option>he</option><option>ho</option>".html_safe %>
    </div>
    <div class="field">
        <%= label_tag "Model ?" %>
        <%= text_field_tag :model, params[:model] %>
        <%#= select_tag :model, "<option>hi</option><option>ha</option>".html_safe %>
    </div>
    <div class="field">
        <%= label_tag "Category ?" %>
        <%= text_field_tag :category, params[:category] %>
    </div>
    <div id="buttonSearch"><%= submit_tag "Search" %></div>
  <% end %>

错误:

Sunspot::UnrecognizedFieldError (No field configured for Product with name 'model'):
  app/controllers/products_controller.rb:99:in `block in search'
  app/controllers/products_controller.rb:97:in `search'

感谢大家的帮助!

【问题讨论】:

    标签: ruby-on-rails ruby solr sunspot


    【解决方案1】:

    您只想要这个(如下)...您只想要全文,它将搜索您在可搜索中定义的所有字段...我使用“s”作为参数名称

    @search = Sunspot.search(Product) do
      fulltext(params[:s])
    end
    

    【讨论】:

    • 这不符合@remink 的要求,即按:model:category 确定查询范围。
    【解决方案2】:

    如果您像这样设置product.rb(将:model:category 指定为可用于范围界定的非全文字符串字段),那么您的搜索代码应该逐字运行。

    # product.rb
    searchable do
      text :address
      string :model
      string :category
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-21
      • 1970-01-01
      • 2013-09-19
      • 2011-11-06
      • 1970-01-01
      • 2012-01-18
      • 1970-01-01
      相关资源
      最近更新 更多