Elasticsearch Index APIsElasticsearch 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 IndexThis 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 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 Or with mapping - By executing this query, we will get below response- Screenshot Note that here we used POST method to create index with mapping whereas in earlier simple index creation, we used PUT method.Get IndexGet 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 If you want to get information about all indexes, use _all or * in place of index name. Delete IndexThe 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 Open/Close IndexWe 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. 2. Click on the Action button whose index you want to close and then on Close button. 3. A Popup will show up to you where click on the OK button. 4. Now, you will see your selected index is closed. 5. Click on the Action drop-down list to reopen the closed index and click on the Open button. 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. 7. Now, you can see that the index starts working normally. Index ExistBy 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 Open index By executing this query, we will get below response- Screenshot Index AliasesThis 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 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 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 AnalyzeThis 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 Index TemplateIndex 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 SettingsAppend _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 Index StatsStats 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 Index Flush
Look at the example given below - Copy Code By executing this query, we will get below response- Screenshot Index Refresh
Look at the example given below - Copy Code By executing this query, we will get below response- Screenshot These were some number of operation that we explained in detail comes under the Index API. Next TopicElasticsearch Cluster APIs |