I have some trouble defining a query with Tire.
What I have:
My documents are events. They have a starts_at
date, and a ends_at
date. The ends_at
date is not required.
What I want:
I want to display upcoming events (for instance). I would define upcoming
as the following:
ends_at
date is present AND after Time.now OR
ends_at
date is not present, AND starts_at
is after Time.now Right now I'm just doing
query do
boolean do
must { range :starts_at, { gte: bod } }
end
end
How can I this OR query?
the only way I can think of right now is to use _missing_
and _exists_
in the string query:
require 'tire'
require 'active_support/core_ext/numeric/time'
require 'active_support/core_ext/date/calculations'
require 'active_support/duration'
Time::DATE_FORMATS.update :lucene => "%Y-%m-%dT%H:%M:%S%z"
Tire.index('events_stackoverflow_demo').delete
class Event
include Tire::Model::Persistence
property :title
property :starts_at, type: 'date'
property :ends_at, type: 'date'
index_name 'events_stackoverflow_demo'
end
Event.create id: 1, title: 'Test 1', starts_at: 2.days.ago, ends_at: 1.days.ago
Event.create id: 2, title: 'Test 2', starts_at: 1.day.ago, ends_at: 1.day.since
Event.create id: 3, title: 'Test 3', starts_at: 1.day.since
Event.tire.index.refresh
upcoming = Event.search do
query do
boolean minimum_number_should_match: 1 do
should { string "_exists_:ends_at AND ends_at:[#{Time.now.to_s(:lucene)} TO *]" }
should { string "_missing_:ends_at AND starts_at:[#{Time.now.to_s(:lucene)} TO *]" }
end
end
p to_curl
end.results
puts "---"
p upcoming
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.