-
Notifications
You must be signed in to change notification settings - Fork 3
/
heuristic_parser.rb
167 lines (146 loc) · 4.15 KB
/
heuristic_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
require 'parslet'
module HeuristicParser
# This query parser is a simple example of going beyond generic query parsing.
# It adds a new clause type for date ranges. The parser recognizes strings
# like "1920s" or "2010" as dates instead of generic terms.
class QueryParser < Parslet::Parser
rule(:eof) { any.absent? }
rule(:decade) do
((str('1') >> str('9') |
str('2') >> str('0')) >>
match('\d') >> str('0')).as(:decade) >>
str('s').maybe >> (eof | space).present?
end
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 | decade | 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]
PhraseClause.new(clause[:operator]&.to_s, clause[:phrase].map { |p| p[:term].to_s }.join(" "))
elsif clause[:decade]
DateRangeClause.new(clause[:operator]&.to_s, Integer(clause[:decade]))
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 DateRangeClause
attr_accessor :operator, :start_year, :end_year
def initialize(operator, decade)
self.operator = Operator.symbol(operator)
self.start_year = decade
self.end_year = decade + 9
end
end
class Query
attr_accessor :should_clauses, :must_not_clauses, :must_clauses
def self.elasticsearch_query_for(query_string)
tree = QueryParser.new.parse(query_string)
query = QueryTransformer.new.apply(tree)
query.to_elasticsearch
end
def initialize(clauses)
self.should_clauses = clauses.select { |c| c.operator == :should }
self.must_not_clauses = clauses.select { |c| c.operator == :must_not }
self.must_clauses = clauses.select { |c| c.operator == :must }
end
def to_elasticsearch
query = {
:query => {
:bool => {
}
}
}
if should_clauses.any?
query[:query][:bool][:should] = should_clauses.map { |clause| clause_to_query(clause) }
end
if must_clauses.any?
query[:query][:bool][:must] = must_clauses.map { |clause| clause_to_query(clause) }
end
if must_not_clauses.any?
query[:query][:bool][:must_not] = must_not_clauses.map { |clause| clause_to_query(clause) }
end
query
end
def clause_to_query(clause)
case clause
when TermClause
match(clause.term)
when PhraseClause
match_phrase(clause.phrase)
when DateRangeClause
date_range(clause.start_year, clause.end_year)
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
def date_range(start_year, end_year)
{
:range => {
:publication_year => {
:gte => start_year,
:lte => end_year
}
}
}
end
end
end