-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathsolr-functions
executable file
·198 lines (177 loc) · 6.26 KB
/
solr-functions
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
#!/usr/bin/env bash
SOLR_HOST=${SOLR_HOST:-http://localhost:${PORT:-8983}}
check_core() {
LOCAL_CORE=$1
LOCAL_URL=$(printf "%s/solr/admin/cores?action=STATUS&core=%s" "$SOLR_HOST" "$LOCAL_CORE")
curl -s "$LOCAL_URL" | jq .status | grep "\"$LOCAL_CORE\":" | grep -c -P '{$'
}
create_core() {
LOCAL_CORE=$1
echo "creating Solr index: $LOCAL_CORE at $SOLR_HOST"
curl -s "$SOLR_HOST/solr/admin/cores?action=CREATE&name=$LOCAL_CORE&configSet=_default"
}
rename_core() {
LOCAL_FROM=$1
LOCAL_TO=$2
echo "rename Solr index: $LOCAL_FROM to $LOCAL_TO"
curl -s "$SOLR_HOST/solr/admin/cores?action=RENAME&core=$LOCAL_FROM&other=$LOCAL_TO"
}
swap_cores() {
LOCAL_FROM=$1
LOCAL_TO=$2
echo "Swap Solr indexes $LOCAL_FROM and $LOCAL_TO"
curl -s "$SOLR_HOST/solr/admin/cores?action=SWAP&core=$LOCAL_FROM&other=$LOCAL_TO"
}
prepare_schema() {
LOCAL_CORE=$1
SCHEMA_URL="$SOLR_HOST/api/cores/$LOCAL_CORE/schema"
echo "prepare_schema $LOCAL_CORE"
HAS_PROPER_SNI=$(curl -is "$SCHEMA_URL/dynamicfields/*_sni" | grep -c '"type":"string_big"')
echo "Does $LOCAL_CORE have proper *_sni field definition? Answer: $HAS_PROPER_SNI"
if [[ "$HAS_PROPER_SNI" -eq 0 ]]; then
HAS_FIELD=$(curl -is "$SCHEMA_URL/dynamicfields/*_sni" | head -1 | grep -c "200 OK")
echo "Does $LOCAL_CORE have *_sni field definition? Answer: $HAS_FIELD"
if [[ $HAS_FIELD -eq 1 ]]; then
echo "Delete *_sni field definition from $LOCAL_CORE."
curl -X POST -H 'Content-type:application/json' --data-binary '{
"delete-dynamic-field":{"name":"*_sni",}
}' "$SCHEMA_URL"
fi
HAS_TYPE=$(curl -is "$SCHEMA_URL/fieldtypes/string_big" | head -1 | grep -c "200 OK")
if [[ "$HAS_TYPE" -eq 1 ]]; then
echo "Delete string_big field type definition from $LOCAL_CORE."
curl -X POST -H 'Content-type:application/json' --data-binary '{
"delete-field-type":{"name":"string_big",}
}' "$SCHEMA_URL"
fi
echo "Create string_big field type definition in $LOCAL_CORE."
curl -X POST -H 'Content-type:application/json' --data-binary '{
"add-field-type" : {
"name":"string_big",
"class":"solr.TextField",
"positionIncrementGap":"100",
"sortMissingLast":true,
"omitNorms":true,
"analyzer" : {
"tokenizer":{
"class":"solr.KeywordTokenizerFactory"
},
}
}
}' "$SCHEMA_URL"
echo "Create *_sni dynamic field definition in $LOCAL_CORE."
curl -X POST -H 'Content-type:application/json' --data-binary '{
"add-dynamic-field":{
"name":"*_sni",
"type":"string_big",
"stored":true,
"indexed":false,
"docValues":false,
"multiValued":false,
}}' "$SCHEMA_URL"
fi
NUMBER_OF_COPY_FIELD=$(curl -is "$SCHEMA_URL/copyfields?source.fl=*_ss" | grep -c '"source":"\*_ss"')
echo "NUMBER_OF_COPY_FIELD: $NUMBER_OF_COPY_FIELD"
if [[ "$NUMBER_OF_COPY_FIELD" -eq 0 ]]; then
echo "add *_ss copyfield definition to $LOCAL_CORE"
# add _ss
# <copyField source="*_ss" dest="_text_"/>
curl -X POST -H 'Content-type:application/json' --data-binary '{
"add-copy-field":{
"source":"*_ss",
"dest":"_text_"
}}' "$SCHEMA_URL"
elif [[ "$NUMBER_OF_COPY_FIELD" -gt 1 ]]; then
while [[ "$NUMBER_OF_COPY_FIELD" -gt 1 ]]; do
echo "delete extra copy fields"
curl -s -X POST -H 'Content-type:application/json' --data-binary '{
"delete-copy-field":{ "source":"*_ss", "dest":"_text_" }
}' "$SCHEMA_URL"
NUMBER_OF_COPY_FIELD=$(curl -is "$SCHEMA_URL/copyfields?source.fl=*_ss" | grep -c '"source":"\*_ss"')
done
fi
HAS_PROPER_TT=$(curl -is "$SCHEMA_URL/dynamicfields/*_tt" | grep -c '"type":"text_general"')
echo "Does $LOCAL_CORE have proper *_tt field definition? Answer: $HAS_PROPER_TT"
if [[ "$HAS_PROPER_TT" -eq 0 ]]; then
# add _tt
# <dynamicField name="*_tt" type="text_general" indexed="true" stored="false"/>
echo "Create *_tt dynamic field definition in $LOCAL_CORE."
curl -X POST -H 'Content-type:application/json' --data-binary '{
"add-dynamic-field":{
"name":"*_tt",
"type":"text_general",
"stored":false,
"indexed":true,
}}' "$SCHEMA_URL"
fi
}
status() {
echo "Solr index status at $SOLR_HOST"
echo
curl -s "$SOLR_HOST/solr/admin/cores?action=STATUS" \
| jq . \
| grep instanceDir \
| head -1 \
| awk '{print $2}' \
| sed -E 's,^"(.*solr).+$,\1,' \
| xargs -I '@@' echo "Solr directory: @@"
FORMAT="%-20s | %-15s | %10s | %10s | %19s\n"
printf "$FORMAT" "core" "location" "nr of docs" "size" "last modified"
printf "$FORMAT" "...................." "..............." ".........." ".........." "..................."
curl -s "$SOLR_HOST/solr/admin/cores?action=STATUS" \
| jq '.status[] | [.name, .instanceDir, .index.numDocs, .index.size, .index.lastModified]' \
| sed -r 's,".*/server/solr/(.+)",\1,' \
| paste - - - - - - - \
| sed -r 's,(\[|]|\t|\,),,g' \
| sed -r 's,([0-9])T([0-9]),\1 \2,g' \
| sed -r 's,\.[0-9]{3}Z,,g' \
| xargs printf "$FORMAT"
exit 1
}
purge_core() {
LOCAL_CORE=$1
SOLR_DB_URL="$SOLR_HOST/solr/$LOCAL_CORE"
echo "Delete records in $SOLR_DB_URL"
curl -s "$SOLR_DB_URL/update" \
-H "Content-type: text/xml" \
--data-binary '<delete><query>*:*</query></delete>' \
> /dev/null
}
optimize_core() {
LOCAL_CORE=$1
SOLR_DB_URL="$SOLR_HOST/solr/$LOCAL_CORE"
echo "Optimizing $SOLR_DB_URL"
curl -s "$SOLR_DB_URL/update?optimize=true" \
-H 'Content-type: text/xml' \
--data-binary '<commit/>' \
> /dev/null
}
purge_and_exit() {
LOCAL_CORE=$1
if [[ -n "$LOCAL_CORE" ]]; then
purge_core "$LOCAL_CORE"
optimize_core "$LOCAL_CORE"
else
echo "You should give the name with --core parameter"
fi
exit 1
}
store_fields() {
SOLR_URL=$1
OUTPUT_DIR=$2
echo "Store fields into $OUTPUT_DIR/solr-fields.json"
echo "curl -s \"$SOLR_URL/admin/luke?wt=json\" | jq '.fields | keys_unsorted'"
curl -s "$SOLR_URL/admin/luke?wt=json" \
| jq '.fields | keys_unsorted' \
> "$OUTPUT_DIR/solr-fields.json"
}
extract_host() {
URL=$1
HOST=$(echo "$URL" | grep -oP "^https?://[^/]+" || true)
echo "$HOST"
}
extract_core() {
URL=$1
CORE=$(echo "$URL" | grep -oP "[^/]+$" || true)
echo "$CORE"
}