在轨道上使用Sunspot / Solr查询始终相同

时间:2015-06-10 13:16:46

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

我有太阳黑子的问题。当我做SOLR请求时,我总是有相同的查询。

SOLR Request (3.7ms) [ path=select parameters={fq: ["type:Tag"], start: 0, rows: 30, q: "*:*"} ]

无论我在搜索字段中输入什么内容,它始终都是相同的查询

这是我的代码:

tag.rb

class Tag < ActiveRecord::Base
    belongs_to :pictogram
    searchable do
        text :name
    end
end

searchs_controller.rb

class SearchsController < ApplicationController
    def index
        if params[:search]
            pictograms = Array.new
            @term = params[:query]
            search = Tag.search do
                fulltext @term
            end


            index = 0
            search.results.each do |t|
                if !pictograms.include? t.pictogram
                    pictograms[index] = t.pictogram
                    index = index + 1
                end
            end

            @results = pictograms.group_by { |p| p.category }
            @search_mode = true
        end
    end
end

的index.html

<%= form_tag result_search_path do %>
    <div class="input-group input-group-lg">
        <%= search_field_tag :term,"#{@term}", class: 'form-control' %>
        <span class="input-group-btn">
            <%= submit_tag "Search", :class => 'btn btn-primary btn' %>
        </span>
    </div>
<% end %>

1 个答案:

答案 0 :(得分:0)

我认为您用于搜索的参数不正确。

<%= search_field_tag :term,"#{@term}", class: 'form-control' %>

表示您传回控制器的参数是[:term],

所以代码应该是这样的:

if params[:term].present?
            pictograms = Array.new
            @term = params[:term]
            search = Tag.search do
                fulltext params[:term]
            end

而不是params [:search]和params [:query]就像你试过的那样。