Elasticsearch Index APIs

Elasticsearch provides Index API that manages all the aspects of an index, such as index template, mapping, aliases, and settings, etc. In Elasticsearch, Index API performs the operation at the index level. It is responsible for managing different indices, index settings, index templates, mapping, file format, and aliases.

When a request is made for an index with specific mapping, this API helps to add or update the JSON document in that respective index. It allows to perform number of operations on indexes such as - create, delete, analyze, refresh, flush, and many more.

There is a list of operations that we perform on Indexes using some APIs. We will discuss each of them in detail with example:

Create Index

This API is used to create an index. An index can be created by sending a PUT request without body or with proper mapping, setting, and alias. An index is created automatically whenever a user passes the JSON objects to any index.

In this below example, we will create an index named book.

For example

By executing this query, we will get below response-

Screenshot

Elasticsearch Index APIs

With Setting -

We can also add some setting like shards or replicas in query while creating index -

By executing this query, we will get below response-

Screenshot

Elasticsearch Index APIs

Or with mapping -

By executing this query, we will get below response-

Screenshot

Elasticsearch Index APIs

Note that here we used POST method to create index with mapping whereas in earlier simple index creation, we used PUT method.

Get Index

Get API is used to provide information about a particular index you want. It fetches the information of index. This API is called by just sending a GET request along with index name, which returns all the information about that particular index. It returns metadata with Get request. Look at the example below

For example

By executing this query, we will get below response-

Screenshot

Elasticsearch Index APIs

If you want to get information about all indexes, use _all or * in place of index name.

Delete Index

The delete API is responsible for deleting any index present in elasticsearch. Whenever you need to remove any index, pass the delete API along with index name.

You can also delete all indexes present in elasticsearch by passing _all or * at once.

For example

By executing this query, we will get below response-

Screenshot

Elasticsearch Index APIs

Open/Close Index

We can close an index for some time or later can open it. The open/close API allows us to perform these operations on indexes. A closed index means that the index is blocked for read/write operation and there is no overhead on cluster except maintaining its metadata. This closed index can be opened whenever you want and start the normal process.

See the below steps how open and close an index -

1. Go to the overview tab that contains the number of indices created by you. Here, you will see two drop-down buttons Info and Action with each index.

Elasticsearch Index APIs

2. Click on the Action button whose index you want to close and then on Close button.

Elasticsearch Index APIs

3. A Popup will show up to you where click on the OK button.

Elasticsearch Index APIs

4. Now, you will see your selected index is closed.

Elasticsearch Index APIs

5. Click on the Action drop-down list to reopen the closed index and click on the Open button.

Elasticsearch Index APIs

You can also perform some other operations such as delete, refresh, or flush index using this interface.

6. A screen will pop up to open the index where click on the OK button.

Elasticsearch Index APIs

7. Now, you can see that the index starts working normally.

Elasticsearch Index APIs

Index Exist

By sending a get request, the existence of an index can be determined. If the HTTP response is 202, index exists and if the return response is Error 404, the index does not exist.

An index can also be opened and closed using a query request. See the example below -

Close index

By executing this query, we will get below response-

Screenshot

Elasticsearch Index APIs

Open index

By executing this query, we will get below response-

Screenshot

Elasticsearch Index APIs

Index Aliases

This API allows us to create an alias for an index. To create a simple alias, use _alias keyword or for complex alias use _aliases. Remember that an alias name cannot be the same as the index name. So, always provide a different name to new aliases. See the following example given below -

For example

Copy Code

By executing this query, we will get below response-

Screenshot

Elasticsearch Index APIs

Get aliases

Now, you can get back the information about created aliases book_pri using GET request. Execute the below code to get all information for aliases -

Copy Code

By executing this query, we will get below response-

Screenshot

Elasticsearch Index APIs

Remove aliases

Removing an alias is similar to adding an alias to an index. We just only need to put remove keyword at the place of add and execute the request. Look at the example given below -

Copy Code

By executing this query, we will get below response-

Screenshot

Elasticsearch Index APIs

Analyze

This API helps us to analyze the text passed in a variable. It breaks the text string and returns the tokens with token value and offset value (i.e., start offset and end offset). In elasticsearch, it allows the user to perform analysis without specifying any index. However, we can also analyze the text with an index where the text will be analysed according to the analyzer associated with the index.

Look at the following example for analysing the text without specifying any index -

Copy Code

By executing this query, we will get below response-

Screenshot

Elasticsearch Index APIs

Index Template

Index templates are applied automatically when an index created. We can also define templates for index using index template API. See the example given below -

Copy Code

After defining this template, any index starts with te will have the same setting as template1.

Index Settings

Append _settings keyword at the end of the URL to get index settings. See the example given below -

Copy Code

By executing this query, we will get below response-

Screenshot

Elasticsearch Index APIs

Index Stats

Stats is stands for Statistics. Sometimes, we require to analyze the statistics for a particular index. This API helps to find the statistics for the index you want. For this, we just need to use GET method and append the _stats keyword along with index name in URL and then execute the query. See the example given below -

Copy Code

By executing this query, we will get below response-

Screenshot

Elasticsearch Index APIs

Index Flush

  • The flush API is used to clean the memory of index as well as internal transaction logs.
  • It also migrates the data to index storage.
  • To perform this operation on an index, use the _flush keyword. Basically, it helps to flush all the garbage data from index memory.

Look at the example given below -

Copy Code

By executing this query, we will get below response-

Screenshot

Elasticsearch Index APIs

Index Refresh

  • In elasticsearch, refresh is scheduled automatically but you can also refresh the individual index manually using this API.
  • The _refresh API makes all the data available until last operation performed.
  • It allows to explicitly refresh one or more indices. Specify the index name you want to refresh and append the _refresh API in URL and execute the query request.
  • Don't forget to use GET method for successful execution of query.

Look at the example given below -

Copy Code

By executing this query, we will get below response-

Screenshot

Elasticsearch Index APIs

These were some number of operation that we explained in detail comes under the Index API.