Exclude draft articles from Solr index with Sunspot

前端 未结 3 1760

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

class Article < ActiveRecord::Base
  searchable do
    text :         


        
3条回答
  •  难免孤独
    2021-01-13 22:30

    If you want to make sure unpublished articles are never included in the search index, you can do it this way instead:

    class Article < ActiveRecord::Base
      searchable :if => :published? do
         text :title
         text :body
      end
    end
    

    The model will then only be indexed when published.

    My approach is less interesting if you also want admins to be able to search for articles, including unpublished ones, however.

    Note: calling article.index! will add the instance to the index regardless of the :if => :method param.

提交回复
热议问题