Working with indexes in Elasticsearch

Indexes in Elasticsearch are collections of data that hold similar characteristics. For example, if you had an index of web hosting plans, it would contain several documents such as shared, VPS, dedicated, and reseller. Simply put, indexes allow you to group similar data together to search through those related items.

Information within Elasticsearch is created and retrieved using HTTP requests, usually containing JSON data with the request.

Note: This article assumes that you have already installed Elasticsearch as well as know how to use SSH.

Listing all indexes in ElasticSearch

If you have various indexes within Elasticsearch, chances are you haven’t memorized them all. The following command will allow you to list all indexes that are present within your Elasticsearch server:

curl ‘localhost:9200/_cat/indices?v’

As you can see in the above, we are first using the curl command to send the HTTP request. Within the quotes, you see our server, which is localhost, meaning that we are making the request to the same server that we are currently in, then the port which is 9200.

After the server is defined, we are then telling Elasticsearch that we will be sending a cat command by entering _cat, then telling it that we want to obtain the indices.

The last part is ?v which tells Elasticsearch that we want a verbose output. This is optional, but allows the column headers to be seen.

Your output should looks something like this:

user@server [~]# curl ‘localhost:9200/_cat/indices?v’
health status index pri rep docs.count docs.deleted store.size pri.store.size
yellow open mynewindex 5 1 0 0 575b 575b
yellow open secondindex 5 1 0 0 575b 575b
yellow open yetanotherindex 5 1 0 0 575b 575b

There’s a lot of information here, but for the purpose of this article, we will just focus on the indexes that exist. If you take a look at the index column, there’s a list of all indexes currently present. If this command doesn’t output anything there, you don’t have any indexes defined and should continue on to the next step which is creating indexes in Elasticsearch.

Creating indexes in Elasticsearch

In the example below, we are using cURL to send a PUT request to the ElasticSearch server with our new index:

curl -XPUT ‘localhost:9200/mynewindex?pretty’

As you can see above, we first call the curl command, then the XPUT flag to tell cURL that we want to use the PUT verb when sending the request.

Then within the quotes, we have the location and path that we want to send the request to. In this example, our server is the same server that we are sending the request from, so we can use localhost for the server. By default, the Elasticsearch server runs on port 9200, so we have also included this as well so that the request is properly passed to Elasticsearch.

Next in the path is mynewindex. This is simply the name of the index that we want to create. You can name your index absolutely anything you want, but it does need to be all lower-case.

Our final piece of the request being sent to the server is ?pretty. This is an optional argument that you can use to return better formatted results. It’s all a matter of preference, but most users would like to see the JSON output properly tabbed and easier to read.

Once the request has been completed, you will receive a confirmation that looks like this:

user@server [~]# curl -XPUT ‘localhost:9200/mynewindex?pretty’
{
“acknowledged” : true
}

As you can see, we are presented with acknowledged: true which means that the query was executed successfully and the index was inserted.

Deleting indexes in Elasticsearch

Deleting indexes is very similar to creating them, but instead we use the DELETE verb within our HTTP request. For example, the following will delete the mynewindex index:

curl -XDELETE ‘localhost:9200/mynewindex?pretty’

As you can see, the request is identical with the exception that the verb that we are sending with our HTTP request is DELETE instead of PUT.

Was this article helpful? Join the conversation!

Questions about our MailChannels Deployment? We have answers and are here to help!Learn More
+