Make true and false searches work correctly for numeric fields.

This commit is contained in:
Charles Haley 2011-04-12 22:11:43 +01:00
parent 13a484833d
commit 566fa8d80f

View File

@ -402,47 +402,55 @@ class ResultCache(SearchQueryParser): # {{{
matches = set([]) matches = set([])
if len(query) == 0: if len(query) == 0:
return matches return matches
if query == 'false':
query = '0'
elif query == 'true':
query = '!=0'
relop = None
for k in self.numeric_search_relops.keys():
if query.startswith(k):
(p, relop) = self.numeric_search_relops[k]
query = query[p:]
if relop is None:
(p, relop) = self.numeric_search_relops['=']
if val_func is None: if val_func is None:
loc = self.field_metadata[location]['rec_index'] loc = self.field_metadata[location]['rec_index']
val_func = lambda item, loc=loc: item[loc] val_func = lambda item, loc=loc: item[loc]
dt = self.field_metadata[location]['datatype'] if query == 'false':
if dt == 'int': q = ''
cast = (lambda x: int (x)) relop = lambda x,y: x is None
adjust = lambda x: x val_func = lambda item, loc=loc: item[loc]
elif dt == 'rating': cast = adjust = lambda x: x
cast = (lambda x: int (x)) elif query == 'true':
adjust = lambda x: x/2 q = ''
elif dt in ('float', 'composite'): relop = lambda x,y: x is not None
cast = lambda x : float (x) val_func = lambda item, loc=loc: item[loc]
adjust = lambda x: x cast = adjust = lambda x: x
else: # count operation
cast = (lambda x: int (x))
adjust = lambda x: x
if len(query) > 1:
mult = query[-1:].lower()
mult = {'k':1024.,'m': 1024.**2, 'g': 1024.**3}.get(mult, 1.0)
if mult != 1.0:
query = query[:-1]
else: else:
mult = 1.0 relop = None
try: for k in self.numeric_search_relops.keys():
q = cast(query) * mult if query.startswith(k):
except: (p, relop) = self.numeric_search_relops[k]
return matches query = query[p:]
if relop is None:
(p, relop) = self.numeric_search_relops['=']
dt = self.field_metadata[location]['datatype']
if dt == 'int':
cast = lambda x: int (x) if x is not None else None
adjust = lambda x: x
elif dt == 'rating':
cast = lambda x: int (x)
adjust = lambda x: x/2
elif dt in ('float', 'composite'):
cast = lambda x : float (x) if x is not None else None
adjust = lambda x: x
else: # count operation
cast = (lambda x: int (x))
adjust = lambda x: x
if len(query) > 1:
mult = query[-1:].lower()
mult = {'k':1024.,'m': 1024.**2, 'g': 1024.**3}.get(mult, 1.0)
if mult != 1.0:
query = query[:-1]
else:
mult = 1.0
try:
q = cast(query) * mult
except:
return matches
for id_ in candidates: for id_ in candidates:
item = self._data[id_] item = self._data[id_]
@ -452,9 +460,7 @@ class ResultCache(SearchQueryParser): # {{{
v = cast(val_func(item)) v = cast(val_func(item))
except: except:
v = 0 v = 0
if not v: if v:
v = 0
else:
v = adjust(v) v = adjust(v)
if relop(v, q): if relop(v, q):
matches.add(item[0]) matches.add(item[0])