文本搜索不适用于 sunspot rails

Text search not working with sunspot rails

我目前正在使用 sunspot 实现一个基本的搜索栏。我的代码似乎有效,但搜索结果总是 returns 没有对象。我不明白为什么,在阅读了所有 sunspot 文档之后,我有点迷茫。如果有人能给我一个线索,那可能会很有帮助。 这是我的代码:

我的控制器:

class BooksController < ApplicationController
  before_action :set_book, only: [:show, :edit, :update, :destroy]
  respond_to :html

  def index
    @search = Book.search do
      fulltext(params[:search])
    end
    @books = @search.results  
   respond_with(@books)
  end
# ADDITIONAL CODE
end

我的模特:

class Book < ActiveRecord::Base
  has_many :wishs
  has_many :loans

  validates_presence_of :title, :author, :description

  enum status: [ :available, :unavailable, :on_loan ]

  searchable do
    string :title
    string :author
    string :description
  end
end

我的看法:

<div class="container">
  <div class="row">
    <h1>Listing books</h1>

    <%= form_tag books_path, :method => :get do %>
        <div class="input-group">
            <span class="input-group-btn">
              <%= submit_tag "Search", :name => nil, class: "btn btn-default" %>
            </span>
          <%= text_field_tag :search, params[:search], class: "form-control", placeholder: "Title, description, author ..." %>
        </div>
    <% end %>

    <table class="table table-hover" style="margin-top: 20px">
      <thead>
      <tr>
        <!-- NAME OF COLUMN -->
      </tr>
      </thead>

      <tbody>
      <% @books.each do |book| %>
        <!-- DO SOME STUFF -->
      <% end %>
      </tbody>
    </table>
  </div>
</div>

而不是 string 使用 text 所以它将是全文搜索。在你的模型中尝试:

searchable do
  text :title
  text :author
  text :description
end

如果该语法失败,这将起作用

searchable do
  text :title, :author, :description
end

最后,重建索引看起来已经修复了它:bundle exec rake sunspot:solr:reindex