1
+ # -*- coding: utf-8 -*-
2
+
3
+
1
4
class Q (object ):
2
5
""" Q is a query builder for the lucene language."""
3
6
@@ -192,3 +195,37 @@ def __str__(self):
192
195
if self .field is not None :
193
196
rv = '{0}:({1})' .format (self .field , rv )
194
197
return rv
198
+
199
+
200
+ def __unicode__ (self ):
201
+ if self ._and is not None :
202
+ rv = u'(' + unicode (self ._and [0 ]) + u' AND ' + unicode (self ._and [1 ]) + u')'
203
+ elif self ._not is not None :
204
+ rv = u'NOT ' + unicode (self ._not )
205
+ elif self ._or is not None :
206
+ if self ._or [0 ].field is not None or self ._or [1 ].field is not None \
207
+ or self ._or [0 ].must or self ._or [1 ].must or self ._or [0 ].must_not \
208
+ or self ._or [1 ].must_not :
209
+ rv = unicode (self ._or [0 ]) + u' ' + unicode (self ._or [1 ])
210
+ else :
211
+ rv = u'(' + unicode (self ._or [0 ]) + u' OR ' + unicode (self ._or [1 ]) + u')'
212
+ elif self .inrange is not None :
213
+ rv = u'[' + unicode (self .inrange [0 ]) + u' TO ' + unicode (self .inrange [1 ]) + u']'
214
+ elif self .exrange is not None :
215
+ rv = u'{' + unicode (self .exrange [0 ]) + u' TO ' + unicode (self .exrange [1 ]) + u'}'
216
+ elif self .fuzzy :
217
+ rv = u'{0!s}~' .format (self .fuzzy [0 ])
218
+ if self .fuzzy [1 ] is not None :
219
+ rv += u'{0:.3f}' .format (self .fuzzy [1 ])
220
+ else :
221
+ rv = u''
222
+ for o in self .must :
223
+ rv += u'+' + unicode (o )
224
+ for o in self .must_not :
225
+ rv += unicode (o )
226
+ for o in self .should :
227
+ rv += unicode (o )
228
+
229
+ if self .field is not None :
230
+ rv = u'{0}:({1})' .format (self .field , rv )
231
+ return rv
0 commit comments