2012-03-19 14 views
1

私はTireでクエリを定義する際に問題があります。ElasticSearch + Tyire:ORクエリ

私は何を持っている:

マイドキュメントイベントです。彼らはstarts_atの日付とends_atの日付を持っています。 ends_atの日付は必須ではありません。私が欲しいもの

は、私が(例えば)今後のイベントを表示したいです。

  • ends_at日付が存在し、Time.now後

OR

  • ends_at日付は存在せず、starts_atはTime.now
  • 後です:私は、次のよう upcoming定義します

今すぐやっています

query do 
    boolean do 
     must { range :starts_at, { gte: bod } } 
    end 
end 

このORクエリはどのようにすればよいですか?

答えて

3

私は今考えることができる唯一の方法は、文字列のクエリで_missing__exists_を使用することです:

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 
+0

グレート、答えるために時間を割いていただきありがとうございます:)私は今夜を試してみましょう。 – Robin

+0

'starts_at:[#{Time.now.to_s(:lucene)} TO#{100.days.since.to_s(:lucene)}]'を定義するより良い方法がありますか?これは範囲クエリに変換されますか? 'FROM'を使うことはできますか? 100.daysは任意です。 – Robin

+0

私はそれがルーネンの構文であることに気がつきました。次のリンクによれば、それは可能ではないようです。(http://lucene.apache.org/core/old_versioned_docs/versions/3_0_0/queryparsersyntax.html#Range Search – Robin