@@ -72,3 +72,105 @@ make ingest
72
72
# # Elasticsearch Mappings
73
73
74
74
Mappings apply to search index, not source.
75
+
76
+ # # Managing Elasticsearch Indices
77
+
78
+ This section covers how to create a snapshot repository and then create and restore snapshots with this.
79
+
80
+ Create a snapshot repository. This puts the files in the ` elasticsearch/snapshots` in this git repo clone, as
81
+ the elasticsearch.yml and docker-compose files create a mapping from that directory to
82
+ ` /usr/share/elasticsearch/snapshots` within the Elasticsearch container and grant permissions on using it.
83
+
84
+ ```
85
+ curl -X "PUT" "http://localhost:9200/_snapshot/my_fs_backup " \
86
+ -H 'Content-Type: application/json; charset=utf-8' \
87
+ -d $'{
88
+ "type": "fs",
89
+ "settings": {
90
+ "location": "/usr/share/elasticsearch/snapshots/my_fs_backup"
91
+ }
92
+ }'
93
+ ```
94
+
95
+ The next step is to create a snapshot of one or more indices into this snapshot repository. This command creates
96
+ a snapshot named `my_snapshot_2` and waits for the action to be completed before returning. This can also be done
97
+ asynchronously, and queried for status. The `indices` parameter determines which indices are snapshotted, and
98
+ can include wildcards.
99
+
100
+ ```
101
+ curl -X "PUT" "http://localhost:9200/_snapshot/my_fs_backup/my_snapshot_2?wait_for_completion=true " \
102
+ -H 'Content-Type: application/json; charset=utf-8' \
103
+ -d $'{
104
+ "metadata": {
105
+ "taken_because": "dump of all items",
106
+ "taken_by": "pvarner"
107
+ },
108
+ "include_global_state": false,
109
+ "ignore_unavailable": false,
110
+ "indices": "items_my-collection"
111
+ }'
112
+ ```
113
+
114
+ To see the status of this snapshot:
115
+
116
+ ```
117
+ curl http://localhost:9200/_snapshot/my_fs_backup/my_snapshot_2
118
+ ```
119
+
120
+ To see all the snapshots:
121
+
122
+ ```
123
+ curl http://localhost:9200/_snapshot/my_fs_backup/_all
124
+ ```
125
+
126
+ To restore a snapshot, run something similar to the following. This specific command will restore any indices that
127
+ match `items_*` and rename them so that the new index name will be suffixed with `-copy`.
128
+
129
+ ```
130
+ curl -X "POST" "http://localhost:9200/_snapshot/my_fs_backup/my_snapshot_2/_restore?wait_for_completion=true " \
131
+ -H 'Content-Type: application/json; charset=utf-8' \
132
+ -d $'{
133
+ "include_aliases": false,
134
+ "include_global_state": false,
135
+ "ignore_unavailable": true,
136
+ "rename_replacement": "items_ $1-copy",
137
+ "indices": "items_ * ",
138
+ "rename_pattern": "items_ (.+)"
139
+ }'
140
+
141
+ ```
142
+
143
+ Now the item documents have been restored in to the new index (e.g., `my-collection-copy`), but the value of the
144
+ `collection` field in those documents is still the original value of `my-collection`. To update these to match the
145
+ new collection name, run the following Elasticsearch Update By Query command, substituting the old collection name
146
+ into the term filter and the new collection name into the script parameter:
147
+
148
+ ```
149
+ curl -X "POST" "http://localhost:9200/items_my-collection-copy/_update_by_query " \
150
+ -H 'Content-Type: application/json; charset=utf-8' \
151
+ -d $'{
152
+ "query": {
153
+ "match_all": {}
154
+ },
155
+ "script": {
156
+ "lang": "painless",
157
+ "params": {
158
+ "collection": "my-collection-copy"
159
+ },
160
+ "source": "ctx._ source.collection = params.collection"
161
+ }
162
+ }'
163
+ ```
164
+
165
+ Then, create a new collection through the api with the new name for each of the restored indices:
166
+
167
+ ```
168
+ curl -X "POST" "http://localhost:8080/collections " \
169
+ -H 'Content-Type: application/json' \
170
+ -d $'{
171
+ "id": "my-collection-copy"
172
+ }'
173
+ ```
174
+
175
+ Voila! You have a copy of the collection now that has a resource URI (`/collections/my-collection-copy`) and can be
176
+ correctly queried by collection name.
0 commit comments