|
115
|
1 |
# -*- coding: utf-8 -*- |
|
126
|
2 |
# |
|
|
3 |
# Copyright IRI (2013) |
|
|
4 |
# |
|
|
5 |
# contact@iri.centrepompidou.fr |
|
|
6 |
# |
|
|
7 |
# This software is governed by the CeCILL-B license under French law and |
|
|
8 |
# abiding by the rules of distribution of free software. You can use, |
|
|
9 |
# modify and/ or redistribute the software under the terms of the CeCILL-B |
|
|
10 |
# license as circulated by CEA, CNRS and INRIA at the following URL |
|
|
11 |
# "http://www.cecill.info". |
|
|
12 |
# |
|
|
13 |
# As a counterpart to the access to the source code and rights to copy, |
|
|
14 |
# modify and redistribute granted by the license, users are provided only |
|
|
15 |
# with a limited warranty and the software's author, the holder of the |
|
|
16 |
# economic rights, and the successive licensors have only limited |
|
|
17 |
# liability. |
|
|
18 |
# |
|
|
19 |
# In this respect, the user's attention is drawn to the risks associated |
|
|
20 |
# with loading, using, modifying and/or developing or reproducing the |
|
|
21 |
# software by the user in light of its specific status of free software, |
|
|
22 |
# that may mean that it is complicated to manipulate, and that also |
|
|
23 |
# therefore means that it is reserved for developers and experienced |
|
|
24 |
# professionals having in-depth computer knowledge. Users are therefore |
|
|
25 |
# encouraged to load and test the software's suitability as regards their |
|
|
26 |
# requirements in conditions enabling the security of their systems and/or |
|
|
27 |
# data to be ensured and, more generally, to use and operate it in the |
|
|
28 |
# same conditions as regards security. |
|
|
29 |
# |
|
|
30 |
# The fact that you are presently reading this means that you have had |
|
|
31 |
# knowledge of the CeCILL-B license and that you accept its terms. |
|
|
32 |
# |
|
115
|
33 |
|
|
|
34 |
|
|
|
35 |
from django.conf import settings |
|
|
36 |
from haystack.query import SQ |
|
|
37 |
from whoosh.qparser import (SimpleParser, FieldsPlugin, OperatorsPlugin, |
|
|
38 |
PhrasePlugin, SingleQuotePlugin, GroupPlugin, PrefixPlugin, GtLtPlugin, |
|
|
39 |
RangePlugin) |
|
|
40 |
from whoosh.query import (Term, And, AndMaybe, Or, AndNot, Not, Phrase, Prefix, |
|
|
41 |
TermRange) |
|
|
42 |
|
|
|
43 |
HAYSTACK_DEFAULT_OPERATOR = getattr(settings,'HAYSTACK_DEFAULT_OPERATOR','AND') |
|
|
44 |
|
|
|
45 |
class QueryParser(object): |
|
|
46 |
|
|
|
47 |
|
|
|
48 |
def __init__(self, fieldname): |
|
|
49 |
''' |
|
|
50 |
Constructor |
|
|
51 |
''' |
|
|
52 |
self.w_parser = SimpleParser(fieldname, None) |
|
|
53 |
self.w_parser.add_plugin(FieldsPlugin()) |
|
|
54 |
self.w_parser.add_plugin(OperatorsPlugin()) |
|
|
55 |
self.w_parser.add_plugin(PhrasePlugin()) |
|
|
56 |
self.w_parser.add_plugin(SingleQuotePlugin()) |
|
|
57 |
self.w_parser.add_plugin(GroupPlugin()) |
|
|
58 |
self.w_parser.add_plugin(PrefixPlugin()) |
|
|
59 |
self.w_parser.add_plugin(GtLtPlugin()) |
|
|
60 |
self.w_parser.add_plugin(RangePlugin()) |
|
|
61 |
self.query = None |
|
|
62 |
self.current_node_stack = [] |
|
|
63 |
|
|
|
64 |
def parse(self, query): |
|
|
65 |
|
|
|
66 |
self.query = SQ() |
|
|
67 |
self.current_node_stack = [(self.query, HAYSTACK_DEFAULT_OPERATOR)] |
|
|
68 |
|
|
|
69 |
wquery = self.w_parser.parse(query) |
|
|
70 |
|
|
|
71 |
self.visit(wquery) |
|
|
72 |
|
|
|
73 |
if len(self.query) == 1 and isinstance(self.query.children[0], SQ): |
|
|
74 |
return self.query.children[0] |
|
|
75 |
else: |
|
|
76 |
return self.query |
|
|
77 |
|
|
|
78 |
|
|
|
79 |
def visit(self, q): |
|
|
80 |
|
|
|
81 |
if isinstance(q, Term): |
|
|
82 |
current_node, current_connector = self.current_node_stack.pop() |
|
|
83 |
current_node.add(SQ(**{q.fieldname:q.text}), current_connector) |
|
|
84 |
self.current_node_stack.append((current_node,current_connector)) |
|
|
85 |
elif isinstance(q, And): |
|
|
86 |
self._add_compound_query(q, SQ.AND) |
|
|
87 |
elif isinstance(q, AndMaybe): |
|
|
88 |
self._add_andmaybe(q) |
|
|
89 |
elif isinstance(q, Or): |
|
|
90 |
self._add_compound_query(q, SQ.OR) |
|
|
91 |
elif isinstance(q, AndNot): |
|
|
92 |
self._add_andnot(q) |
|
|
93 |
elif isinstance(q, Not): |
|
|
94 |
self._add_not(q) |
|
|
95 |
elif isinstance(q, Phrase): |
|
|
96 |
self._add_phrase(q) |
|
|
97 |
elif isinstance(q, Prefix): |
|
|
98 |
self._add_prefix(q) |
|
|
99 |
elif isinstance(q, TermRange): |
|
|
100 |
self._add_range(q) |
|
|
101 |
|
|
|
102 |
def _add_compound_query(self, q, connector): |
|
|
103 |
|
|
|
104 |
new_node = SQ() |
|
|
105 |
self.current_node_stack.append((new_node, connector)) |
|
|
106 |
for subquery in q.subqueries: |
|
|
107 |
self.visit(subquery) |
|
|
108 |
self.current_node_stack.pop() |
|
|
109 |
|
|
|
110 |
if len(new_node)==1 and isinstance(new_node.children[0], SQ) : |
|
|
111 |
new_node = new_node.children[0] |
|
|
112 |
|
|
|
113 |
current_node, current_connector = self.current_node_stack[-1] |
|
|
114 |
current_node.add(new_node, current_connector) |
|
|
115 |
|
|
|
116 |
|
|
|
117 |
def _add_andnot(self, q): |
|
|
118 |
|
|
|
119 |
new_node = SQ() |
|
|
120 |
self.current_node_stack.append((new_node, SQ.AND)) |
|
|
121 |
self.visit(q.a) |
|
|
122 |
self.visit(Not(q.b)) |
|
|
123 |
self.current_node_stack.pop() |
|
|
124 |
|
|
|
125 |
if len(new_node)==1 and isinstance(new_node.children[0], SQ) : |
|
|
126 |
new_node = new_node.children[0] |
|
|
127 |
|
|
|
128 |
current_node, current_connector = self.current_node_stack[-1] |
|
|
129 |
current_node.add(new_node, current_connector) |
|
|
130 |
|
|
|
131 |
def _add_andmaybe(self, q): |
|
|
132 |
|
|
|
133 |
new_node = SQ() |
|
|
134 |
self.current_node_stack.append((new_node, SQ.AND)) |
|
|
135 |
self.visit(q.a) |
|
|
136 |
self.visit(q.b) |
|
|
137 |
self.current_node_stack.pop() |
|
|
138 |
|
|
|
139 |
if len(new_node)==1 and isinstance(new_node.children[0], SQ) : |
|
|
140 |
new_node = new_node.children[0] |
|
|
141 |
|
|
|
142 |
current_node, current_connector = self.current_node_stack[-1] |
|
|
143 |
current_node.add(new_node, current_connector) |
|
|
144 |
|
|
|
145 |
|
|
|
146 |
def _add_not(self, q): |
|
|
147 |
|
|
|
148 |
new_node = SQ() |
|
|
149 |
self.current_node_stack.append((new_node, SQ.AND)) |
|
|
150 |
self.visit(q.query) |
|
|
151 |
self.current_node_stack.pop() |
|
|
152 |
|
|
|
153 |
if len(new_node)==1 and isinstance(new_node.children[0], SQ) : |
|
|
154 |
new_node = new_node.children[0] |
|
|
155 |
|
|
|
156 |
current_node, current_connector = self.current_node_stack[-1] |
|
|
157 |
current_node.add(~new_node, current_connector) |
|
|
158 |
|
|
|
159 |
def _add_phrase(self, q): |
|
|
160 |
new_node = SQ(**{q.fieldname+"__exact":" ".join(q.words)}) |
|
|
161 |
current_node, current_connector = self.current_node_stack[-1] |
|
|
162 |
current_node.add(new_node, current_connector) |
|
|
163 |
|
|
|
164 |
def _add_prefix(self, q): |
|
|
165 |
new_node = SQ(**{q.fieldname+"__startswith":q.text}) |
|
|
166 |
current_node, current_connector = self.current_node_stack[-1] |
|
|
167 |
current_node.add(new_node, current_connector) |
|
|
168 |
|
|
|
169 |
def _add_range(self, q): |
|
|
170 |
|
|
|
171 |
if q.start is None: |
|
|
172 |
if q.endexcl: |
|
|
173 |
postfix = "__lt" |
|
|
174 |
else: |
|
|
175 |
postfix = "__lte" |
|
|
176 |
new_node = SQ(**{q.fieldname+postfix:self.__convert_nb(q.end)}) |
|
|
177 |
elif q.end is None: |
|
|
178 |
if q.startexcl: |
|
|
179 |
postfix = "__gt" |
|
|
180 |
else: |
|
|
181 |
postfix = "__gte" |
|
|
182 |
new_node = SQ(**{q.fieldname+postfix:self.__convert_nb(q.start)}) |
|
|
183 |
else: |
|
|
184 |
new_node = SQ(**{q.fieldname+"__range":[self.__convert_nb(q.start),self.__convert_nb(q.end)]}) |
|
|
185 |
|
|
|
186 |
current_node, current_connector = self.current_node_stack[-1] |
|
|
187 |
current_node.add(new_node, current_connector) |
|
|
188 |
|
|
|
189 |
def __convert_nb(self, str_nb): |
|
|
190 |
try: |
|
|
191 |
res = int(str_nb) |
|
|
192 |
return res |
|
|
193 |
except ValueError: |
|
|
194 |
try: |
|
|
195 |
res = float(str_nb) |
|
|
196 |
return res |
|
|
197 |
except ValueError: |
|
|
198 |
return str_nb |
|
|
199 |
|
|
|
200 |
|
|
|
201 |
|