Class: PhraseParser::Query

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

Overview

Query object

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(clauses) ⇒ Query

Returns a new instance of Query.



74
75
76
77
78
79
# File 'lib/doing/phrase_parser.rb', line 74

def initialize(clauses)
  grouped = clauses.chunk(&:operator).to_h
  self.should_clauses = grouped.fetch(:should, [])
  self.must_not_clauses = grouped.fetch(:must_not, [])
  self.must_clauses = grouped.fetch(:must, [])
end

Instance Attribute Details

#must_clausesObject

Returns the value of attribute must_clauses.



72
73
74
# File 'lib/doing/phrase_parser.rb', line 72

def must_clauses
  @must_clauses
end

#must_not_clausesObject

Returns the value of attribute must_not_clauses.



72
73
74
# File 'lib/doing/phrase_parser.rb', line 72

def must_not_clauses
  @must_not_clauses
end

#should_clausesObject

Returns the value of attribute should_clauses.



72
73
74
# File 'lib/doing/phrase_parser.rb', line 72

def should_clauses
  @should_clauses
end

Instance Method Details

#clause_to_query(clause) ⇒ Object



105
106
107
108
109
110
111
112
113
114
# File 'lib/doing/phrase_parser.rb', line 105

def clause_to_query(clause)
  case clause
  when TermClause
    match(clause.term)
  when PhraseClause
    match_phrase(clause.phrase)
  else
    raise "Unknown clause type: #{clause}"
  end
end

#match(term) ⇒ Object



116
117
118
# File 'lib/doing/phrase_parser.rb', line 116

def match(term)
  term
end

#match_phrase(phrase) ⇒ Object



120
121
122
# File 'lib/doing/phrase_parser.rb', line 120

def match_phrase(phrase)
  phrase
end

#to_elasticsearchObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/doing/phrase_parser.rb', line 81

def to_elasticsearch
  query = {}

  if should_clauses.any?
    query[:should] = should_clauses.map do |clause|
      clause_to_query(clause)
    end
  end

  if must_clauses.any?
    query[:must] = must_clauses.map do |clause|
      clause_to_query(clause)
    end
  end

  if must_not_clauses.any?
    query[:must_not] = must_not_clauses.map do |clause|
      clause_to_query(clause)
    end
  end

  query
end