From 99b15051de083e5cfd96f9fb59c0a97fcbb1e8e6 Mon Sep 17 00:00:00 2001 From: Bohuslav Kabrda Date: Thu, 14 Feb 2013 11:49:01 +0100 Subject: [PATCH] Add some more options to search method --- flask_whooshee.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/flask_whooshee.py b/flask_whooshee.py index 7b9dfcc..a318e1d 100644 --- a/flask_whooshee.py +++ b/flask_whooshee.py @@ -13,10 +13,10 @@ class AbstractWhoosheer(object): __metaclass__ = abc.ABCMeta @classmethod - def search(cls, search_string, values_of=''): - prepped_string = cls.prep_search_string(search_string) + def search(cls, search_string, values_of='', group=whoosh.qparser.OrGroup, match_substrings=True): + prepped_string = cls.prep_search_string(search_string, match_substrings) with cls.index.searcher() as searcher: - parser = whoosh.qparser.MultifieldParser(cls.schema.names(), cls.index.schema) + parser = whoosh.qparser.MultifieldParser(cls.schema.names(), cls.index.schema, group=group) query = parser.parse(prepped_string) results = searcher.search(query) if values_of: @@ -24,14 +24,15 @@ class AbstractWhoosheer(object): return results @classmethod - def prep_search_string(cls, search_string): + def prep_search_string(cls, search_string, match_substrings): s = search_string.strip() # we don't want stars from user s = s.replace('*', '') if len(s) < cls.search_string_min_len: raise ValueError('Search string must have at least 3 characters') - # replace multiple whitechars with one space - s = u'*{0}*'.format(re.sub('[\s]+', '*', s)) + # replace multiple with star space star + if match_substrings: + s = u'*{0}*'.format(re.sub('[\s]+', '* *', s)) # TODO: some sanitization return s