繁体   English   中英

ElasticSearch + Tire:OR查询

[英]ElasticSearch + Tire: OR query

我在使用Tire定义查询时遇到了一些麻烦。

我有的:

我的文件是事件。 它们有一个starts_at日期和一个ends_at日期。 ends_at日期不是必需的。

我想要的是:

我想显示即将发生的事件(例如)。 我将定义upcoming如下:

  • 在Time.now之后存在ends_at日期

要么

  • ends_at日期不存在,AND starts_at在Time.now之后

现在我正在做

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

我怎么能这个OR查询?

我现在能想到的唯一方法是在字符串查询中使用_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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM