diff options
author | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2021-02-03 15:17:05 +0100 |
---|---|---|
committer | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2021-02-03 15:17:05 +0100 |
commit | 61d154920d158767dc2b29bf9239f74d0dcdc751 (patch) | |
tree | a1f1d843ce5501dd2dfa994bf3bfcdd3d8166499 | |
parent | 23de3297bbd74980a2f6da326acc5da63ddef2fe (diff) |
Allow to get a sorted list of all entries
-rw-r--r-- | lesana/collection.py | 12 | ||||
-rw-r--r-- | lesana/command.py | 6 | ||||
-rw-r--r-- | 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): |