class Sensei::BoolQuery

Public Instance Methods

operands() click to toggle source
# File lib/sensei/query.rb, line 94
def operands
  options[:operands]
end
to_h() click to toggle source
# File lib/sensei/query.rb, line 98
def to_h
  if self.not_query?
    raise Exception, "Error: independent boolean NOT query not allowed."
  end

  not_queries, non_not_queries = operands.partition(&:not_query?)
  not_queries = not_queries.map{|x| x.operands.map(&:to_h)}.flatten

  non_not_queries = non_not_queries.reject{|x| x.is_a? AllQuery} if options[:operation] == :must

  subqueries = non_not_queries.map(&:to_h)
  mergeable, nonmergeable = subqueries.partition do |x|
    isbool = x[:bool]
    sameop = isbool && isbool[options[:operation]]
    boosted = isbool && isbool[:boost]
    isbool && sameop && (boosted.nil? || boosted == options[:boost])
  end
  merged_queries = mergeable.map{|x| x[:bool][options[:operation]]}.flatten(1)
  merged_nots = mergeable.map{|x| x[:bool][:must_not] || []}.flatten(1)

  all_nots = merged_nots + not_queries
  not_clause = (all_nots.count > 0 ? {:must_not => all_nots} : {})

  {:bool => {
      options[:operation] => nonmergeable + merged_queries
    }.merge(get_boost).merge(not_clause)
  }
end