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

Feature: build Mango indexes from dynamic expressions #3912

Open
wants to merge 6 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Replace the $explode operator with $jq
Here we replace our prototype `$explode` operator with the more general
`$jq` operator. The index is built by executing the jq expression
against each doc and storing each result into the index.
  • Loading branch information
jcoglan committed Jan 25, 2022
commit 4c27463453256366fae45f12a431d50495a7259b
23 changes: 10 additions & 13 deletions src/mango/src/mango_doc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

get_field/2,
get_field/3,
get_field_fun/2,
get_field_jq/2,
rem_field/2,
set_field/3
]).
Expand Down Expand Up @@ -416,20 +416,17 @@ get_field(Values, [Name | Rest], Validator) when is_list(Values) ->
get_field(_, [_ | _], _) ->
bad_path.

get_field_fun(Props, MangoFun) ->
{FunName, {Args}} = MangoFun,
case FunName of
<<"$explode">> -> handle_explode(Props, Args);
_ -> bad_path
get_field_jq(Props, Jq) ->
io:format("----[get_field_jq] ~p ~p~n", [Props, Jq]),
case couch_jq:compile(Jq) of
{ok, Program} ->
case couch_jq:eval(Program, Props) of
{ok, Results} -> {jq, Results};
Else -> bad_path
end;
Else -> bad_path
end.

handle_explode({Doc}, Args) ->
FieldName = proplists:get_value(<<"$field">>, Args),
Separator = proplists:get_value(<<"$separator">>, Args),
{_, FieldValue} = lists:keyfind(FieldName, 1, Doc),
R = string:split(FieldValue, Separator, all),
{fn, R}.

rem_field(Props, Field) when is_binary(Field) ->
{ok, Path} = mango_util:parse_field(Field),
rem_field(Props, Path);
Expand Down
35 changes: 13 additions & 22 deletions src/mango/src/mango_native_proc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -122,34 +122,25 @@ get_index_entries({IdxProps}, Doc) ->
case lists:member(not_found, Values) of
true -> [];
false -> case Values of
[{fn, Values1}] ->
io:format("----[get_index_entries] ~p~n", [Values1]),
[[[V], null] || V <- Values1];
[{jq, Values1}] -> [[[V], null] || V <- Values1];
_Else -> [[Values, null]]
end
end
end.

get_index_values(Fields, Doc) ->
io:format("----[get_index_values fields] ~p~n", [Fields]),
lists:map(
fun({_Field, {[MangoFun]}}) ->
case mango_doc:get_field_fun(Doc, MangoFun) of
not_found -> not_found;
bad_path -> not_found;
Value ->
io:format("----[get_field_fun result] ~p~n", [Value]),
Value
end;
({Field, _Dir}) ->
case mango_doc:get_field(Doc, Field) of
not_found -> not_found;
bad_path -> not_found;
Value -> Value
end
end,
Fields
).
lists:map(fun(Field) -> get_index_field_value(Field, Doc) end, Fields).

get_index_field_value({Field, FieldDef}, Doc) ->
Value = case FieldDef of
{[{<<"$jq">>, Jq}]} -> mango_doc:get_field_jq(Doc, Jq);
_ -> mango_doc:get_field(Doc, Field)
end,
case Value of
not_found -> not_found;
bad_path -> not_found;
Value1 -> Value1
end.

get_text_entries({IdxProps}, Doc) ->
Selector = get_index_partial_filter_selector(IdxProps),
Expand Down
2 changes: 1 addition & 1 deletion src/mango/test/22-index-function-test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def setUpClass(klass):
klass.db,
"_design/jq-split",
"jq-split-json-index",
{"bar_words": {"$explode": {"$field": "bar", "$separator": " "}}},
{"bar_words": {"$jq": '.bar | split(" ") | .[]'}},
)
klass.db.save_docs([{"_id": "example-doc", "bar": "a b c"}])

Expand Down