forked from threatgrid/clj-momo
/
index.clj
114 lines (98 loc) · 3.24 KB
/
index.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
(ns clj-momo.lib.es.index
(:refer-clojure :exclude [get])
(:require [clj-http.client :as client]
[clj-momo.lib.es
[conn :refer [default-opts safe-es-read]]
[schemas :refer [ESConn]]]
[schema.core :as s]))
(s/defn index-uri :- s/Str
"make an index uri from a host and an index name"
[uri :- s/Str
index-name :- s/Str]
(format "%s/%s" uri index-name))
(s/defn template-uri :- s/Str
"make a template uri from a host and a template name"
[uri :- s/Str
template-name :- s/Str]
"make a template uri from a host and a template name"
(format "%s/_template/%s" uri template-name))
(s/defn index-exists? :- s/Bool
"check if the supplied ES index exists"
[{:keys [uri cm]} :- ESConn
index-name :- s/Str]
(not= 404
(:status (client/head (index-uri uri index-name)
(assoc default-opts
:connection-manager cm)))))
(s/defn create!
"create an index"
[{:keys [uri cm] :as conn} :- ESConn
index-name :- s/Str
settings :- s/Any]
(safe-es-read
(client/put (index-uri uri index-name)
(assoc default-opts
:form-params settings
:connection-manager cm))))
(s/defn update-settings!
"update an ES index settings"
[{:keys [uri cm] :as conn} :- ESConn
index-name :- s/Str
settings :- s/Any]
(safe-es-read
(client/put (str (index-uri uri index-name) "/_settings")
(assoc default-opts
:form-params settings
:connection-manager cm))))
(s/defn get
"get an index"
[{:keys [uri cm] :as conn} :- ESConn
index-name :- s/Str]
(safe-es-read
(client/get (index-uri uri index-name)
(assoc default-opts
:connection-manager cm))))
(s/defn delete!
"delete indexes using a wildcard"
[{:keys [uri cm] :as conn} :- ESConn
index-wildcard :- s/Str]
(safe-es-read
(client/delete (index-uri uri index-wildcard)
(assoc default-opts
:connection-manager cm))))
(s/defn create-template!
"create an index template, update if already exists"
[{:keys [uri cm]} :- ESConn
index-name :- s/Str
index-config]
(let [template (str index-name "*")
opts (assoc index-config :template template)]
(safe-es-read
(client/put (template-uri uri index-name)
(merge default-opts
{:form-params opts
:connection-manager cm})))))
(s/defn refresh!
"refresh an index"
[{:keys [uri cm]} :- ESConn
index-name :- s/Str]
(safe-es-read
(client/post (str (index-uri uri index-name) "/_refresh")
(assoc default-opts
:connection-manager cm))))
(s/defn open!
"open an index"
[{:keys [uri cm]} :- ESConn
index-name :- s/Str]
(safe-es-read
(client/post (str (index-uri uri index-name) "/_open")
(assoc default-opts
:connection-manager cm))))
(s/defn close!
"close an index"
[{:keys [uri cm]} :- ESConn
index-name :- s/Str]
(safe-es-read
(client/post (str (index-uri uri index-name) "/_close")
(assoc default-opts
:connection-manager cm))))