|
35 | 35 | -include("mango.hrl").
|
36 | 36 | -include("mango_idx_view.hrl").
|
37 | 37 |
|
| 38 | +-define(PREFIX_RE, "^\\w+"). |
| 39 | + |
38 | 40 | validate_new(#idx{} = Idx, _Db) ->
|
39 | 41 | {ok, Def} = do_validate(Idx#idx.def),
|
40 | 42 | {ok, Idx#idx{def = Def}}.
|
@@ -310,6 +312,8 @@ indexable({[{<<"$gte">>, _}]}) ->
|
310 | 312 | % Making `$exists` indexable should not cause problems in other cases.
|
311 | 313 | indexable({[{<<"$exists">>, _}]}) ->
|
312 | 314 | true;
|
| 315 | +indexable({[{<<"$regex">>, _}]}) -> |
| 316 | + true; |
313 | 317 | % All other operators are currently not indexable.
|
314 | 318 | % This is also a subtle assertion that we don't
|
315 | 319 | % call indexable/1 on a field name.
|
@@ -485,6 +489,14 @@ range({[{<<"$gt">>, Arg}]}, LCmp, Low, HCmp, High) ->
|
485 | 489 | max ->
|
486 | 490 | empty
|
487 | 491 | end;
|
| 492 | +% use any text prefix in the regex to narrow the query |
| 493 | +range({[{<<"$regex">>, Arg}]}, LCmp, Low, HCmp, High) -> |
| 494 | + case re:run(Arg, ?PREFIX_RE, [{capture, first, binary}]) of |
| 495 | + {match, [Prefix]} -> |
| 496 | + {'$gte', Prefix, '$lte', <<Prefix/binary, 16#10FFFF>>}; |
| 497 | + nomatch -> |
| 498 | + {LCmp, Low, HCmp, High} |
| 499 | + end; |
488 | 500 | % There's some other un-indexable restriction on the index
|
489 | 501 | % that will be applied as a post-filter. Ignore it and
|
490 | 502 | % carry on our merry way.
|
|
0 commit comments