【问题标题】:Problems implementing Sunspot search through associations通过关联实现太阳黑子搜索的问题
【发布时间】:2014-06-04 21:02:41
【问题描述】:

我在实施 Sunspot 时遇到了一些问题!在过去的 2 天里,我一直在尝试让它工作,但我似乎找不到任何解决方案(尽管它无疑一直在盯着我看!)。

我希望用户能够搜索汽车制造商。创建车辆时,会通过关联为其分配制造商(如下所示)。现在..我如何允许用户在搜索字段中输入(或者更好的是所有制造商的 collection_select)并且它会过滤掉结果?

在理想的世界中,我希望将其用作我的搜索:

<%= simple_form_for :search,  url: vehicles_path , :method => :get do |f| %>
    <%= select_tag :manufacturer, options_from_collection_for_select(Manufacturer.all, :id, :name) %> 
    <%= submit_tag "Search", :name => nil %>
<% end %>

但是很遗憾,我无法在我的 Vehicle 模型中找到解决方案。

如果这对你有任何意义,我希望你能引导我走向正确的方向!

非常感谢!

class Vehicle < ActiveRecord::Base

    belongs_to :manufacturer

    searchable do
        text    :name, :registration
        integer :manufacturer_id
    end

    def to_s
        self.name
    end
end

然后

class VehiclesController < ApplicationController
  before_action :set_vehicle, only: [:show, :edit, :update, :destroy]

  def index
    @manufacturer = Manufacturer.all

    @search = Vehicle.search do
      fulltext params[:search]
    end

    @vehicles = @search.results
  end
end

class Manufacturer < ActiveRecord::Base
    has_many :vehicles

    searchable do
        text    :name
    end

    def to_s
        self.name
    end

end

车辆索引.html.erb

    <%= form_tag vehicles_path, :method => :get do %>
        <%= text_field_tag :search, params[:search] %>
        <%= submit_tag "Search", :name => nil %>
    <% end %>

还有我的车辆形态

<%= simple_nested_form_for @vehicle, :html=>{:multipart => true } do |f| %>

  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.input :name %>
    <%= f.association :manufacturer, include_blank: false %>
</div>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>

 <% end %>

为了方便起见,我删除了示例中大部分不必要的内容。

为了一笑而过,你可以看看我失败的尝试!

我也跑了这么多 rake sunspot:reindex 这太疯狂了。

    #integer :manufacturer_id, :multiple => true

    #integer :manufacturer_id, :references => Manufacturer, :multiple => true

    #integer :manufacturer_id, :references => Manufacturer, :multiple => true do
    #   manufacturers.map {|manufacturer| manufacturer.manufacturer_name}
    #end

    #text :manufacturer_names do |manufacturer|
    #  manufacturer.name { |manufacturer| manufacturer.name }
    #end 

    #integer :manufacturer_id {|manufacturer| manufacturer.name }

    #string :manufacturer_name do
    #   manufacturer.name { |manufacturer| manufacturer.name }
    #end

    #text :manufacturers do
    #   manufacturers.map { |manufacturer| manufacturer.name }
    #end

    #integer :manufacturer_id, :stored => true

【问题讨论】:

    标签: ruby-on-rails sunspot sunspot-rails sunspot-solr


    【解决方案1】:

    问题在于您在控制器中进行搜索的方式。 您说在理想世界中您想从一个选择元素中选择制造商:

    <%= simple_form_for :search,  url: vehicles_path , :method => :get do |f| %>
    <%= select_tag :manufacturer, options_from_collection_for_select(Manufacturer.all, :id, :name) %> 
    <%= submit_tag "Search", :name => nil %>
    

    如果您以这种方式定义您的搜索表单,您将在提交表单后的 URL 中有一个manufacturer 参数。

    你想在你的控制器中通过制造商的id来过滤车辆,所以你不能做全文搜索,你必须在搜索块中使用with

    class VehiclesController < ApplicationController
      def index
        @search = Vehicle.search do
          # Scope the query
          with :manufacturer_id, params[:manufacturer]
        end
    
        @vehicles = @search.results
      end
    end
    

    太阳黑子文档说:

    未定义为文本的字段(例如,整数、布尔值、时间等)可用于在执行全文匹配之前限定(限制)查询。

    您可以找到有关范围界定的更多信息:https://github.com/sunspot/sunspot#scoping-scalar-fields

    【讨论】:

      猜你喜欢
      • 2014-05-30
      • 2015-01-23
      • 2012-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-20
      相关资源
      最近更新 更多