我有一个名为 Article 的索引模型,我不希望 solr 索引未发表的文章。
class Article < ActiveRecord::Base
searchable do
text :title
text :body
end
end
如何指定不是#published 的文章?不应该被索引吗?
我有一个名为 Article 的索引模型,我不希望 solr 索引未发表的文章。
class Article < ActiveRecord::Base
searchable do
text :title
text :body
end
end
如何指定不是#published 的文章?不应该被索引吗?
一定要索引已发布的状态。
class Article < ActiveRecord::Base
searchable do
text :title
text :body
boolean :is_published, :using => :published?
end
end
然后将过滤器添加到您的查询中
Sunspot.search(Article) do |search|
search.with(:is_published, true)
# ...
end
如果你想确保未发表的文章永远不会包含在搜索索引中,你可以这样做:
class Article < ActiveRecord::Base
searchable :if => :published? do
text :title
text :body
end
end
该模型将仅在发布时被索引。
但是,如果您还希望管理员能够搜索文章(包括未发表的文章),我的方法就不那么有趣了。
注意:article.index!
无论参数如何,调用都会将实例添加到索引中:if => :method
。
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
.