summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAurélien Bompard <aurelien@bompard.org>2013-06-11 15:42:15 +0200
committerAurélien Bompard <aurelien@bompard.org>2013-06-11 15:42:15 +0200
commita76d36799e25e3e37b6900bfe981e63d4ef6c931 (patch)
tree64b41ee80be52ca5553867376ae93ab1442dcc10
parent0ca7dbe76738095c3597a0c30f8f0682ec0e48e4 (diff)
downloadkittystore-a76d36799e25e3e37b6900bfe981e63d4ef6c931.tar.gz
kittystore-a76d36799e25e3e37b6900bfe981e63d4ef6c931.tar.xz
kittystore-a76d36799e25e3e37b6900bfe981e63d4ef6c931.zip
Add a way to specify the search results order
-rw-r--r--kittystore/storm/search.py9
-rw-r--r--kittystore/storm/store.py9
2 files changed, 13 insertions, 5 deletions
diff --git a/kittystore/storm/search.py b/kittystore/storm/search.py
index 8c37747..094b7eb 100644
--- a/kittystore/storm/search.py
+++ b/kittystore/storm/search.py
@@ -83,7 +83,7 @@ class SearchEngine(object):
else:
writer.commit()
- def search(self, query, page=None, limit=10):
+ def search(self, query, page=None, limit=10, sortedby=None, reverse=False):
"""
TODO: Should the searcher be shared?
http://pythonhosted.org/Whoosh/threads.html#concurrency
@@ -94,10 +94,13 @@ class SearchEngine(object):
return_value = {"total": 0, "results": []}
with self.index.searcher() as searcher:
if page:
- results = searcher.search_page(query, page, pagelen=limit)
+ results = searcher.search_page(
+ query, page, pagelen=limit, sortedby=sortedby,
+ reverse=reverse)
return_value["total"] = results.total
else:
- results = searcher.search(query, limit=limit)
+ results = searcher.search(
+ query, limit=limit, sortedby=sortedby, reverse=reverse)
# http://pythonhosted.org/Whoosh/searching.html#results-object
if results.has_exact_length():
return_value["total"] = len(results)
diff --git a/kittystore/storm/store.py b/kittystore/storm/store.py
index 07770e8..6a5364c 100644
--- a/kittystore/storm/store.py
+++ b/kittystore/storm/store.py
@@ -252,7 +252,8 @@ class StormStore(object):
)).one()
return msg
- def search(self, query, list_name=None, page=None, limit=10):
+ def search(self, query, list_name=None, page=None, limit=10,
+ sortedby=None, reverse=False):
""" Returns a list of email containing the specified keyword in
their content.
@@ -262,10 +263,14 @@ class StormStore(object):
searched.
:param page: the page number to return. If None, don't paginate.
:param limit: the number of results per page.
+ :param sortedby: the field to sort by. If None or not specified, sort
+ by match score.
+ :param reverse: reverse the order of the results.
"""
if list_name is not None:
query += " list_name:%s" % list_name
- results = self.search_index.search(query, page, limit)
+ results = self.search_index.search(
+ query, page, limit, sortedby=sortedby, reverse=reverse)
results["results"] = [ self.get_message_by_id_from_list(
r["list_name"], r["message_id"])
for r in results["results"] ]