From 851914e5da250e6a38e83bccf182ab8af1db0d32 Mon Sep 17 00:00:00 2001 From: Elena ``of Valhalla'' Grandi Date: Sat, 19 Aug 2017 18:50:29 +0200 Subject: Enable wildcard searches --- lesana/collection.py | 11 ++++++++++- tests/test_collection.py | 9 +++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lesana/collection.py b/lesana/collection.py index 7c654d8..fbdb935 100644 --- a/lesana/collection.py +++ b/lesana/collection.py @@ -101,6 +101,12 @@ class Entry(object): class Collection(object): """ """ + PARSER_FLAGS = ( + xapian.QueryParser.FLAG_BOOLEAN | + xapian.QueryParser.FLAG_PHRASE | + xapian.QueryParser.FLAG_LOVEHATE | + xapian.QueryParser.FLAG_WILDCARD + ) def __init__(self, directory=None, itemdir='items'): self.basedir = directory or os.getcwd() @@ -296,11 +302,14 @@ class Collection(object): cache = self._get_cache() queryparser = xapian.QueryParser() queryparser.set_stemmer(self.stemmer) + queryparser.set_database(cache) for field in self.indexed_fields: queryparser.add_prefix(field['name'], field['prefix']) - query = queryparser.parse_query(querystring) + query = queryparser.parse_query( + querystring, + self.PARSER_FLAGS) self._enquire = xapian.Enquire(cache) self._enquire.set_query(query) diff --git a/tests/test_collection.py b/tests/test_collection.py index 875a35a..84f0a7a 100644 --- a/tests/test_collection.py +++ b/tests/test_collection.py @@ -80,6 +80,15 @@ class testCollection(unittest.TestCase): for m in matches: self.assertIsInstance(m, lesana.Entry) + def test_search_wildcard(self): + self.collection = lesana.Collection('tests/data/simple') + self.collection.start_search('Ite*') + res = self.collection.get_search_results() + matches = list(res) + self.assertEqual(len(matches), 2) + for m in matches: + self.assertIsInstance(m, lesana.Entry) + def test_search_non_init(self): self.collection = lesana.Collection('tests/data/simple') matches = list(self.collection.get_search_results()) -- cgit v1.2.3