From 61d154920d158767dc2b29bf9239f74d0dcdc751 Mon Sep 17 00:00:00 2001 From: Elena ``of Valhalla'' Grandi Date: Wed, 3 Feb 2021 15:17:05 +0100 Subject: Allow to get a sorted list of all entries --- lesana/collection.py | 12 +++++++++++- lesana/command.py | 6 +++++- tests/test_collection.py | 12 ++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/lesana/collection.py b/lesana/collection.py index 9c3ea07..2ade0be 100644 --- a/lesana/collection.py +++ b/lesana/collection.py @@ -330,7 +330,10 @@ class Collection(object): for field in self.indexed_fields: queryparser.add_prefix(field['name'], field['prefix']) - query = queryparser.parse_query(querystring, self.PARSER_FLAGS) + if querystring == '*': + query = xapian.Query.MatchAll + else: + query = queryparser.parse_query(querystring, self.PARSER_FLAGS) self._enquire = xapian.Enquire(cache) self._enquire.set_query(query) @@ -373,6 +376,13 @@ class Collection(object): offset += pagesize def get_all_documents(self): + """ + Yield all documents in the collection. + + Note that the results can't be sorted, even if the collection + has a default_sort; if you need sorted values you need to use a + regular search with a query of '*' + """ cache = self._get_cache() postlist = cache.postlist("") for post in postlist: diff --git a/lesana/command.py b/lesana/command.py index cb053eb..bf86b95 100644 --- a/lesana/command.py +++ b/lesana/command.py @@ -287,7 +287,11 @@ class Search(Command): offset = self.args.offset or 0 pagesize = self.args.pagesize or 12 collection = self.collection_class(self.args.collection) - if self.args.query == ['*']: + # sorted results require a less efficient full search rather + # than being able to use the list of all documents. + if self.args.query == ['*'] and not ( + self.args.sort or collection.settings.default_sort + ): results = collection.get_all_documents() else: collection.start_search( diff --git a/tests/test_collection.py b/tests/test_collection.py index 4c95c2b..1e37589 100644 --- a/tests/test_collection.py +++ b/tests/test_collection.py @@ -391,6 +391,18 @@ class testComplexCollection(unittest.TestCase): self.assertEqual(matches[2].data['order'], 'charlie') self.assertEqual(matches[3].data['order'], 'zucchini') + def test_search_all_documents_default_sort(self): + self.collection.start_search('*') + res = self.collection.get_search_results() + matches = list(res) + self.assertEqual(len(matches), 9) + for i in range(5): + self.assertEqual(matches[i].data['order'], None) + self.assertEqual(matches[5].data['order'], 'alpha') + self.assertEqual(matches[6].data['order'], 'charlie') + self.assertEqual(matches[7].data['order'], 'delta') + self.assertEqual(matches[8].data['order'], 'zucchini') + class testCollectionWithErrors(unittest.TestCase): def setUp(self): -- cgit v1.2.3