Skip to content

Commit

Permalink
allow to limit and/or reverse the search result set even if no sort_f…
Browse files Browse the repository at this point in the history
…ield is given. this mimics zope.catalog searchResults semantics.
  • Loading branch information
janwijbrand committed Jul 8, 2010
1 parent 1c7958b commit c6b3736
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
17 changes: 14 additions & 3 deletions src/hurry/query/query.py
Expand Up @@ -42,6 +42,9 @@ def searchResults(
return

if sort_field is not None:
# Like in zope.catalog's searchResults we require the given
# index to sort on to provide IIndexSort. We bail out if
# the index does not.
catalog_name, index_name = sort_field
catalog = getUtility(ICatalog, catalog_name, context)
index = catalog[index_name]
Expand All @@ -50,9 +53,17 @@ def searchResults(
'Index %s in catalog %s does not support '
'sorting.' % (index_name, catalog_name))
results = list(index.sort(results, limit=limit, reverse=reverse))
# Note: in case no sort_field is provided, the resultset order
# is undefined. As such limiting and reverse does not have any
# practical meaning.
else:
# There's no sort_field given. We still allow to reverse
# and/or limit the resultset. This mimics zope.catalog's
# searchResults semantics.
if reverse or limit:
results = list(results)
if reverse:
results.reverse()
if limit:
del results[limit:]

uidutil = getUtility(IIntIds, '', context)
return ResultSet(results, uidutil)

Expand Down
14 changes: 13 additions & 1 deletion src/hurry/query/query.txt
Expand Up @@ -474,7 +474,6 @@ performing any sorting here ourselves.
... r = query.searchResults(q, context, **kw)
... return [e.id for e in r]


Without using sorting in the query itself, the resultset has an undefined
order. We "manually" sort the results here to have something testable.

Expand Down Expand Up @@ -515,3 +514,16 @@ raised.
Traceback (most recent call last):
...
ValueError: Index t in catalog catalog1 does not support sorting.

The resultset can still be reversed and limited even if there's no sort_field
given (Note that the actual order of the result set when not using explicit
sorting is not defined. In this test it is assumed that the natural order of
the tested index is deterministic enough to be used as a proper test).

>>> f1 = ('catalog1', 'f1')
>>> displayResult(Eq(f1, 'a'), limit=2)
[1, 2]

>>> f1 = ('catalog1', 'f1')
>>> displayResult(Eq(f1, 'a'), limit=2, reverse=True)
[6, 2]

0 comments on commit c6b3736

Please sign in to comment.