davisp commented on a change in pull request #1376: Add fields to sort based on
selector
URL: https://github.com/apache/couchdb/pull/1376#discussion_r193523326
##########
File path: src/mango/src/mango_idx.erl
##########
@@ -125,6 +126,32 @@ maybe_filter_by_sort_fields(Indexes, SortFields) ->
end.
+% This is a user experience improvement. If a selector has a sort field set,
+% an index is only valid if all the fields in the index are also specified
+% in the sort.
+
+% If a field is in the selector is constant, eg {age: {$eq: 21}} and it's
+% part of the index then we can automatically add it to the sort list because
+% it won't affect sorting.
+
+% This will then increase the likely hood of the index being choosen
+% and make it a little easier for the user.
+normalize_sort_fields(Cols, SortFields, Selector) ->
+ % Keep any fields in the sort that might not be defined in the index
+ Start = lists:subtract(SortFields, Cols),
+ lists:foldl(fun (Col, Sort) ->
+ case lists:member(Col, SortFields) of
+ true ->
+ [Col | Sort];
+ _ ->
+ case mango_selector:is_constant_field(Selector, Col) of
+ true -> [Col | Sort];
+ _ -> Sort
+ end
+ end
+ end, Start, lists:reverse(Cols)).
Review comment:
You can use lists:foldr above instead of reversing `Cols` here.
----------------------------------------------------------------
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