|
| 1 | +== Creating indexes, alias and reindexing using Elasticsearch API |
| 2 | + |
| 3 | +In the following steps, we are going to, manually and using `Elasticsearch` API, create an index called `ecommerce.products.v1`, associate an alias called `ecommerce.products` for it and then reindex to another index called `ecommerce.products.v2`. |
| 4 | + |
| 5 | +Make sure you have a clean `Elasticsearch` without the indexes and alias mentioned previously. Also, the following `curl` commands must be executed in `springboot-elasticsearch-thymeleaf` root folder. |
| 6 | + |
| 7 | +* Check ES is up and running |
| 8 | ++ |
| 9 | +[source] |
| 10 | +---- |
| 11 | +curl localhost:9200 |
| 12 | +---- |
| 13 | ++ |
| 14 | +It should return something like |
| 15 | ++ |
| 16 | +[source] |
| 17 | +---- |
| 18 | +{ |
| 19 | + "name" : "99fdd70d5915", |
| 20 | + "cluster_name" : "docker-cluster", |
| 21 | + "cluster_uuid" : "1HUDp8N3SF2WLtzZYOGgxA", |
| 22 | + "version" : { |
| 23 | + "number" : "8.5.3", |
| 24 | + "build_flavor" : "default", |
| 25 | + "build_type" : "docker", |
| 26 | + "build_hash" : "4ed5ee9afac63de92ec98f404ccbed7d3ba9584e", |
| 27 | + "build_date" : "2022-12-05T18:22:22.226119656Z", |
| 28 | + "build_snapshot" : false, |
| 29 | + "lucene_version" : "9.4.2", |
| 30 | + "minimum_wire_compatibility_version" : "7.17.0", |
| 31 | + "minimum_index_compatibility_version" : "7.0.0" |
| 32 | + }, |
| 33 | + "tagline" : "You Know, for Search" |
| 34 | +} |
| 35 | +---- |
| 36 | +
|
| 37 | +* Create `ecommerce.products.v1` index |
| 38 | ++ |
| 39 | +[source] |
| 40 | +---- |
| 41 | +curl -X PUT localhost:9200/ecommerce.products.v1 -H "Content-Type: application/json" -d @elasticsearch/mapping-v1.json |
| 42 | +---- |
| 43 | ++ |
| 44 | +It should return |
| 45 | ++ |
| 46 | +[source] |
| 47 | +---- |
| 48 | +{"acknowledged":true,"shards_acknowledged":true,"index":"ecommerce.products.v1"} |
| 49 | +---- |
| 50 | +
|
| 51 | +* Check indexes |
| 52 | ++ |
| 53 | +[source] |
| 54 | +---- |
| 55 | +curl "localhost:9200/_cat/indices?v" |
| 56 | +---- |
| 57 | ++ |
| 58 | +It should return something like |
| 59 | ++ |
| 60 | +[source] |
| 61 | +---- |
| 62 | +health status index uuid pri rep docs.count docs.deleted store.size pri.store.size |
| 63 | +yellow open ecommerce.products.v1 qgIIfyD1TUCN2s0wiDlmzA 1 1 0 0 225b 225b |
| 64 | +---- |
| 65 | +
|
| 66 | +* Check `ecommerce.products.v1` index mapping |
| 67 | ++ |
| 68 | +[source] |
| 69 | +---- |
| 70 | +curl "localhost:9200/ecommerce.products.v1/_mapping?pretty" |
| 71 | +---- |
| 72 | ++ |
| 73 | +It should return |
| 74 | ++ |
| 75 | +[source] |
| 76 | +---- |
| 77 | +{ |
| 78 | + "ecommerce.products.v1" : { |
| 79 | + "mappings" : { |
| 80 | + "properties" : { |
| 81 | + "categories" : { |
| 82 | + "type" : "keyword" |
| 83 | + }, |
| 84 | + "created" : { |
| 85 | + "type" : "date", |
| 86 | + "format" : "strict_date_time_no_millis||yyyy-MM-dd'T'HH:mmZZ" |
| 87 | + }, |
| 88 | + "description" : { |
| 89 | + "type" : "text", |
| 90 | + "analyzer" : "my_analyzer", |
| 91 | + "search_analyzer" : "my_search_analyzer" |
| 92 | + }, |
| 93 | + "name" : { |
| 94 | + "type" : "text", |
| 95 | + "analyzer" : "my_analyzer", |
| 96 | + "search_analyzer" : "my_search_analyzer" |
| 97 | + }, |
| 98 | + "price" : { |
| 99 | + "type" : "float" |
| 100 | + }, |
| 101 | + "reference" : { |
| 102 | + "type" : "text" |
| 103 | + }, |
| 104 | + "reviews" : { |
| 105 | + "properties" : { |
| 106 | + "comment" : { |
| 107 | + "type" : "text" |
| 108 | + }, |
| 109 | + "created" : { |
| 110 | + "type" : "date", |
| 111 | + "format" : "strict_date_time_no_millis||yyyy-MM-dd'T'HH:mmZZ" |
| 112 | + }, |
| 113 | + "stars" : { |
| 114 | + "type" : "short" |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | +---- |
| 123 | +
|
| 124 | +* Create alias for `ecommerce.products.v1` index |
| 125 | ++ |
| 126 | +[source] |
| 127 | +---- |
| 128 | +curl -X POST localhost:9200/_aliases -H 'Content-Type: application/json' \ |
| 129 | + -d '{ "actions": [{ "add": {"alias": "ecommerce.products", "index": "ecommerce.products.v1" }}]}' |
| 130 | +---- |
| 131 | ++ |
| 132 | +It should return |
| 133 | ++ |
| 134 | +[source] |
| 135 | +---- |
| 136 | +{"acknowledged":true} |
| 137 | +---- |
| 138 | +
|
| 139 | +* Check aliases |
| 140 | ++ |
| 141 | +[source] |
| 142 | +---- |
| 143 | +curl "localhost:9200/_aliases?pretty" |
| 144 | +---- |
| 145 | ++ |
| 146 | +It should return |
| 147 | ++ |
| 148 | +[source] |
| 149 | +---- |
| 150 | +{ |
| 151 | + "ecommerce.products.v1" : { |
| 152 | + "aliases" : { |
| 153 | + "ecommerce.products" : { } |
| 154 | + } |
| 155 | + } |
| 156 | +} |
| 157 | +---- |
| 158 | +
|
| 159 | +* Create `ecommerce.products.v2` index |
| 160 | ++ |
| 161 | +[source] |
| 162 | +---- |
| 163 | +curl -X PUT localhost:9200/ecommerce.products.v2 -H "Content-Type: application/json" -d @elasticsearch/mapping-v2.json |
| 164 | +---- |
| 165 | ++ |
| 166 | +It should return |
| 167 | ++ |
| 168 | +[source] |
| 169 | +---- |
| 170 | +{"acknowledged":true,"shards_acknowledged":true,"index":"ecommerce.products.v2"} |
| 171 | +---- |
| 172 | ++ |
| 173 | +Checking indexes again |
| 174 | ++ |
| 175 | +[source] |
| 176 | +---- |
| 177 | +curl "localhost:9200/_cat/indices?v" |
| 178 | +---- |
| 179 | ++ |
| 180 | +It should return something like |
| 181 | ++ |
| 182 | +[source] |
| 183 | +---- |
| 184 | +health status index uuid pri rep docs.count docs.deleted store.size pri.store.size |
| 185 | +yellow open ecommerce.products.v2 pGzs5rfCR32aBVukwmEu6Q 1 1 0 0 225b 225b |
| 186 | +yellow open ecommerce.products.v1 qgIIfyD1TUCN2s0wiDlmzA 1 1 0 0 225b 225b |
| 187 | +---- |
| 188 | +
|
| 189 | +* Reindex from `ecommerce.products.v1` to `ecommerce.products.v2` |
| 190 | ++ |
| 191 | +[source] |
| 192 | +---- |
| 193 | +curl -X POST localhost:9200/_reindex -H 'Content-Type: application/json' \ |
| 194 | + -d '{ "source": { "index": "ecommerce.products.v1" }, "dest": { "index": "ecommerce.products.v2" }}' |
| 195 | +---- |
| 196 | ++ |
| 197 | +It should return something like |
| 198 | ++ |
| 199 | +[source] |
| 200 | +---- |
| 201 | +{"took":13,"timed_out":false,"total":0,"updated":0,"created":0,"deleted":0,"batches":0,"version_conflicts":0,"noops":0,"retries":{"bulk":0,"search":0},"throttled_millis":0,"requests_per_second":-1.0,"throttled_until_millis":0,"failures":[]} |
| 202 | +---- |
| 203 | +
|
| 204 | +* Adjust alias after reindex from `ecommerce.products.v1` to `ecommerce.products.v2` |
| 205 | ++ |
| 206 | +[source] |
| 207 | +---- |
| 208 | +curl -X POST localhost:9200/_aliases -H 'Content-Type: application/json' \ |
| 209 | + -d '{ "actions": [{ "remove": {"alias": "ecommerce.products", "index": "ecommerce.products.v1" }}, { "add": {"alias": "ecommerce.products", "index": "ecommerce.products.v2" }}]}' |
| 210 | +---- |
| 211 | ++ |
| 212 | +It should return |
| 213 | ++ |
| 214 | +[source] |
| 215 | +---- |
| 216 | +{"acknowledged":true} |
| 217 | +---- |
| 218 | ++ |
| 219 | +Checking aliases again |
| 220 | ++ |
| 221 | +[source] |
| 222 | +---- |
| 223 | +curl "localhost:9200/_aliases?pretty" |
| 224 | +---- |
| 225 | ++ |
| 226 | +It should return something like |
| 227 | ++ |
| 228 | +[source] |
| 229 | +---- |
| 230 | +{ |
| 231 | + "ecommerce.products.v2" : { |
| 232 | + "aliases" : { |
| 233 | + "ecommerce.products" : { } |
| 234 | + } |
| 235 | + }, |
| 236 | + "ecommerce.products.v1" : { |
| 237 | + "aliases" : { } |
| 238 | + } |
| 239 | +} |
| 240 | +---- |
| 241 | +
|
| 242 | +* Delete `ecommerce.products.v1` index |
| 243 | ++ |
| 244 | +[source] |
| 245 | +---- |
| 246 | +curl -X DELETE localhost:9200/ecommerce.products.v1 |
| 247 | +---- |
| 248 | ++ |
| 249 | +It should return |
| 250 | ++ |
| 251 | +[source] |
| 252 | +---- |
| 253 | +{"acknowledged":true} |
| 254 | +---- |
| 255 | ++ |
| 256 | +Checking aliases again |
| 257 | ++ |
| 258 | +[source] |
| 259 | +---- |
| 260 | +curl "localhost:9200/_aliases?pretty" |
| 261 | +---- |
| 262 | ++ |
| 263 | +It should return |
| 264 | ++ |
| 265 | +[source] |
| 266 | +---- |
| 267 | +{ |
| 268 | + "ecommerce.products.v2" : { |
| 269 | + "aliases" : { |
| 270 | + "ecommerce.products" : { } |
| 271 | + } |
| 272 | + } |
| 273 | +} |
| 274 | +---- |
| 275 | +
|
| 276 | +* Simple search |
| 277 | ++ |
| 278 | +[source] |
| 279 | +---- |
| 280 | +curl "localhost:9200/ecommerce.products/_search?pretty" |
| 281 | +---- |
| 282 | ++ |
| 283 | +It should return something like |
| 284 | ++ |
| 285 | +[source] |
| 286 | +---- |
| 287 | +{ |
| 288 | + "took" : 5, |
| 289 | + "timed_out" : false, |
| 290 | + "_shards" : { |
| 291 | + "total" : 1, |
| 292 | + "successful" : 1, |
| 293 | + "skipped" : 0, |
| 294 | + "failed" : 0 |
| 295 | + }, |
| 296 | + "hits" : { |
| 297 | + "total" : { |
| 298 | + "value" : 0, |
| 299 | + "relation" : "eq" |
| 300 | + }, |
| 301 | + "max_score" : null, |
| 302 | + "hits" : [ ] |
| 303 | + } |
| 304 | +} |
| 305 | +---- |
| 306 | ++ |
| 307 | +> As we don't have any products, the `hits` array field is empty |
0 commit comments