-
Notifications
You must be signed in to change notification settings - Fork 3
/
phrase_parser.rb
137 lines (120 loc) · 3.22 KB
/
phrase_parser.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
126
127
128
129
130
131
132
133
134
135
136
137
require 'parslet'
module PhraseParser
# This parser adds quoted phrases (using matched double quotes) in addition to
# terms. This is done creating multiple types of clauses instead of just one.
# A phrase clause generates an Elasticsearch match_phrase query.
class QueryParser < Parslet::Parser
rule(:term) { match('[^\s"]').repeat(1).as(:term) }
rule(:quote) { str('"') }
rule(:operator) { (str('+') | str('-')).as(:operator) }
rule(:phrase) do
(quote >> (term >> space.maybe).repeat >> quote).as(:phrase)
end
rule(:clause) { (operator.maybe >> (phrase | term)).as(:clause) }
rule(:space) { match('\s').repeat(1) }
rule(:query) { (clause >> space.maybe).repeat.as(:query) }
root(:query)
end
class QueryTransformer < Parslet::Transform
rule(:clause => subtree(:clause)) do
if clause[:term]
TermClause.new(clause[:operator]&.to_s, clause[:term].to_s)
elsif clause[:phrase]
phrase = clause[:phrase].map { |p| p[:term].to_s }.join(" ")
PhraseClause.new(clause[:operator]&.to_s, phrase)
else
raise "Unexpected clause type: '#{clause}'"
end
end
rule(:query => sequence(:clauses)) { Query.new(clauses) }
end
class Operator
def self.symbol(str)
case str
when '+'
:must
when '-'
:must_not
when nil
:should
else
raise "Unknown operator: #{str}"
end
end
end
class TermClause
attr_accessor :operator, :term
def initialize(operator, term)
self.operator = Operator.symbol(operator)
self.term = term
end
end
class PhraseClause
attr_accessor :operator, :phrase
def initialize(operator, phrase)
self.operator = Operator.symbol(operator)
self.phrase = phrase
end
end
class Query
attr_accessor :should_clauses, :must_not_clauses, :must_clauses
def initialize(clauses)
grouped = clauses.chunk { |c| c.operator }.to_h
self.should_clauses = grouped.fetch(:should, [])
self.must_not_clauses = grouped.fetch(:must_not, [])
self.must_clauses = grouped.fetch(:must, [])
end
def to_elasticsearch
query = {
:query => {
:bool => {
}
}
}
if should_clauses.any?
query[:query][:bool][:should] = should_clauses.map do |clause|
clause_to_query(clause)
end
end
if must_clauses.any?
query[:query][:bool][:must] = must_clauses.map do |clause|
clause_to_query(clause)
end
end
if must_not_clauses.any?
query[:query][:bool][:must_not] = must_not_clauses.map do |clause|
clause_to_query(clause)
end
end
query
end
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
def match(term)
{
:match => {
:title => {
:query => term
}
}
}
end
def match_phrase(phrase)
{
:match_phrase => {
:title => {
:query => phrase
}
}
}
end
end
end