You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ro...@apache.org on 2015/02/03 16:13:54 UTC

[47/50] [abbrv] couchdb-mango git commit: Fix text sort desc

Fix text sort desc

Call append_sort_type on field name before appending "-" when the sort
is descending.

BugzId: 43531


Project: http://git-wip-us.apache.org/repos/asf/couchdb-mango/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-mango/commit/e57dcc5e
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-mango/tree/e57dcc5e
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-mango/diff/e57dcc5e

Branch: refs/heads/master
Commit: e57dcc5e1de9f1e52cc1ab5693419a4fa6721eb0
Parents: 801857d
Author: Tony Sun <to...@cloudant.com>
Authored: Mon Jan 19 13:14:26 2015 -0800
Committer: Tony Sun <to...@cloudant.com>
Committed: Wed Jan 21 11:59:12 2015 -0800

----------------------------------------------------------------------
 src/mango_cursor_text.erl | 16 +++++++++++-----
 test/09-text-sort-test.py | 11 +++++++++++
 2 files changed, 22 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/e57dcc5e/src/mango_cursor_text.erl
----------------------------------------------------------------------
diff --git a/src/mango_cursor_text.erl b/src/mango_cursor_text.erl
index 7c1b992..c774c82 100644
--- a/src/mango_cursor_text.erl
+++ b/src/mango_cursor_text.erl
@@ -204,12 +204,18 @@ apply_user_fun(CAcc, Doc) ->
 sort_query(Opts, Selector) ->
     {sort, {Sort}} = lists:keyfind(sort, 1, Opts),
     SortList = lists:map(fun(SortField) ->
-        RawSortField = case SortField of
-            {Field, <<"asc">>} -> Field;
-            {Field, <<"desc">>} -> <<"-", Field/binary>>;
-            Field when is_binary(Field) -> Field
+        {Dir, RawSortField}  = case SortField of
+            {Field, <<"asc">>} -> {asc, Field};
+            {Field, <<"desc">>} -> {desc, Field};
+            Field when is_binary(Field) -> {asc, Field}
         end,
-        mango_selector_text:append_sort_type(RawSortField, Selector)
+        SField = mango_selector_text:append_sort_type(RawSortField, Selector),
+        case Dir of
+            asc ->
+                SField;
+            desc ->
+                <<"-", SField/binary>>
+        end
     end, Sort),
     case SortList of
         [] -> relevance;

http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/e57dcc5e/test/09-text-sort-test.py
----------------------------------------------------------------------
diff --git a/test/09-text-sort-test.py b/test/09-text-sort-test.py
index d2bd454..b77ca9a 100644
--- a/test/09-text-sort-test.py
+++ b/test/09-text-sort-test.py
@@ -21,6 +21,17 @@ class SortTests(mango.UserDocsTextTests):
         assert len(docs) == 15
         assert docs[0]["age"] == 22
 
+    def test_number_sort_desc(self):
+        q = {"age": {"$gt": 0}}
+        docs = self.db.find(q, sort=[{"age": "desc"}])
+        assert len(docs) == 15
+        assert docs[0]["age"] == 79
+
+        q = {"manager": True}
+        docs = self.db.find(q, sort=[{"age:number": "desc"}])
+        assert len(docs) == 11
+        assert docs[0]["age"] == 79
+
     def test_string_sort(self):
         q = {"email": {"$gt": None}}
         docs = self.db.find(q, sort=["email:string"])