Class: Sensei::BoolQuery
Instance Attribute Summary
Attributes inherited from Query
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
#operands ⇒ Object
94 95 96 |
# File 'lib/sensei/query.rb', line 94 def operands [:operands] end |
#to_h ⇒ Object
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 [:operation] == :must subqueries = non_not_queries.map(&:to_h) mergeable, nonmergeable = subqueries.partition do |x| isbool = x[:bool] sameop = isbool && isbool[[:operation]] boosted = isbool && isbool[:boost] isbool && sameop && (boosted.nil? || boosted == [:boost]) end merged_queries = mergeable.map{|x| x[:bool][[: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 => { [:operation] => nonmergeable + merged_queries }.merge(get_boost).merge(not_clause) } end |