Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use text prefix in regex to speed up query #4776

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/mango/src/mango_idx_view.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
-include("mango.hrl").
-include("mango_idx_view.hrl").

-define(PREFIX_RE, "^\\w+").

validate_new(#idx{} = Idx, _Db) ->
{ok, Def} = do_validate(Idx#idx.def),
{ok, Idx#idx{def = Def}}.
Expand Down Expand Up @@ -310,6 +312,8 @@ indexable({[{<<"$gte">>, _}]}) ->
% Making `$exists` indexable should not cause problems in other cases.
indexable({[{<<"$exists">>, _}]}) ->
true;
indexable({[{<<"$regex">>, _}]}) ->
true;
% All other operators are currently not indexable.
% This is also a subtle assertion that we don't
% call indexable/1 on a field name.
Expand Down Expand Up @@ -485,6 +489,14 @@ range({[{<<"$gt">>, Arg}]}, LCmp, Low, HCmp, High) ->
max ->
empty
end;
% use any text prefix in the regex to narrow the query
range({[{<<"$regex">>, Arg}]}, LCmp, Low, HCmp, High) ->
case re:run(Arg, ?PREFIX_RE, [{capture, first, binary}]) of
{match, [Prefix]} ->
{'$gte', Prefix, '$lte', <<Prefix/binary, 16#10FFFF>>};
nomatch ->
{LCmp, Low, HCmp, High}
end;
% There's some other un-indexable restriction on the index
% that will be applied as a post-filter. Ignore it and
% carry on our merry way.
Expand Down