Class: PhraseParser::Query
Overview
Query object
Instance Attribute Summary collapse
-
#must_clauses ⇒ Object
Returns the value of attribute must_clauses.
-
#must_not_clauses ⇒ Object
Returns the value of attribute must_not_clauses.
-
#should_clauses ⇒ Object
Returns the value of attribute should_clauses.
Instance Method Summary collapse
- #clause_to_query(clause) ⇒ Object
-
#initialize(clauses) ⇒ Query
constructor
A new instance of Query.
- #match(term) ⇒ Object
- #match_phrase(phrase) ⇒ Object
- #to_elasticsearch ⇒ Object
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_clauses ⇒ Object
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_clauses ⇒ Object
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_clauses ⇒ Object
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_elasticsearch ⇒ Object
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 |