/
index.clj
338 lines (273 loc) · 12.7 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team
;;
;; Licensed under the Apache License, Version 2.0 (the "License");
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;; http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.
(ns clojurewerkz.elastisch.rest.index
(:refer-clojure :exclude [flush])
(:require [clojurewerkz.elastisch.rest :as rest]
[clj-http.client :as http]
[clojurewerkz.elastisch.rest.utils :refer [join-names]])
(:import clojurewerkz.elastisch.rest.Connection))
;;
;; create, delete, exists?
;;
(defn create
"Creates an index.
Accepted options are `:mappings` and `:settings`. Both accept maps with the same structure as in the REST API.
Examples:
```clojure
(require '[clojurewerkz.elastisch.rest.index :as idx])
(idx/create conn \"myapp_development\")
(idx/create conn \"myapp_development\" :settings {\"number_of_shards\" 1})
(let [mapping-types {:person {:properties {:username {:type \"string\" :store \"yes\"}
:first-name {:type \"string\" :store \"yes\"}
:last-name {:type \"string\"}
:age {:type \"integer\"}
:title {:type \"string\" :analyzer \"snowball\"}
:planet {:type \"string\"}
:biography {:type \"string\" :analyzer \"snowball\" :term_vector \"with_positions_offsets\"}}}}]
(idx/create conn \"myapp_development\" :mappings mapping-types))
```"
([^Connection conn ^String index-name] (create conn index-name nil))
([^Connection conn ^String index-name opts]
(let [{:keys [settings mappings]} opts]
(rest/post conn (rest/index-url conn
index-name)
{:body (if mappings
{:settings settings :mappings mappings}
{:settings settings})}))))
(defn exists?
"Used to check if the index (indices) exists or not."
[^Connection conn ^String index-name]
(= 200 (:status (rest/head conn (rest/index-url conn
index-name)))))
(defn type-exists?
"Used to check if a type/types exists in an index/indices."
[^Connection conn ^String index-name ^String type-name]
(= 200 (:status (rest/head conn (rest/mapping-type-url conn
index-name type-name)))))
(defn delete
"Deletes an existing index."
([^Connection conn]
(rest/delete conn (rest/index-url conn
"_all")))
([^Connection conn ^String index-name]
(rest/delete conn (rest/index-url conn
index-name))))
;;
;; Mappings
;;
(defn get-mapping
"The get mapping API allows to retrieve mapping definition of index or index/type."
([^Connection conn ^String index-name]
(rest/get conn (rest/index-mapping-url conn
(join-names index-name))))
([^Connection conn ^String index-name ^String type-name]
(rest/get conn
(rest/index-mapping-url conn
index-name type-name))))
(defn update-mapping
"The put mapping API allows to register or modify specific mapping definition for a specific type."
[^Connection conn ^String index-name-or-names ^String type-name opts]
(let [{:keys [mapping]} opts]
(rest/put conn
(rest/index-mapping-url conn (join-names index-name-or-names) type-name)
{:body mapping
:query-params (dissoc opts :mapping)})))
;;
;; Settings
;;
(defn update-settings
"Change specific index level settings in real time."
([^Connection conn settings]
(rest/put conn (rest/index-settings-url conn) {:body settings}))
([^Connection conn ^String index-name settings]
(rest/put conn (rest/index-settings-url conn
index-name) {:body settings})))
(defn get-settings
"The get settings API allows to retrieve settings of an index or multiple indices."
([^Connection conn]
(rest/get conn (rest/index-settings-url conn)))
([^Connection conn ^String index-name]
(rest/get conn (rest/index-settings-url conn index-name))))
;;
;; Open/close
;;
(defn open
"Opens an index."
[^Connection conn index-name]
(rest/post conn (rest/index-open-url conn
index-name)))
(defn close
"Closes an index."
[^Connection conn index-name]
(rest/post conn (rest/index-close-url conn
index-name)))
(defn snapshot
"Takes a snapshot of an index or multiple indexes."
[^Connection conn index-name]
(rest/post conn (rest/index-snapshot-url conn
index-name)))
(defn refresh
"Refreshes an index manually.
Refreshing an index makes all changes (added, modified and deleted documents) since the last refresh available for search. In other
words, index changes become \"visible\" to clients. Elasticsearch periodically refreshes indexes, the period is configurable via index
settings.
* 0-arity updates *all* indexes and may be a very expensive operation. Use it carefully.
* 1-arity refreshes a single index."
([^Connection conn]
(rest/post conn (rest/index-refresh-url conn)))
([^Connection conn index-name]
(rest/post conn (rest/index-refresh-url conn
(join-names index-name)))))
(defn optimize
"Optimizes an index.
Optimization makes searches over the index faster and also reclaims some disk space used by
deleted documents. Optionally you can optimize and refresh an index in a single request.
* 0-arity optimizes *all* indexes and may be a very expensive operation. Use it carefully.
* 1-arity optimizes a single index.
Accepted options:
* `:max_num_segments`: the number of segments to optimize to.
* `:only_expunge_deletes`: should the optimize process only expunge segments with deleted documents in it?
* `:refresh`: when set to `true`, refreshes the index
* `:flush`: when set to `true`, flushes the index
* `:wait_for_merge`: should the request wait for the merge to end?"
([^Connection conn]
(rest/post conn (rest/index-optimize-url conn)))
([^Connection conn index-name]
(optimize conn index-name nil))
([^Connection conn index-name opts]
(rest/post conn (rest/index-optimize-url conn
(join-names index-name))
{:body opts})))
(defn flush
"Flushes an index.
This causes the index by flushing data to the index storage and clearing the internal transaction log.
Typically it is sufficient to let Elasticsearch when to periodically flush indexes.
* 0-arity flushes *all* indexes and may be a very expensive operation. Use it carefully.
* 1-arity flushes a single index.
Accepted options:
* `:refresh`: should a refresh be performed after the flush?"
([^Connection conn]
(rest/post conn (rest/index-flush-url conn)))
([^Connection conn index-name]
(rest/post conn (rest/index-flush-url conn
(join-names index-name))))
([^Connection conn index-name opts]
(rest/post conn (rest/index-flush-url conn
(join-names index-name)) {:body opts})))
(defn clear-cache
"Clears index caches.
* 0-arity clears caches for *all* indexes and may be a very expensive operation. Use it carefully.
* 1-arity clears caches for a single index.
Accepted options:
* `:filter`: should filter caches be cleared?
* `:field_data`: should field data caches be cleared?
* `:bloom`: should Bloom filter caches be cleared?"
([^Connection conn]
(rest/post conn (rest/index-clear-cache-url conn)))
([^Connection conn index-name]
(rest/post conn (rest/index-clear-cache-url conn
(join-names index-name))))
([^Connection conn index-name opts]
(rest/post conn (rest/index-clear-cache-url conn
(join-names index-name))
{:body opts})))
;;
;; Aliases
;;
(defn update-aliases
"Performs a batch of alias operations. Takes a collection of actions in the form of
```edn
{ :add { :index \"test1\" :alias \"alias1\" } }
{ :remove { :index \"test1\" :alias \"alias1\" } }
```"
[^Connection conn & actions]
(rest/post conn (rest/index-aliases-batch-url conn)
{:body {:actions actions}}))
(defn get-aliases
"Fetches and returns aliases for an index or multiple indexes."
[^Connection conn index-name]
(rest/get conn (rest/index-aliases-url conn
(join-names index-name))))
;;
;; Templates
;;
(defn create-template
"Creates or updates a new index template.
Accepted options:
* `:template`: a pattern of index name that this template will be applied to
* `:settings`: the same as for [[create]]
* `:mappings`: the same as for [[create]]
* `:aliases`: template aliases configuration"
([^Connection conn ^String template-name] (create-template conn template-name nil))
([^Connection conn ^String template-name opts]
(let [{:keys [template settings mappings aliases]} opts]
(rest/post conn (rest/index-template-url conn
template-name)
{:body (merge {:template template
:settings settings}
(if mappings {:mappings mappings})
(if aliases {:aliases aliases}))
}))))
(defn get-template
[^Connection conn ^String template-name]
(rest/get conn (rest/index-template-url conn
template-name)))
(defn delete-template
[^Connection conn ^String template-name]
(rest/delete conn (rest/index-template-url conn
template-name)))
#_
(defn recovery
"TODO refine description
Provides insight into index shard recoveries.
Accepted options:
`:detailed`: Display a detailed view. This is primarily useful for viewing the recovery of physical index files. Default: `false`.
`:active_only`: Display only those recoveries that are currently on-going. Default: `false`.
API reference: <https://www.elastic.co/guide/en/elasticsearch/reference/1.7/indices-recovery.html>"
([^Connection conn opts]
(rest/get conn (rest/index-recovery-url conn)
{:query-params opts}))
([^Connection conn index-name opts]
(rest/get conn (rest/index-recovery-url conn
(join-names index-name))
{:query-params opts})))
;; The above fails with "Can't have more than 1 variadic overload"
(defn recovery
"TODO refine description AND resolve the above issue"
[conn index-name]
(rest/get conn (rest/index-recovery-url conn
(join-names index-name))))
(defn segments
"Returns segments information for an index or multiple indexes."
([^Connection conn]
(rest/get conn (rest/index-segments-url conn)))
([^Connection conn index-name]
(rest/get conn (rest/index-segments-url conn
(join-names index-name)))))
(defn stats
"Returns statistics about an index or multiple indexes
Accepted options define what exactly will be contained in the response:
* `:stats`: the specific stat(s) to return (defaults to all)
* `:types`: combined with index stats to provide document type level stats
* `:groups`: search statistics can be associated with one or more groups
* `:fields`: fields to be included in the statistics by default where applicable
* `:completion_fields`: fields to be included in the completion suggest statistics
* `:fielddata_fields`: fields to be included in the fielddata statistics
API Reference: <https://www.elastic.co/guide/en/elasticsearch/reference/1.5/indices-stats.html>"
([^Connection conn index-name] (stats conn index-name nil))
([^Connection conn index-name opts]
(rest/get conn (rest/index-stats-url conn
(join-names index-name)
(join-names (get opts :stats "_all")))
{:query-params (dissoc opts :stats)})))