2013-08-14 1 views
2

항상 어떤 결과를 반환하지 않습니다. 검색중인 텍스트가 내 콘텐츠 모델의 내 :title 또는 :text 속성에 있음을 확인했습니다. Rails4- Sunspot- 전체 텍스트 PARAMS는 [: 검색] 내가 레일 4를 사용하고 내가 내 웹 사이트에 대한 검색 엔진을 구현하기 위해 태양 흑점의 보석을 사용하려고하지만 내 검색이 항상 결과가 반환

Content.rb 모델 : application.html.haml에 위치한

class Content < ActiveRecord::Base 
    searchable do 
     string :title 
     text :text 
    end 
    validates :title, presence: true 
    validates :text, presence: true 
    has_many :links 
    validates_associated :links 
    accepts_nested_attributes_for :links 
end 

내 검색 양식 :

= form_tag search_path, :method => :get do 
%p 
    = text_field_tag :search, params[:search] 
    = submit_tag "Search" 

contents_controller .rb 컨트롤러 :

class ContentsController < ApplicationController 
    ... 
    def search 
     if params[:search] 
      @search = Content.search do 
       fulltext params[:search] 
      end 
      @query = params[:search] 
      @content = @search.results 
     else 
      @content = Content.all 
     end 
    end 
    ... 
end 

Gemfile :

... 
    gem 'sunspot_rails' 
    gem 'sunspot_solr' 
    ... 

Contents Controllerdef search 후라고 내 search.html.haml 파일 :

-if @content.empty? || @query == nil 
    %p="No results found for: #{@query}" 
-elsif [email protected]? 
    %p="Your search results for #{@query}" 
[email protected] do |c| 
    %h1.content-title=link_to removeHTML(c.title), content_path(c) 
    %p.content-text=removeHTML(c.text.split[0..25].join(" ")) + " ..." 

위의 코드에 무슨 일이 내 @content 항상 빈 것입니다. 이것은 라인 이후에 발생합니다. 나는 회선을 제거하고 @content = Content.all로 전화하면 예상대로 모든 내 콘텐츠 개체를 표시하기 때문에 이것을 확인했습니다.

사람이 @search.results 반환 nil을 왜 나를 이해하는 데 도움이 ? 그것은 아무튼 때문에 당신은 항상 빈 집합을 반환 검색 엔진을 사용하는 쿼리, 당신이 우회 SOLR으로 그 일을하고 있기 때문에 당신은 Content.all를 가져올 수

rake sunspot:solr:reindex 

: 당신이 데이터를 인덱싱 할 필요가 같은

답변

11

보인다 어떤 지표도 없다.

+0

는 정말 고마워요! 이 명령을 실행할 때, 보석에'progress_bar'를 추가하라고했습니다. 그것을 한 후에, 그것은 작동합니다! – sameetandpotatoes

+1

'searchable'블록을 변경 한 후에는 항상 다시 색인해야합니다. solr 없이는 실패하지 않지만 새로운 필드에서 데이터를 가져 오지는 않습니다. – zrl3dx

+0

알았어, 조언 주셔서 감사합니다. – sameetandpotatoes