views:

105

answers:

2

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?

+2  A: 

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
Simone Carletti
In our case that will extend Solr index twice because most the articles stay drafts forever in our database. So this solution is not acceptable for us.
Bogdan Gusiev
A: 

A small look into the code base of sunspot_rails reveals a method called maybe_mark_for_auto_indexing which will be added to the models that include solr. You could override that method and set @marked_for_auto_indexing based on your criteria in the specific model. Its monkey patching but can help you solve the problem. The code for ur reference is in lib/sunspot/searchable.rb.

Kunday
Yes, It is from unreleased code. Waiting for 1.2 though.
Bogdan Gusiev