-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelasticsearch_utils.sh
executable file
·101 lines (75 loc) · 2.48 KB
/
elasticsearch_utils.sh
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
#!/usr/bin/env bash
#####
## This file contains utility functions to interact with Elasticsearch
## Some functions require the `ES_HOST` and `ES_PORT` variables to be set
##
## NOTE: To avoid name clashes with other scripts
## * Functions used outside of this script are prefixed with `es_`
## * Helper functions for this script are prefixed with `es_utils_`
##
## Please stick to the guidelines
#####
##### dependencies:
##### logger.sh
##### utils.sh
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${SCRIPT_DIR}/logger.sh"
source "${SCRIPT_DIR}/utils.sh"
##### end of dependencies
##### globals
: "${ES_HOST:=localhost}"
: "${ES_PORT:=9208}"
# Replace with target es-client
: "${ES_CLIENT_SERVICE:=some-es-client}"
##### end of globals
es_cluster_health() {
curl --silent --fail --max-time 5 "http://${ES_HOST}:${ES_PORT}/_cluster/health"
}
es_connection_check() {
if ! es_cluster_health >/dev/null; then
log ERROR "Could not connect to Elasticsearch at ${ES_HOST}:${ES_PORT}"
return 1
fi
}
es_get_index_with_name() {
local index="$1"
_validate_is_set index || return 1
curl --silent "http://${ES_HOST}:${ES_PORT}/_cat/indices?h=index" | grep --extended-regexp "${index}" || true
}
es_index_check() {
local index="$1"
_validate_is_set index || return 1
if ! es_get_index_with_name "${index}" >/dev/null; then
log WARN "Index '${index}' does not exist"
return 1
fi
}
es_get_project_indices() {
local project_key="$1"
_validate_is_set project_key || return 1
local search_pattern="${project_key}-some-index-pattern"
curl --silent "http://${ES_HOST}:${ES_PORT}/_cat/indices?h=index" | grep --extended-regexp "${search_pattern}" || true
}
es_delete_index() {
local index="$1"
_validate_is_set index || return 1
if ! curl --silent --fail --request DELETE "http://${ES_HOST}:${ES_PORT}/${index}"; then
log ERROR "Failed to delete '${index}'"
return 1
fi
}
es_update_index_nested_objects_mapping_limit() {
local index="$1"
local docs_limit="$2"
_validate_is_set index || return 1
_validate_positive_integer docs_limit || return 1
log INFO "Updating nested objects mapping limit for ${index}..."
if ! curl --silent --fail \
--request PUT \
--header "Content-Type: application/json" \
--data '{"index": {"mapping.nested_objects.limit": '"${docs_limit}"'}}' \
"http://${ES_HOST}:${ES_PORT}/${index}/_settings"; then
log ERROR "Failed to update index nested objects mapping limit for ${index}"
return 1
fi
}