Merge pull request #6661 from HJianBo/fix-client-fuzzy-matching-for-5

chore: client searching support \[]()* chars
This commit is contained in:
JianBo He 2022-01-11 15:03:44 +08:00 committed by GitHub
commit 19dd5a7c97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 27 deletions

View File

@ -288,28 +288,20 @@ ms(lifetime, X) ->
%% Fuzzy filter funcs
fuzzy_filter_fun(Fuzzy) ->
REFuzzy = lists:map(fun({K, like, S}) ->
{ok, RE} = re:compile(escape(S)),
{K, like, RE}
end, Fuzzy),
fun(MsRaws) when is_list(MsRaws) ->
lists:filter( fun(E) -> run_fuzzy_filter(E, REFuzzy) end
, MsRaws)
lists:filter( fun(E) -> run_fuzzy_filter(E, Fuzzy) end
, MsRaws)
end.
escape(B) when is_binary(B) ->
re:replace(B, <<"\\\\">>, <<"\\\\\\\\">>, [{return, binary}, global]).
run_fuzzy_filter(_, []) ->
true;
run_fuzzy_filter(E = {_, #{clientinfo := ClientInfo}, _},
[{Key, _, RE} | Fuzzy]) ->
Val = case maps:get(Key, ClientInfo, "") of
undefined -> "";
[{Key, like, SubStr} | Fuzzy]) ->
Val = case maps:get(Key, ClientInfo, <<>>) of
undefined -> <<>>;
V -> V
end,
re:run(Val, RE, [{capture, none}]) == match
andalso run_fuzzy_filter(E, Fuzzy).
binary:match(Val, SubStr) /= nomatch andalso run_fuzzy_filter(E, Fuzzy).
%%--------------------------------------------------------------------
%% format funcs

View File

@ -710,26 +710,19 @@ ms(created_at, X) ->
%% Match funcs
fuzzy_filter_fun(Fuzzy) ->
REFuzzy = lists:map(fun({K, like, S}) ->
{ok, RE} = re:compile(escape(S)),
{K, like, RE}
end, Fuzzy),
fun(MsRaws) when is_list(MsRaws) ->
lists:filter( fun(E) -> run_fuzzy_filter(E, REFuzzy) end
, MsRaws)
lists:filter( fun(E) -> run_fuzzy_filter(E, Fuzzy) end
, MsRaws)
end.
escape(B) when is_binary(B) ->
re:replace(B, <<"\\\\">>, <<"\\\\\\\\">>, [{return, binary}, global]).
run_fuzzy_filter(_, []) ->
true;
run_fuzzy_filter(E = {_, #{clientinfo := ClientInfo}, _}, [{Key, _, RE} | Fuzzy]) ->
Val = case maps:get(Key, ClientInfo, "") of
undefined -> "";
run_fuzzy_filter(E = {_, #{clientinfo := ClientInfo}, _}, [{Key, like, SubStr} | Fuzzy]) ->
Val = case maps:get(Key, ClientInfo, <<>>) of
undefined -> <<>>;
V -> V
end,
re:run(Val, RE, [{capture, none}]) == match andalso run_fuzzy_filter(E, Fuzzy).
binary:match(Val, SubStr) /= nomatch andalso run_fuzzy_filter(E, Fuzzy).
%%--------------------------------------------------------------------
%% format funcs