forked from foursquare/slashem
/
Ast.scala
627 lines (591 loc) · 20.7 KB
/
Ast.scala
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
// Copyright 2011-2012 Foursquare Labs Inc. All Rights Reserved.
package com.foursquare.slashem
import org.elasticsearch.index.query.{FilterBuilder => ElasticFilterBuilder,
FilterBuilders => EFilterBuilders,
QueryBuilder => ElasticQueryBuilder,
QueryBuilders => EQueryBuilders,
QueryStringQueryBuilder}
/**
* Abstract Syntax Tree used to represent queries.
*/
object Ast {
val escapePattern = """\b(OR|AND|or|and)\b""".r
/**
* Ripped from lucene source code QueryParser.java
* http://bit.ly/AzKzV3
*
* @param q Query string to sanitize
*/
def escape(q: String): String = {
val sb = new StringBuilder()
q.trim.foreach{c: Char =>
// These characters are part of the query syntax and must be escaped
if (c == '\\' || c == '+' || c == '-' || c == '!' || c == '(' || c == ')' || c == ':'
|| c == '^' || c == '[' || c == ']' || c == '\"' || c == '{' || c == '}' || c == '~'
|| c == '*' || c == '?' || c == '|' || c == '&') {
sb.append('\\')
}
// Added (not part of QueryParser.java)
if (c != '\u2019') {
sb.append(c)
}
}
// Added (not part of QueryParser.java)
escapePattern.replaceAllIn(sb.toString, m => {"\"" + m.group(0) + "\""})
}
/**
* Return the input string in an escaped quotes
*
* @param q Query string to quote
*/
def quote(q: String): String = "\"" + q + "\""
/**
* AbstractClause represents query clauses.
*/
abstract class AbstractClause {
/**
* Returns the clause's Solr query format string representation
*/
def extend(): String
/**
* Extend the query into its elastic search format
*
* @param qf List of @see WeightedField
* @param pf List of @see PhraseWeightedField
* @param mm Option[String] representing the minimum match percentage
* @return ElasticQueryBuilder representing the clause
*/
def elasticExtend(qf: List[WeightedField],
pf: List[PhraseWeightedField],
mm: Option[String]): ElasticQueryBuilder
/**
* Creates a filter from a list of weighted fields.
*
* @param qf List of weighted fields used to build a filter list.
* @return ElasticFilterBuilder
*/
def elasticFilter(qf: List[WeightedField]): ElasticFilterBuilder = {
// By default we can just use the QueryFilterBuilder and the query extender
EFilterBuilders.queryFilter(this.elasticExtend(qf, Nil, None))
}
/**
* Generate an OrClause from a list of clauses
*
* @param clauses a list of abstract clauses to OR together
* @return OrClause inputs ORed together
*/
def or(clauses: AbstractClause*): OrClause = {
OrClause(this::clauses.toList)
}
/**
* Generate an AndClause from a list of clauses
*
* @param clauses a list of abstract clauses to AND together
* @return AbstractClause* ANDed together @see AndClause
*/
def and(clauses: AbstractClause*): AndClause = {
AndClause(this::clauses.toList)
}
}
/**
* Case class representing a list of clauses ORed together
*
* You can use a OrClause() to join two or more clauses with an OR
*/
case class OrClause(clauses: List[AbstractClause]) extends AbstractClause {
/** @inheritdoc */
def extend(): String = {
clauses.map(c => "(" + c.extend + ")").mkString(" OR ")
}
/** @inheritdoc */
def elasticExtend(qf: List[WeightedField], pf: List[PhraseWeightedField],
mm: Option[String]): ElasticQueryBuilder = {
val q = EQueryBuilders.boolQuery
clauses.map(c => c.elasticExtend(qf, pf, mm)).map(eqb => q.should(eqb))
q
}
/**
* @inheritdoc
* By default we can just use the QueryFilterBuilder and the query extender
*/
override def elasticFilter(qf: List[WeightedField]): ElasticFilterBuilder = {
EFilterBuilders.orFilter(clauses.map(_.elasticFilter(qf)):_*)
}
}
/**
* Case class representing a list of clauses ANDed together
*/
case class AndClause(clauses: List[AbstractClause]) extends AbstractClause {
/** @inheritdoc */
def extend(): String = {
clauses.map(c => "(" + c.extend() + ")").mkString(" AND ")
}
/** @inheritdoc */
def elasticExtend(qf: List[WeightedField], pf: List[PhraseWeightedField],
mm: Option[String]): ElasticQueryBuilder = {
val query = EQueryBuilders.boolQuery
// Extend the clauses and add them to the ElasticQueryBuilder
clauses.map(ac => ac.elasticExtend(qf, pf, mm)).map(eqb => query.must(eqb))
query
}
/**
* @inheritdoc
* By default we can just use the QueryFilterBuilder and the query extender
*/
override def elasticFilter(qf: List[WeightedField]): ElasticFilterBuilder = {
val filters = clauses.map(_.elasticFilter(qf))
EFilterBuilders.andFilter(filters:_*)
}
}
/**
* A 'Clause' is something of the form 'field:(query)'
* @param plus Defaults to true, used to negate queries (by setting to false).
*/
case class Clause[T](fieldName: String, query: Query[T], plus: Boolean = true) extends AbstractClause {
/** @inheritdoc */
def extend(): String = {
val (q,boost) = query match {
case Group(x) => (query,1)
case Splat() => (query,1)
case Boost(Group(x),b) => (Group(x),b)
case Boost(x,b) => (Group(x),b)
case _ => (Group(query),1)
}
// If a field does not have a name then do not attempt to specify it
val qstr = fieldName match {
case "" => q.extend()
case x => x + ":" + q.extend()
}
val booleanQuery = plus match {
case true => qstr
//This is added as a work around for the lack of support of
//pure negative queries (even though its partially supported
//now it turns out they don't work so well when nested)
case false => "(*:* -"+qstr+")"
}
//Boost as approraite
boost match {
case 1.0 => booleanQuery
case x => booleanQuery+"^"+x
}
}
/** @inheritdoc */
def elasticExtend(qf: List[WeightedField], pf: List[PhraseWeightedField],
mm: Option[String]): ElasticQueryBuilder = {
// Support extending specific fields.
val fields = fieldName match {
case "" => qf
case _ => List(WeightedField(fieldName))
}
val baseQuery = query.elasticExtend(fields, pf, mm)
plus match {
case true => baseQuery
case false => EQueryBuilders.boolQuery.mustNot(baseQuery)
}
}
/** @inheritdoc */
override def elasticFilter(qf: List[WeightedField]): ElasticFilterBuilder = {
// Support extending specific fields.
val fields = fieldName match {
case "" => qf
case _ => List(WeightedField(fieldName))
}
val baseFilters = query.elasticFilter(fields)
plus match {
case true => baseFilters
case false => EFilterBuilders.boolFilter.mustNot(baseFilters)
}
}
}
/**
* Class representing a field that can have its score boosted.
* @see ScoreBoost
*/
case class Field(fieldName: String) extends ScoreBoost {
def boost(): String = {
fieldName
}
def elasticBoost(): Pair[List[String],String] = {
Pair(Nil,"doc['" + fieldName + "'].value")
}
}
/**
* Class representing a boost (multiplier) for matches occuring in the
* given field
* @see ScoreBoost
*/
case class WeightedField(fieldName: String, weight: Double = 1) extends ScoreBoost {
def boost(): String = {
weight match {
case 1.0 => fieldName
case x: Double => fieldName + "^" + x.toString
}
}
def elasticBoost(): Pair[List[String],String] = {
weight match {
case 1.0 => Pair(Nil,"doc['" + fieldName + "'].value")
case _ => Pair(Nil,"(doc['" + fieldName + "'].value *" + weight.toString + ")")
}
}
}
/**
* A phrase weighted field. Results in a document scoring boost
* @param pf Traditional phrase query
* @param pf2 In edismax type queries two word shingle matches
* @param pf3 In edismax type queries three word shingle matches
*/
case class PhraseWeightedField(fieldName: String, weight: Double = 1,
pf: Boolean, pf2: Boolean, pf3: Boolean) {
def extend(): String = {
weight match {
case 1.0 => fieldName
case x: Double => fieldName + "^" + x.toString
}
}
}
/**
* Abstarct Query class that provides an API for common query operations.
* @define extend
* @define and
* @define boost
* @define elasticExtend
* @define elasticFilter
*/
abstract class Query[T]() {
/**
* @return String Query's Solr query format string representation
*/
def extend(): String
/**
* Combine two queries using an AND
* @see And
*/
def and(c: Query[T]): Query[T] = And(this, c)
/**
* Combine two queries using an Or
* @see Or
*/
def or(c: Query[T]): Query[T] = Or(this, c)
/**
* Boost a query by a weight
* @see Boost
*/
def boost(weight: Float): Query[T] = Boost(this, weight)
/**
* @return String Query's elastic query string representation
*/
def elasticExtend(qf: List[WeightedField], pf: List[PhraseWeightedField], mm: Option[String]): ElasticQueryBuilder
def elasticFilter(qf: List[WeightedField]): ElasticFilterBuilder = {
EFilterBuilders.queryFilter(this.elasticExtend(qf, Nil, None))
}
}
/**
* A class that provides and API for boosting the score of a field.
*/
abstract class ScoreBoost {
/**
* Solr field boost function
*/
def boost(): String
/**
* Elastic Search field boost function
* The first param is a list of of params and
* the second is a string to which scala format could be applied
*/
def elasticBoost(): Pair[List[String],String]
}
/**
* Name doesn't have to be a field name for Solr
* it could be "lat,lng". However for ES it must be
* a field
*/
case class GeoDist(name: String, lat: Double, lng: Double, distType: String = "") extends ScoreBoost {
/** @inheritdoc */
def boost(): String = {
distType match {
case "square" => "sqedist(%s,%s,%s)".format(lat,lng,name)
case _ => "dist(2,%s,%s,%s".format(lat,lng,name)
}
}
/** @inheritdoc */
def elasticBoost(): Pair[List[String],String] = {
val distanceInKm = Pair(List(lat,lng).map(_.toString),"doc['"+name+"'].distanceInKm(%s,%s)")
distType match {
case "square" => Pair(distanceInKm._1,"pow(%s,2.0)".format(distanceInKm._2))
case _ => distanceInKm
}
}
}
case class Recip(query: ScoreBoost, x: Int, y: Int, z: Int) extends ScoreBoost {
/** @inheritdoc */
def boost: String = "recip(%s,%d,%d,%d)".format(query.boost, x, y, z)
/** @inheritdoc */
def elasticBoost(): Pair[List[String],String] = {
val subquery= query.elasticBoost()
Pair(subquery._1,
"%d.0*pow(((%d.0*(%s))+%d.0),-1.0)".format(y, x, subquery._2, z))
}
}
/**
* An empty Query.
*/
case class Empty[T]() extends Query[T] {
/** @inheritdoc */
def extend(): String = "\"\""
/** @inheritdoc */
def elasticExtend(qf: List[WeightedField], pf: List[PhraseWeightedField], mm: Option[String]): ElasticQueryBuilder = {
//An empty query matches no documents, so it is the same as the negation of matchAll
//Note: this is kind of ugly since this is may likely an OR clause or negated up above
//so we should try and avoid generating this
val q = EQueryBuilders.boolQuery.mustNot(EQueryBuilders.matchAllQuery())
q
}
}
/**
* A phrase containing a query that is optionally escaped.
*
* Represents a contiguous series of words to be matched in that order.
*/
case class Phrase[T](query: T, escapeQuery: Boolean = true) extends Query[T] {
/** @inheritdoc */
def extend(): String = {
if (escapeQuery) {
'"' + escape(query.toString) + '"'
} else {
'"' + query.toString + '"'
}
}
/** @inheritdoc */
def elasticExtend(qf: List[WeightedField], pf: List[PhraseWeightedField], mm: Option[String]): ElasticQueryBuilder = {
val q = EQueryBuilders.queryString(this.extend())
qf.map(f => q.field(f.fieldName, f.weight.toFloat))
q
}
}
/**
* A Phrase Prefix.
* @see Phrase
*/
case class PhrasePrefix[T](query: T, escapeQuery: Boolean = true) extends Query[T] {
/** @inheritdoc */
def extend(): String = {
if (escapeQuery) {
'"' + escape(query.toString) + '*' + '"'
} else {
'"' + query.toString + '*' + '"'
}
}
/** @inheritdoc */
def elasticExtend(qf: List[WeightedField], pf: List[PhraseWeightedField], mm: Option[String]): ElasticQueryBuilder = {
val q = EQueryBuilders.disMaxQuery()
q.tieBreaker(1)
qf.map(f => {
val basePhrase = EQueryBuilders.textPhraseQuery(f.fieldName, this.extend())
val phraseQuery = f.weight match {
case 1.0 => basePhrase
case _ => basePhrase.boost(f.weight.toFloat)
}
q.add(phraseQuery)
})
q
}
}
/**
* A term query. Used for queries that don't need to be analyzed
*
* By default, elasticFilter() will always be cached!
*/
case class Term[T](query: Iterable[T], escapeQuery: Boolean = true, cached: Boolean = true) extends Query[T] {
/** @inheritdoc */
//def extend() = throw new UnimplementedException("Slashem does not support Term queries Solr")
def extend(): String = {
escapeQuery match {
// hack to fix wrapping the queries in a List()
case true => {
val queries = query.map(q => {'"' + escape(q.toString) + '"'})
queries.mkString(" OR ")
}
case false => '"' + query.mkString(" OR ") + '"'
}
}
/** @inheritdoc */
def elasticExtend(qf: List[WeightedField], pf: List[PhraseWeightedField], mm: Option[String]): ElasticQueryBuilder = {
val fieldName = qf.head.fieldName
val weight = qf.head.weight.toFloat
query match {
case term::Nil => EQueryBuilders.termQuery(fieldName, term).boost(weight)
case terms => {
val moarTerms = terms.toSeq.map(_.toString)
EQueryBuilders.termsQuery(fieldName, moarTerms: _*).boost(weight)
}
}
}
/** @inheritdoc */
override def elasticFilter(qf: List[WeightedField]): ElasticFilterBuilder = {
val fieldName = qf.head.fieldName
query match {
case term::Nil => EFilterBuilders.termFilter(fieldName, term).cache(cached)
case terms => {
val moarTerms = terms.toSeq.map(_.toString)
EFilterBuilders.termsFilter(fieldName, moarTerms: _*).cache(cached)
}
}
}
}
case class Range[T](q1: Query[T],q2: Query[T]) extends Query[T] {
/** @inheritdoc */
def extend(): String = {'[' + q1.extend() + " TO " + q2.extend() +']'}
/** @inheritdoc */
def elasticExtend(qf: List[WeightedField], pf: List[PhraseWeightedField], mm: Option[String]): ElasticQueryBuilder = {
val q = EQueryBuilders.rangeQuery(qf.head.fieldName)
q.from(q1)
q.to(q2)
}
//By default we can just use the QueryFilterBuilder and the query extender
override def elasticFilter(qf: List[WeightedField]): ElasticFilterBuilder = {
val q = EFilterBuilders.rangeFilter(qf.head.fieldName)
q.from(q1)
q.to(q2)
}
}
/**
* A class representing a Bag of words style query
*/
case class BagOfWords[T](query: T, escapeQuery: Boolean = true) extends Query[T] {
/** @inheritdoc */
def extend(): String = {
if (escapeQuery) {
escape(query.toString)
} else {
query.toString
}
}
/**
* @inheritdoc
*
* We take a look at the list of fields being queried
* and the list of phrase boost fields and generate
* corresponding phrase boost queries.
*/
def elasticExtend(qf: List[WeightedField], pf: List[PhraseWeightedField], mm: Option[String]): ElasticQueryBuilder = {
val normalq = EQueryBuilders.queryString(this.extend())
// If we are matching 100% then set operation to "and"
mm match {
case Some("100%") => normalq.defaultOperator(QueryStringQueryBuilder.Operator.AND)
case _ => {}
}
// Update the normal query with the query fields' names and their weights
qf.map(f => normalq.field(f.fieldName, f.weight.toFloat))
val qfnames: Set[String] = qf.map(_.fieldName).toSet
val queriesToGen = pf.filter(pwf => {qfnames contains pwf.fieldName})
queriesToGen match {
case Nil => normalq
case _ => {
val q = EQueryBuilders.disMaxQuery
q.tieBreaker(1)
q.add(normalq)
queriesToGen.map(pwf => {
val basePhrase = EQueryBuilders.textPhraseQuery(pwf.fieldName, this.extend())
val phraseQuery = pwf.weight match {
case 1 => basePhrase
case _ => basePhrase.boost(pwf.weight.toFloat)
}
q.add(phraseQuery)
})
q
}
}
}
}
/**
* Class representing a semantic grouping of Queries
*/
case class Group[T](items: Query[T]) extends Query[T] {
/** @inheritdoc */
def extend(): String = {"(%s)".format(items.extend)}
/** @inheritdoc */
def elasticExtend(qf: List[WeightedField], pf: List[PhraseWeightedField], mm: Option[String]): ElasticQueryBuilder = {
items.elasticExtend(qf, pf, mm)
}
//By default we can just use the QueryFilterBuilder and the query extender
override def elasticFilter(qf: List[WeightedField]): ElasticFilterBuilder = {
items.elasticFilter(qf)
}
}
/**
* Class representing queries ANDed together
*/
case class And[T](queries: Query[T]*) extends Query[T] {
/** @inheritdoc */
def extend(): String = {
"(" + queries.map(c => c.extend()).mkString(" AND ") + ")"
}
/** @inheritdoc */
def elasticExtend(qf: List[WeightedField], pf: List[PhraseWeightedField], mm: Option[String]): ElasticQueryBuilder = {
val eqb = EQueryBuilders.boolQuery
// Extend the queries and add them to the ElasticQueryBuilder
queries.map(q => q.elasticExtend(qf, pf, mm)).map(qb => eqb.must(qb))
eqb
}
//By default we can just use the QueryFilterBuilder and the query extender
override def elasticFilter(qf: List[WeightedField]): ElasticFilterBuilder = {
EFilterBuilders.andFilter(queries.map(_.elasticFilter(qf)):_*)
}
}
/**
* Case class representing a list of queries ORed together
*/
case class Or[T](queries: Query[T]*) extends Query[T] {
/** @inheritdoc */
def extend(): String = {
queries.map(c => c.extend()).mkString(" OR ")
}
/** @inheritdoc */
def elasticExtend(qf: List[WeightedField], pf: List[PhraseWeightedField], mm: Option[String]): ElasticQueryBuilder = {
val query = EQueryBuilders.boolQuery
queries.map(q => q.elasticExtend(qf, pf, mm)).map(q => query.should(q))
query
}
//By default we can just use the QueryFilterBuilder and the query extender
override def elasticFilter(qf: List[WeightedField]): ElasticFilterBuilder = {
EFilterBuilders.orFilter(queries.map(_.elasticFilter(qf)):_*)
}
}
case class Splat[T]() extends Query[T] {
/** @inheritdoc */
def extend(): String = "*"
/** @inheritdoc */
//Is there a better way to do this?
def elasticExtend(qf: List[WeightedField], pf: List[PhraseWeightedField], mm: Option[String]): ElasticQueryBuilder = {
println("extending splat query with "+qf)
//So we have a special case, if we are searching against the
//"_all" field we can just construct a ninja query
if (qf.map(_.fieldName).contains("_all")) {
println("pandas")
EQueryBuilders.matchAllQuery()
} else {
val q = EQueryBuilders.queryString(this.extend())
qf.map(f => q.field(f.fieldName, f.weight.toFloat))
q
}
}
}
/**
* Class representing a Query boosted by a weight.
*/
case class Boost[T](q: Query[T], weight: Float) extends Query[T] {
/** @inheritdoc */
def extend(): String = q.extend() + "^" + weight.toString
/** @inheritdoc */
def elasticExtend(qf: List[WeightedField], pf: List[PhraseWeightedField], mm: Option[String]): ElasticQueryBuilder = {
val boostedQuery = EQueryBuilders.boostingQuery
if (weight > 0) {
boostedQuery.positive(q.elasticExtend(qf, pf, mm))
} else {
boostedQuery.negative(q.elasticExtend(qf, pf, mm))
}
boostedQuery.boost(weight.abs)
boostedQuery
}
}
}