Skip to content

Commit

Permalink
[#74] Added count to delete functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfacorro committed Oct 16, 2014
1 parent c543667 commit 14b74d0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
2 changes: 2 additions & 0 deletions examples/elastic-blog/run
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ main(_) ->
<<"It just make things easy">>,
Author),

timer:sleep(1000),

%% Let's now delete them.
true = blog:del_post(Post),
true = blog:del_author(Author),
Expand Down
23 changes: 17 additions & 6 deletions src/sumo_repo_elasticsearch.erl
Original file line number Diff line number Diff line change
Expand Up @@ -98,25 +98,36 @@ persist(Doc, #{index := Index, pool_name := PoolName} = State) ->

{ok, Doc1, State}.

delete(DocName, Id, State) ->
delete_by(DocName, [{id, Id}], State).

delete_by(DocName, Conditions, #{index := Index, pool_name := PoolName} = State) ->
delete(DocName, Id, #{index := Index, pool_name := PoolName} = State) ->
Type = atom_to_binary(DocName, utf8),
Result = case tirerl:delete_doc(PoolName, Index, Type, Id) of
{ok, _} -> 1;
_ -> 0
end,
{ok, Result, State}.

delete_by(DocName,
Conditions,
#{index := Index, pool_name := PoolName} = State) ->
Query = build_query(Conditions),
Type = atom_to_binary(DocName, utf8),

{ok, #{<<"count">> := Count}} =
tirerl:count(PoolName, Index, Type, Query, []),
{ok, _} = tirerl:delete_by_query(PoolName, Index, Type, Query, []),

{ok, 1, State}.
{ok, Count, State}.

delete_all(DocName, #{index := Index, pool_name := PoolName} = State) ->
lager:debug("deleting all: ~p", [DocName]),
Type = atom_to_binary(DocName, utf8),
MatchAll = #{query => #{match_all => #{}}},

{ok, #{<<"count">> := Count}} =
tirerl:count(PoolName, Index, Type, MatchAll, []),
{ok, _} = tirerl:delete_by_query(PoolName, Index, Type, MatchAll, []),

{ok, 1, State}.
{ok, Count, State}.

find_by(DocName, Conditions, Limit, Offset,
#{index := Index, pool_name := PoolName} = State) ->
Expand Down

0 comments on commit 14b74d0

Please sign in to comment.