Date Range Boundaries Using Sunspot in Ruby on Rails

I use Sunspot to search for events (parties, concerts, ...) in the Ruby on Rails 3 application.

I managed to set up free text search and aspect search for several types of categories.

Now I am stuck in my next task. I want to customize the faces associated with when the event occurs.

I want to have faces that describe both relative date / time ranges , such as β€œtoday,” β€œthis weekend,” β€œnext weekend,” and absolute date and time ranges , such as β€œEaster Holiday 2011,” β€œNew Year 2012” , ... Date-time ranges sometimes overlap.

I looked at the Sunspot API documentation, the Sunspot wiki, read and downloaded articles and blogs here on Stackoverflow. People write what can be achieved, but I do not find examples or ideas of implementation that make me understand how to do this.

Any suggestions?

Since my problem is not in my code, I am not posting any code. The Event class has an instance of DateTime named "start_time". I understand that my task is to determine when the absolute date / time ranges appear on the calendar.

Yours faithfully,

./Stefana

PS I said I'm a newbie? ;-) DS

+4
source share
2 answers

You must configure your fields as time fields for efficient range queries:

class Event searchable do time :start_time, :trie => true end end 

Then you can use query facets for a range-based facet:

 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 create faces using arbitrary areas. Note that the name of the face :start_time used only to refer to the facet in the search results, and the line labels :today and :tomorrow similarly used only on the client side to determine the number of lines matching these requests; they do not make any sense from the point of view of Solr, so you can call them no matter what you want (using any data type you want - they do not have to be characters).

More information about aspects of the request here: http://sunspot.github.com/docs/Sunspot/DSL/FieldQuery.html#facet-instance_method

+19
source

You can do this:

 with(:registration_date).between(params[:start_date].to_date..params[:end_date].to_date) 

Have a look here: http://sunspot.imtqy.com/docs/Sunspot.html#search-class_method

0
source

Source: https://habr.com/ru/post/1341116/


All Articles