Class: BooleanTermParser::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/doing/boolean_term_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(clauses) ⇒ Query

Returns a new instance of Query.



51
52
53
54
55
56
# File 'lib/doing/boolean_term_parser.rb', line 51

def initialize(clauses)
  grouped = clauses.chunk { |c| c.operator }.to_h
  self.should_terms = grouped.fetch(:should, []).map(&:term)
  self.must_not_terms = grouped.fetch(:must_not, []).map(&:term)
  self.must_terms = grouped.fetch(:must, []).map(&:term)
end

Instance Attribute Details

#must_not_termsObject

Returns the value of attribute must_not_terms.



49
50
51
# File 'lib/doing/boolean_term_parser.rb', line 49

def must_not_terms
  @must_not_terms
end

#must_termsObject

Returns the value of attribute must_terms.



49
50
51
# File 'lib/doing/boolean_term_parser.rb', line 49

def must_terms
  @must_terms
end

#should_termsObject

Returns the value of attribute should_terms.



49
50
51
# File 'lib/doing/boolean_term_parser.rb', line 49

def should_terms
  @should_terms
end

Instance Method Details

#match(term) ⇒ Object



82
83
84
# File 'lib/doing/boolean_term_parser.rb', line 82

def match(term)
  term
end

#to_elasticsearchObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/doing/boolean_term_parser.rb', line 58

def to_elasticsearch
  query = {}

  if should_terms.any?
    query[:should] = should_terms.map do |term|
      match(term)
    end
  end

  if must_terms.any?
    query[:must] = must_terms.map do |term|
      match(term)
    end
  end

  if must_not_terms.any?
    query[:must_not] = must_not_terms.map do |term|
      match(term)
    end
  end

  query
end