You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by to...@apache.org on 2015/09/10 02:34:14 UTC
[12/26] couchdb-mango git commit: Correctly choose index type
Correctly choose index type
When users specify "$text" in the selector, json indexes should not be
used satisfy the query. We check the list of fields and look for
"$default". This implies that "$text" was used in the selector and
filter out all json indexes.
BugzId: 46498
Project: http://git-wip-us.apache.org/repos/asf/couchdb-mango/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-mango/commit/f481b566
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-mango/tree/f481b566
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-mango/diff/f481b566
Branch: refs/heads/master
Commit: f481b566881a7e7effd86bf0449b487789ad93ed
Parents: a814fe7
Author: Tony Sun <to...@cloudant.com>
Authored: Tue Aug 25 13:44:47 2015 -0700
Committer: Tony Sun <to...@cloudant.com>
Committed: Tue Aug 25 13:44:47 2015 -0700
----------------------------------------------------------------------
src/mango_idx_view.erl | 20 +++++++++++++++++++-
test/05-index-selection-test.py | 8 ++++++++
2 files changed, 27 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/f481b566/src/mango_idx_view.erl
----------------------------------------------------------------------
diff --git a/src/mango_idx_view.erl b/src/mango_idx_view.erl
index ce0b206..a313f56 100644
--- a/src/mango_idx_view.erl
+++ b/src/mango_idx_view.erl
@@ -112,7 +112,25 @@ is_usable(Idx, Selector) ->
% a member of the indexable fields of the selector.
Columns = columns(Idx),
Fields = indexable_fields(Selector),
- lists:member(hd(Columns), Fields).
+ lists:member(hd(Columns), Fields) and not is_text_search(Selector).
+
+
+is_text_search({[]}) ->
+ false;
+is_text_search({[{<<"$default">>, _}]}) ->
+ true;
+is_text_search({[{_Field, Cond}]}) when is_list(Cond) ->
+ lists:foldl(fun(C, Exists) ->
+ Exists or is_text_search(C)
+ end, false, Cond);
+is_text_search({[{_Field, Cond}]}) when is_tuple(Cond) ->
+ is_text_search(Cond);
+is_text_search({[{_Field, _Cond}]}) ->
+ false;
+%% we reached values, which should always be false
+is_text_search(Val)
+ when is_number(Val); is_boolean(Val); is_binary(Val)->
+ false.
start_key([]) ->
http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/f481b566/test/05-index-selection-test.py
----------------------------------------------------------------------
diff --git a/test/05-index-selection-test.py b/test/05-index-selection-test.py
index 856e924..5934f91 100644
--- a/test/05-index-selection-test.py
+++ b/test/05-index-selection-test.py
@@ -32,6 +32,14 @@ class IndexSelectionTests(mango.UserDocsTests):
}, explain=True)
assert resp["index"]["type"] == "json"
+ def test_with_text(self):
+ resp = self.db.find({
+ "$text" : "Stephanie",
+ "name.first": "Stephanie",
+ "name.last": "This doesn't have to match anything."
+ }, explain=True)
+ assert resp["index"]["type"] == "text"
+
@unittest.skip
def test_no_view_index(self):
resp = self.db.find({"name.first": "Ohai!"}, explain=True)