Creating an Index
In this article, we will see how to add an index to our Elasticsearch cluster. To do this, we will be using the Kibana plugin, Sense, which we installed in a previous article. To create an index in Elasticsearch, simply issue a PUT request like below.
PUT /ecommerce
{
}
This command is translated into an HTTP request that uses the HTTP verb PUT. So while we are using a relative URI, you would have to use a fully qualified URL when using cURL or when connecting to Elasticsearch in an application. In fact, Sense can help you with this; by clicking the wrench icon and clicking “Copy as cURL”, Sense will give you the corresponding cURL request. Additionally, if you copy a cURL request into Sense, it will automatically format it to the Sense syntax, which is pretty useful.
After issuing the request, let’s confirm that the index has been created by issuing another request. Using the _cat API, we can retrieve a lot of information about our cluster in a human friendly format (i.e. not JSON). For now, we will just make use of the indices command.
GET /_cat/indices?v
The v query parameter is used to make the output verbose, i.e. including descriptive column headers. The result shows various information about the indexes in our cluster, such as the number of primary shards, replicas, documents, deleted documents, etc.
health status index pri rep docs.count docs.deleted store.size pri.store.size
yellow open .kibana 1 1 2 0 5kb 5kb
yellow open ecommerce 5 1 4110 5 681kb 681kb
Note that the indexes have a yellow health tagged to them. Yellow means that some replicas are not allocated. The reason this happens is that by default, Elasticsearch created one replica for the indexes. Since we only have one node running at the moment, that one replica cannot yet be allocated for high availability, until a later point in time when another node joins the cluster. Once an indexe’s replica gets allocated onto a second node, the health status for that index will turn to green.
It is also worth mentioning that one does not have to create an index before adding a document to it. By simply adding a document with a PUT request, Elasticsearch will take care of creating the index for us, using default settings. While this is very convenient, adding an index in advance gives you more control up-front.
We have now added the first index to our Elasticsearch cluster, and that’s all we set out to accomplish in this article.
Here is what you will learn:
- The architecture of Elasticsearch
- Mappings and analyzers
- Many kinds of search queries (simple and advanced alike)
- Aggregations, stemming, auto-completion, pagination, filters, fuzzy searches, etc.
- ... and much more!
