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
138
139
140
141
142
143
144
145
146
147
148
|
require 'parslet'
module SearchQueryParser
class QueryParser < Parslet::Parser
rule(:term) { match('[^\s"]').repeat(1).as(:term) }
rule(:quote) { str('"') }
rule(:operator) { (str('+') | str('-')).as(:operator) }
rule(:fieldname) { match('[^\s:"]').repeat(1).as(:fieldname) }
rule(:field) { (fieldname >> str(':')).as(:field) }
rule(:phrase) do
(quote >> (term >> space.maybe).repeat >> quote).as(:phrase)
end
rule(:clause) { (operator.maybe >> field.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[:field], clause[:term].to_s)
elsif clause[:phrase]
phrase = clause[:phrase].map { |p| p[:term].to_s }.join(' ')
PhraseClause.new(clause[:operator]&.to_s, clause[:field], 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, :field, :term
def initialize(operator, field, term)
self.operator = Operator.symbol(operator)
self.field = field
self.term = term
end
end
class PhraseClause
attr_accessor :operator, :field, :phrase
def initialize(operator, field, phrase)
self.operator = Operator.symbol(operator)
self.field = field
self.phrase = phrase
end
end
class Query
attr_accessor :should_clauses, :must_not_clauses, :must_clauses
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
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
def clause_to_query(clause)
case clause
when TermClause
match(clause.field, clause.term)
when PhraseClause
match_phrase(clause.field, clause.phrase)
else
raise "Unknown clause type: #{clause}"
end
end
def match(field, term)
if field
{
match: {
field[:fieldname].to_s.to_sym => {
query: term
}
}
}
else
term = ('*' + term.downcase + '*') unless term.downcase.include? '*'
term.tr!(' ', '*')
{
wildcard: {
atom: {
wildcard: term,
boost: 4
}
}
}
end
end
def match_phrase(field, phrase)
{
match_phrase: {
field ? field[:fieldname].to_s.to_sym : :name => {
query: phrase
}
}
}
end
end
end
|