Skip to content

Latest commit

 

History

History
307 lines (294 loc) · 6.39 KB

create-index-alias-reindex.adoc

File metadata and controls

307 lines (294 loc) · 6.39 KB

Creating indexes, alias and reindexing using Elasticsearch API

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.

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.

  • Check ES is up and running

    curl localhost:9200

    It should return something like

    {
      "name" : "99fdd70d5915",
      "cluster_name" : "docker-cluster",
      "cluster_uuid" : "1HUDp8N3SF2WLtzZYOGgxA",
      "version" : {
        "number" : "8.5.3",
        "build_flavor" : "default",
        "build_type" : "docker",
        "build_hash" : "4ed5ee9afac63de92ec98f404ccbed7d3ba9584e",
        "build_date" : "2022-12-05T18:22:22.226119656Z",
        "build_snapshot" : false,
        "lucene_version" : "9.4.2",
        "minimum_wire_compatibility_version" : "7.17.0",
        "minimum_index_compatibility_version" : "7.0.0"
      },
      "tagline" : "You Know, for Search"
    }
  • Create ecommerce.products.v1 index

    curl -X PUT localhost:9200/ecommerce.products.v1 -H "Content-Type: application/json" -d @elasticsearch/mapping-v1.json

    It should return

    {"acknowledged":true,"shards_acknowledged":true,"index":"ecommerce.products.v1"}
  • Check indexes

    curl "localhost:9200/_cat/indices?v"

    It should return something like

    health status index                 uuid                   pri rep docs.count docs.deleted store.size pri.store.size
    yellow open   ecommerce.products.v1 qgIIfyD1TUCN2s0wiDlmzA   1   1          0            0       225b           225b
  • Check ecommerce.products.v1 index mapping

    curl "localhost:9200/ecommerce.products.v1/_mapping?pretty"

    It should return

    {
      "ecommerce.products.v1" : {
        "mappings" : {
          "properties" : {
            "categories" : {
              "type" : "keyword"
            },
            "created" : {
              "type" : "date",
              "format" : "strict_date_time_no_millis||yyyy-MM-dd'T'HH:mmZZ"
            },
            "description" : {
              "type" : "text",
              "analyzer" : "my_analyzer",
              "search_analyzer" : "my_search_analyzer"
            },
            "name" : {
              "type" : "text",
              "analyzer" : "my_analyzer",
              "search_analyzer" : "my_search_analyzer"
            },
            "price" : {
              "type" : "float"
            },
            "reference" : {
              "type" : "text"
            },
            "reviews" : {
              "properties" : {
                "comment" : {
                  "type" : "text"
                },
                "created" : {
                  "type" : "date",
                  "format" : "strict_date_time_no_millis||yyyy-MM-dd'T'HH:mmZZ"
                },
                "stars" : {
                  "type" : "short"
                }
              }
            }
          }
        }
      }
    }
  • Create alias for ecommerce.products.v1 index

    curl -X POST localhost:9200/_aliases -H 'Content-Type: application/json' \
        -d '{ "actions": [{ "add": {"alias": "ecommerce.products", "index": "ecommerce.products.v1" }}]}'

    It should return

    {"acknowledged":true}
  • Check aliases

    curl "localhost:9200/_aliases?pretty"

    It should return

    {
      "ecommerce.products.v1" : {
        "aliases" : {
          "ecommerce.products" : { }
        }
      }
    }
  • Create ecommerce.products.v2 index

    curl -X PUT localhost:9200/ecommerce.products.v2 -H "Content-Type: application/json" -d @elasticsearch/mapping-v2.json

    It should return

    {"acknowledged":true,"shards_acknowledged":true,"index":"ecommerce.products.v2"}

    Checking indexes again

    curl "localhost:9200/_cat/indices?v"

    It should return something like

    health status index                 uuid                   pri rep docs.count docs.deleted store.size pri.store.size
    yellow open   ecommerce.products.v2 pGzs5rfCR32aBVukwmEu6Q   1   1          0            0       225b           225b
    yellow open   ecommerce.products.v1 qgIIfyD1TUCN2s0wiDlmzA   1   1          0            0       225b           225b
  • Reindex from ecommerce.products.v1 to ecommerce.products.v2

    curl -X POST localhost:9200/_reindex -H 'Content-Type: application/json' \
         -d '{ "source": { "index": "ecommerce.products.v1" }, "dest": { "index": "ecommerce.products.v2" }}'

    It should return something like

    {"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":[]}
  • Adjust alias after reindex from ecommerce.products.v1 to ecommerce.products.v2

    curl -X POST localhost:9200/_aliases -H 'Content-Type: application/json' \
         -d '{ "actions": [{ "remove": {"alias": "ecommerce.products", "index": "ecommerce.products.v1" }}, { "add": {"alias": "ecommerce.products", "index": "ecommerce.products.v2" }}]}'

    It should return

    {"acknowledged":true}

    Checking aliases again

    curl "localhost:9200/_aliases?pretty"

    It should return something like

    {
      "ecommerce.products.v2" : {
        "aliases" : {
          "ecommerce.products" : { }
        }
      },
      "ecommerce.products.v1" : {
        "aliases" : { }
      }
    }
  • Delete ecommerce.products.v1 index

    curl -X DELETE localhost:9200/ecommerce.products.v1

    It should return

    {"acknowledged":true}

    Checking aliases again

    curl "localhost:9200/_aliases?pretty"

    It should return

    {
      "ecommerce.products.v2" : {
        "aliases" : {
          "ecommerce.products" : { }
        }
      }
    }
  • Simple search

    curl "localhost:9200/ecommerce.products/_search?pretty"

    It should return something like

    {
      "took" : 5,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 0,
          "relation" : "eq"
        },
        "max_score" : null,
        "hits" : [ ]
      }
    }

    As we don’t have any products, the hits array field is empty