Class: ParsingNesting::Tree::AndList

Inherits:
List show all
Defined in:
lib/parsing_nesting/tree.rb

Instance Attribute Summary

Attributes inherited from List

#list, #query_parser

Instance Method Summary collapse

Methods inherited from List

#initialize, #simple_pure_negative?, #to_single_query_params

Constructor Details

This class inherits a constructor from ParsingNesting::Tree::List

Instance Method Details

#can_embed?Boolean

We make an and-list embeddable only if all it’s elements are embeddable, then no problem we just embed them all as Solr ‘+’ mandatory, and achieve the AND. For now, pure negative is considered not embeddable, although theoretically it could sometimes be embedded if transformed properly.

Returns:

  • (Boolean)


210
211
212
# File 'lib/parsing_nesting/tree.rb', line 210

def can_embed?
  !simple_pure_negative? && !list.collect { |i| i.can_embed? }.include?(false)
end

#negateObject

convent logical property here, not(a AND b) === not(a) OR not(b)



249
250
251
# File 'lib/parsing_nesting/tree.rb', line 249

def negate
  OrList.new(list.collect { |n| n.negate })
end

#to_embedObject

Only if all operands are embeddable. Trick is if they were bare terms/phrases, we add a ‘+’ on front, but if they already were +/-, then we don’t need to, and leaving them along will have desired semantics. This works even on “-”, because dismax mm seems to not consider “-” clauses, they are always required regardless of mm.



220
221
222
223
224
225
226
227
228
229
# File 'lib/parsing_nesting/tree.rb', line 220

def to_embed
  list.collect do |operand|
    s = operand.to_embed
    if s =~ /^\+/ || s =~ /^\-/
      s
    else
      '+' + s
    end
  end.join(" ")
end

#to_query(local_params) ⇒ Object

for those that aren’t embeddable, or pure negative



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/parsing_nesting/tree.rb', line 232

def to_query(local_params)
  if simple_pure_negative?
    # Can do it in one single nested dismax, if we're simple arguments
    # that are pure negative.
    # build_nested_query will handle negating the pure negative for
    # us.
    build_nested_query(list, local_params)
  else
    "( " +
list.collect do |i|
  i.to_query(local_params)
end.join(" AND ") +
   " )"
  end
end