-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
query.py
95 lines (62 loc) · 2.12 KB
/
query.py
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
from __future__ import absolute_import, unicode_literals
#
# Base classes
#
class SearchQuery:
def __and__(self, other):
return And([self, other])
def __or__(self, other):
return Or([self, other])
def __invert__(self):
return Not(self)
def __repr__(self):
raise NotImplementedError
#
# Basic query classes
#
class PlainText(SearchQuery):
OPERATORS = ['and', 'or']
DEFAULT_OPERATOR = 'and'
def __init__(self, query_string: str, operator: str = DEFAULT_OPERATOR,
boost: float = 1):
self.query_string = query_string
self.operator = operator.lower()
if self.operator not in self.OPERATORS:
raise ValueError("`operator` must be either 'or' or 'and'.")
self.boost = boost
def __repr__(self):
return '<PlainText {} operator={} boost={}>'.format(repr(self.query_string), repr(self.operator), repr(self.boost))
class Phrase(SearchQuery):
def __init__(self, query_string: str):
self.query_string = query_string
def __repr__(self):
return '<PlainText {}>'.format(repr(self.query_string))
class MatchAll(SearchQuery):
def __repr__(self):
return '<MatchAll>'
class Boost(SearchQuery):
def __init__(self, subquery: SearchQuery, boost: float):
self.subquery = subquery
self.boost = boost
def __repr__(self):
return '<Boost {} boost={}>'.format(repr(self.subquery), repr(self.boost))
#
# Combinators
#
class And(SearchQuery):
def __init__(self, subqueries):
self.subqueries = subqueries
def __repr__(self):
return '<And {}>'.format(' '.join(repr(subquery) for subquery in self.subqueries))
class Or(SearchQuery):
def __init__(self, subqueries):
self.subqueries = subqueries
def __repr__(self):
return '<Or {}>'.format(' '.join(repr(subquery) for subquery in self.subqueries))
class Not(SearchQuery):
def __init__(self, subquery: SearchQuery):
self.subquery = subquery
def __repr__(self):
return '<Not {}>'.format(repr(self.subquery))
MATCH_ALL = MatchAll()
MATCH_NONE = Not(MATCH_ALL)