Date range facets with Sunspot in Ruby on Rails
You should set up your fields as trie time fields for efficient range queries:
class Event
searchable do
time :start_time, :trie => true
end
end
Then you can use query facets to facet based on ranges:
Event.search do
facet :start_time do
bod = Time.zone.now.beginning_of_day
row :today do
with :start_time, bod..(bod + 1)
end
row :tomorrow do
with :start_time, (bod + 1)..(bod + 2)
end
# etc.
end
end
The key insight here is that you can construct facets using arbitrary scopes. Note that the facet name :start_time
is just used to reference the facet in the search results, and the row labels :today
and :tomorrow
are similarly just used on the client side to identify the row counts corresponding to those queries; they have no meaning from Solr's standpoint, so you can call them whatever you want (using whatever data type you want -- they don't have to be symbols).
More information on query facets here: http://sunspot.github.com/docs/Sunspot/DSL/FieldQuery.html#facet-instance_method
Rails sunspot price range facet
You might have already found a way around this, one of the workarounds i could find for this was to use the range facet instead of the query facet. SO it would be something like :
facet :price, :range => 0..300, :range_interval => 50
with(:price, Range.new(*params[:price].first.split("..").map(&:to_i))) if params[:price].present?
Hope it helps!
How do I set up a facet search with a many to many relationship using Sunspot?
Anything you want to filter, facet, or order on, Sunspot needs to know about. So in your model:
searchable do
text :first_name, :surname
integer :skill_ids, :multiple => true, :references => Skill
end
Your #search call in your controller looks right. In your view, you'd do something along these lines:
- @search.facet(:skill_ids).rows.each do |row|
= row.instance.name
row.instance
will return the instance of Skill that the row's value refers to (that's what the :references
option is doing in the searchable
definition).
I'm not sure what you mean by "select multiple facets to search by" -- one can generate multiple facets (which give users choices for further search refinement) by calling the facet
method multiple times in a search; and you can then use their facet choices with scope restrictions using the with
method, which you can also call as many times as you'd like.
Speaking of wikis, most of this information is available (with more explanation) in the Sunspot wiki:
- http://wiki.github.com/outoftime/sunspot/
Sunspot: facets on location
The answer is simple: switch to ElasticSearch <3 http://www.elasticsearch.org/
Sunspot rails: include associated models when calling .results
Found the answer, it was actually quite simple:
Event.search(:include => [:user]) do...
Sunspot facet option for an array-type attribute
You should change your design so that you have a separate Location class and set the Job class to have has_and_belongs_to_many :locations
because it is a many-to-many relationship.
Then you can create an integer scope in your Job class searchable block for the location:
integer :locations, :multiple => true
to allow multiple locations per job.
Now it is easy to add a facet(:locations)
that will do exactly what you want.
The above works for any number of locations, not only two. However, if you don't want to create a new class/table and are sure you only have 1 or 2 locations, you can just create a query scope field:
integer :locations, :multiple => true do
if location == 'NY & Boston'
['NY', 'Boston']
else
[location]
end
end
Related Topics
Escaping the .Each { } Iteration Early in Ruby
How to Trigger Mouse Event in Capybara Test
Testing Rake Task with Rspec with Rails Environment
Require File Without Executing Code
How to Break Out of a Map/Collect and Return Whatever Has Been Collected Up to That Point
Is There a Shorthand If (Without Else) Statement in Ruby on Rails
Getting Remove_Entry_Secure Error While Using Ruby Application
Argument Out of Range Rails 4 and Bootstrap3-Datetimepicker-Rails
How to Properly Test Cancan Abilities with Rspec
Calling a Class Method Within a Class
How to Access a Variable Within a Heredoc in Ruby
Combine Thumbnails to One Large Image with Rmagick
Stub Method Only on The First Call with Rspec
Ruby, How to Add a Param to an Url That You Don't Know If It Has Any Other Param Already
What the Purpose of Bind/Unbind Methods in Ruby
Gmail Threading, Imap and Ruby