# Add to Gemfile:
gem 'sunspot_rails'
gem 'sunspot_solr'
# optional pre-packaged Solr distribution for use in development
#Bundle it!
bundle install
# Generate a default configuration file:
rails generate sunspot_rails:install
# If sunspot_solr was installed, start the packaged Solr distribution with:
bundle exec rake sunspot:solr:start # or sunspot:solr:run to start in foreground
Text
class Post < ActiveRecord::Base
searchable do
text :title, :body
text :comments do
comments.map { |comment| comment.body }
end
boolean :featured
integer :blog_id
integer :author_id
integer :category_ids, :multiple => true
double :average_rating
time :published_at
time :expired_at
string :sort_title do
title.downcase.gsub(/^(an?|the)/, '')
end
end
end
Text
Post.search do
fulltext 'big pizza' do
fields(:body, :title => 2.0)
phrase_fields :title => 2.0
phrase_slop 1
boost(2.0) { with(:featured, true) }
end
with :blog_id, 1
with :category_id, 5
with(:published_at).less_than Time.now
order_by :published_at, :desc
paginate :page => 2, :per_page => 15
end
# Note*:
# text fields will be full-text searchable.
# Other fields (e.g., integer and string) can be used to scope queries.
<fieldType name="text" class="solr.TextField" omitNorms="false">
<analyzer>
<tokenizer class="solr.StandardTokenizerFactory"/>
<filter class="solr.StandardFilterFactory"/>
<filter class="solr.LowerCaseFilterFactory"/>
<filter class="solr.StopFilterFactory" words="stopwords.txt" ignoreCase="true"/>
<charFilter class="solr.HTMLStripCharFilterFactory"/>
<filter class="solr.PorterStemFilterFactory"/>
</analyzer>
</fieldType>
<dynamicField name="*_text" stored="false" type="text" multiValued="true" indexed="true"/>
<solrQueryParser defaultOperator="OR"/>
score(q,d) = coord-factor(q,d) . query-boost(q) . V(q) . V(d) . doc-len-norm(d) . doc-boost(d)
____________
|V(q)|