./ct_report/coverage/mongoose_elasticsearch.COVER.html

1 %%------------------------------------------------------------------
2 %% Copyright 2018 Erlang Solutions Ltd.
3 %%
4 %% Licensed under the Apache License, Version 2.0 (the "License");
5 %% you may not use this file except in compliance with the License.
6 %% You may obtain a copy of the License at
7 %%
8 %% http://www.apache.org/licenses/LICENSE-2.0
9 %%
10 %% Unless required by applicable law or agreed to in writing, software
11 %% distributed under the License is distributed on an "AS IS" BASIS,
12 %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 %% See the License for the specific language governing permissions and
14 %% limitations under the License.
15 %%
16 %% @doc Provides functions to interact with ElasticSearch cluster
17 %% via HTTP API.
18 %%------------------------------------------------------------------
19 -module(mongoose_elasticsearch).
20
21 -export([health/0]).
22 -export([insert_document/4]).
23 -export([search/3]).
24 -export([count/3]).
25 -export([delete_by_query/3]).
26
27 -type index() :: binary().
28 -type type() :: binary().
29 -type document() :: map().
30 -type id() :: binary().
31 -type query() :: map().
32
33 -export_type([index/0]).
34 -export_type([type/0]).
35 -export_type([document/0]).
36 -export_type([id/0]).
37 -export_type([query/0]).
38
39 -ignore_xref([health/0]).
40
41 -include("mongoose.hrl").
42
43 -define(POOL_NAME, mongoose_wpool:make_pool_name(elastic, global, default)).
44
45 %%-------------------------------------------------------------------
46 %% API
47 %%-------------------------------------------------------------------
48
49 %% @doc Returns the health status of the ElasticSearch cluster.
50 %%
51 %% See https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-health.html for
52 %% more information.
53 -spec health() -> {ok, Resp :: map()} | {error, term()}.
54 health() ->
55 81 case catch tirerl:health(?POOL_NAME) of
56 {'EXIT', _} = Err ->
57 1 {error, Err};
58 Other ->
59 80 Other
60 end.
61
62 %% @doc Tries to insert a document into given ElasticSearch index.
63 %%
64 %% See https://www.elastic.co/guide/en/elasticsearch/reference/6.2/docs-index_.html for more
65 %% information.
66 -spec insert_document(index(), type(), id(), document()) -> {ok, Resp :: map()} | {error, term()}.
67 insert_document(Index, Type, Id, Document) ->
68 1415 case tirerl:insert_doc(?POOL_NAME, Index, Type, Id, Document) of
69 {ok, #{<<"_id">> := Id}} = Resp ->
70 1415 Resp;
71 {error, _} = Err ->
72
:-(
Err
73 end.
74
75 %% @doc Runs a search query on a given ElasticSearch index.
76 %%
77 %% See https://www.elastic.co/guide/en/elasticsearch/reference/6.2/search.html for more information.
78 -spec search(index(), type(), query()) -> {ok, Resp :: map()} | {error, term()}.
79 search(Index, Type, SearchQuery) ->
80 266 tirerl:search(?POOL_NAME, Index, Type, SearchQuery).
81
82 %% @doc Retrieves count of documents matching given search query in given ElasticSearch index.
83 %%
84 %% See https://www.elastic.co/guide/en/elasticsearch/reference/6.2/search-count.html for more
85 %% information.
86 -spec count(index(), type(), query()) -> {ok, Count :: non_neg_integer()} | {error, term()}.
87 count(Index, Type, SearchQuery) ->
88 516 case tirerl:count(?POOL_NAME, Index, Type, SearchQuery, []) of
89 {ok, #{<<"count">> := Count}} when is_integer(Count), Count >= 0 ->
90 516 {ok, Count};
91 {error, _} = Err ->
92
:-(
Err
93 end.
94
95 %% @doc Deletes documents matching a query in a given ElasticSearch index.
96 %%
97 %% See https://www.elastic.co/guide/en/elasticsearch/reference/6.2/docs-delete-by-query.html for
98 %% more information.
99 -spec delete_by_query(index(), type(), query()) -> ok | {error, term()}.
100 delete_by_query(Index, Type, SearchQuery) ->
101 158 case tirerl:delete_by_query(?POOL_NAME, Index, Type, SearchQuery, []) of
102 {ok, _} ->
103 158 ok;
104 {error, _} = Err ->
105
:-(
Err
106 end.
107
108 %%-------------------------------------------------------------------
109 %% Helpers
110 %%-------------------------------------------------------------------
111
Line Hits Source