Class: Sensei::BoolQuery

Inherits:
Query
  • Object
show all
Defined in:
lib/sensei/query.rb

Instance Attribute Summary

Attributes inherited from Query

#options

Instance Method Summary collapse

Methods inherited from Query

construct, #get_boost, #initialize, #not_query?, q, #run, #to_sensei

Methods included from Operators

#&, #*, #boost!, #must_not, #|, #~

Constructor Details

This class inherits a constructor from Sensei::Query

Instance Method Details

#operandsObject



94
95
96
# File 'lib/sensei/query.rb', line 94

def operands
  options[:operands]
end

#to_hObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# 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