Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude draft articles from Solr index with Sunspot

I have an indexed model called Article and I don't want solr to index unpublished articles.

class Article < ActiveRecord::Base
  searchable do
    text :title
    text :body
  end
end

How can I specify that article that is not #published? should not be indexed?

like image 672
Bogdan Gusiev Avatar asked Dec 03 '22 12:12

Bogdan Gusiev


1 Answers

Be sure to index the published status.

class Article < ActiveRecord::Base
  searchable do
    text :title
    text :body
    boolean :is_published, :using => :published?
  end
end

Then add a filter to your query

Sunspot.search(Article) do |search|
  search.with(:is_published, true)
  # ...
end
like image 85
Simone Carletti Avatar answered Dec 25 '22 02:12

Simone Carletti