How to Create and Delete an Elasticsearch Index

Introduction

Elasticsearch provides an open-source, scalable, broadly-distributable search engine that suits enterprise needs. Its API offers many options including simple indexing and speedy searches. Elasticsearch can perform the same query in less than 10 milliseconds that a conventional SQL database require more than 10 seconds to conduct. Unlike SQL options, Elasticsearch easily considers loosely structured raw data residing outside the database. This blog gets you started with step-by-step instructions on creating and deleting an index in Elasticsearch.

Prerequisites

  • The ELK complete stack (Elasticsearch, Logstash, and Kibana) needs to be installed and running.
  • Visit your server’s web address with :5601 (or whatever port Kibana is running on) at the end of the URL to ensure that the Kibana UI is working.
  • For example: https://{YOUR_SERVER}.com:5601 or http://localhost:5601
  • Likewise the default port for Elasticsearch is 9200, so you can check your cluster’s settings by navigating to https://{YOUR_SERVER}.com:9200 or http://localhost:9200 in your browser.
  • You can also use this command in terminal to check on the status of Kibana:
    ps -ef | grep kibana
  • Creating an index within Elasticsearch is fairly simple, however be mindful of the following list of limitations:

  • Lowercase only
  • None of these special characters: , /, *, ?, “, open carat , closed carat , |,   (space character), ,, #
  • No starting with ‘-‘, ‘_’ or ‘+’,
  • No starting a single period (.) or a double period (..),
  • No longer than 255 bytes.

Create Index

  • To create an index in Elasticsearch you’ll be utilizing the Create Index API.
PUT samplecar
  • The above will create the index “car” with the default settings. Read the code below to see the result.
{
"samplecar" : {
"aliases" : { },
"mappings" : { },
"settings" : {
"index" : {
"creation_date" : "1552896770025",
"number_of_shards" : "5",
"number_of_replicas" : "1",
"uuid" : "xuJUcIhSTJ-S-bAjPa92bg",
"version" : {
"created" : "6060299"
},
"provided_name" : "samplecar"
}
}
}
}
  • You can see that the aliases and mappings default values are empty, while number_of_shards (shards) and number_of_replicas (replica) are 5 and 1 respectively which are the respective default values as assigned by Elasticsearch.

  • You can override the above to match your requirement.

  • See below for how-to create an index with the settings mentioned above.

PUT userprof

{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1
},
"mappings": {
"_doc" : {
"properties" : {
"name" : {"type" : "string"}
}
}
},
"aliases": {
"aliasname_1": {},
"aliasname_2" : {
"filter": {
"term" : {"user" : "Yeshua"}

},
"routing": "Yeshua"
}
}
}
  • The result of the above will be:
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "userprof"
}

The “acknowledged” entry tells us that the index was successfully created. shards_acknowledged indicates whether the shard copies were started for each shard in the index before timing out. It is actually possible for either acknowledged or shards_acknowledged to be false, but have the index created successfully.

Delete Index

  • If you’re deleting an index you’ll be utilizing the Delete Index API:
DELETE /userprof
  • The above will delete an index called userprof, it is required to specify an index or a wildcard expression.

  • You can use _all or * in delete index API to delete one or more index.

Note: use the _all or * with extreme caution as the name denotes. To disable allowing such operation, set the action.destructive_requires_name configuration to true.

Conclusion

It takes only a few minutes and a bit of Json code to get started with an Elasticsearch index. You will need the ELK complete stack already installed, but with this accomplished you can easily jump into using this super-fast query tool. After double-checking your prerequisites, you can begin using the tool to index and query data. This efficient mechanism performs much more quickly than SQL options and can easily consider data outside the formal database, as well.

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.