· 1 min read
Screencast: Suchen mit Sunspot
Downloads in verschiedenen Formaten:
Resourcen:
bash
[bash] bundle rails g sunspot_rails:install rake sunspot:solr:start rake sunspot:reindex [/bash]
Gemfile
[ruby] gem ‘sunspot_rails’ [/ruby]
models/article.rb
[ruby] searchable do text :name, :boost => 5 text :content, :publish_month text :comments do comments.map(&:content) end time :published_at string :publish_month end
def publish_month published_at.strftime(“%B %Y”) end [/ruby]
articles_controller.rb
[ruby] def index @search = Article.search do fulltext params[:search] with(:published_at).less_than(Time.zone.now) facet(:publish_month) with(:publish_month, params[:month]) if params[:month].present? end @articles = @search.results end [/ruby]
articles/index.html.erb
[html] <%= form_tag articles_path, :method => :get do %>
<%= text_field_tag :search, params[:search] %> <%= submit_tag “Search”, :name => nil %>
<% end %>Published
- <% for row in @search.facet(:publish\_month).rows %>
- <% if params\[:month\].blank? %> <%= link\_to row.value, :month => row.value %> (<%= row.count %>) <% else %> <%= row.value %> (<%= link\_to "remove", :month => nil %>) <% end %> <% end %>