Skip to content

Commit 7f8ced6

Browse files
committed
Merge pull request #15 from versae/master
Unicode support
2 parents 2f55227 + 17f62b4 commit 7f8ced6

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

lucenequerybuilder/query.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# -*- coding: utf-8 -*-
2+
3+
14
class Q(object):
25
""" Q is a query builder for the lucene language."""
36

@@ -192,3 +195,37 @@ def __str__(self):
192195
if self.field is not None:
193196
rv = '{0}:({1})'.format(self.field, rv)
194197
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

Comments
 (0)