tonysun83 commented on a change in pull request #866: Fix incorrect index 
selection when sort specified
URL: https://github.com/apache/couchdb/pull/866#discussion_r143023000
 
 

 ##########
 File path: src/mango/src/mango_idx.erl
 ##########
 @@ -70,13 +70,33 @@ get_usable_indexes(Db, Selector0, Opts) ->
         ?MANGO_ERROR({no_usable_index, no_index_matching_name})
     end,
 
-    SortIndexes = mango_idx:for_sort(FilteredIndexes, Opts),
-    if SortIndexes /= [] -> ok; true ->
-        ?MANGO_ERROR({no_usable_index, missing_sort_index})
+    UsableFilter = fun(I) -> mango_idx:is_usable(I, Selector) end,
+    UsableIndexes0 = lists:filter(UsableFilter, FilteredIndexes),
+
+    UsableIndexes1 = case has_use_index(Opts) of
+        true ->
+            UsableFilteredIndexes = 
mango_cursor:maybe_filter_indexes_by_ddoc(UsableIndexes0, Opts),
+            if UsableFilteredIndexes /= [] -> ok; true ->
+                ?MANGO_ERROR({no_usable_index, no_usable_index_matching_name})
+            end,
+            UsableFilteredIndexes;
+        false ->
+            UsableIndexes0
     end,
 
-    UsableFilter = fun(I) -> mango_idx:is_usable(I, Selector) end,
-    lists:filter(UsableFilter, SortIndexes).
+    % If a sort was specified we have to find an index that
+    % can satisfy the request.
+    case has_sort(Opts) of
+        true ->
+            SortIndexes = mango_idx:for_sort(UsableIndexes1, Opts),
 
 Review comment:
   similar stylist change like above:
   ```
   case mango_idx:for_sort(UsableIndexes1, Opts) of
       [] -> ?MANGO_ERROR({no_usable_index, missing_sort_index}); 
       SortIndexes -> SortIndexes
   end;
   ```
 
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to