Elasticsearch Deep Dive into Elasticsearch Endpoints

By Opster Team

Updated: Nov 5, 2023

| 3 min read

Quick Links

Overview

Elasticsearch, a highly scalable open-source full-text search and analytics engine, provides a multitude of endpoints that allow developers to interact with the data stored within the clusters. Elasticsearch exposes these HTTP endpoints to perform various operations such as indexing, searching, updating, and deleting data. This article will delve into the intricacies of Elasticsearch endpoints, their types, and their usage.

Elasticsearch endpoints types

The main Elasticsearch endpoints can be broadly categorized into the following types: Cluster APIs, Index APIs, Document APIs, and Search APIs.

1. Cluster APIs: 

These APIs are used to manage and monitor your Elasticsearch cluster. They provide information about the health, status, and settings of the cluster. Some of the commonly used Cluster APIs include:

  • Cluster Health API: This API is used to get a quick overview of the cluster health. It returns basic index metrics and status information.
  • Cluster Stats API: This API provides comprehensive statistics about the cluster.
  • Cluster Settings API: This API is used to view and change cluster-wide settings.

2. Index APIs: 

These APIs are used to manage individual indices within the Elasticsearch cluster. They allow you to create, delete, and manage your indices. Some of the commonly used Index APIs include:

  – Create Index API: This API is used to create a new index.
  – Delete Index API: This API is used to delete an index.
  – Index Settings API: This API is used to view and change settings of an index.

3. Document APIs: 

These APIs are used to manage the documents within an index. They allow you to add, update, and delete documents. Some of the commonly used Document APIs include:

  • Index API: This API is used to add a new document to an index.
  • Get API: This API is used to get a document by its ID.
  • Delete API: This API is used to delete a document by its ID.
  • Delete by query API: This API is used to delete all documents matching a given query.
  • Update by query API: This API is used to update all documents matching a given query.   
  • Reindex API: This API is used to reindex an old index into a new one.

4. Search APIs: 

These APIs are used to execute search queries on an Elasticsearch cluster. They allow you to perform full-text search and other types of queries. Some of the commonly used Search APIs include:

  • Search API: This API is used to execute a search query and get back search hits.
  • Multi-Search API: This API is used to execute multiple search requests within a single API call.
  • Count API: This API is used to get the number of documents matching a query.

Usage & Examples

Let’s take a look at how to use these APIs with some examples.

Example 1: Using the Cluster Health API

To get the health status of your cluster, you can use the following command:

GET /_cluster/health

Example 2: Using the Create Index API

To create a new index named “test” with specific settings and field mappings, you can use the following command:

PUT /test
{
  "settings": {
     "index.number_of_shards": 1,
     "index.number_of_replicas": 1
  },
  "mappings": {
     "properties": {
        "timestamp": {
           "type": "date"
        },
        "field1": {
           "type": "text"
        },
        "field2": {
           "type": "keyword"
        }
     }
  }
}

Example 3: Using the Index API

To add a new document to the “test” index, you can use the following command:

POST /test/_doc
{
  "field1": "text value",
  "field2": "keyword value"
}

Example 4: Using the Update by query API

To update all documents of the “test” index to add a timestamp field, you can use the following command:

POST /test/_update_by_query
{
  "query": {
    "match": {
      "field1": "some value1"
    }
  },
  "script": {
    "source": "ctx._source.timestamp = ZonedDateTime.toInstant().toEpochMilli()"
  }
}

Example 5: Using the Search API

To execute a search query on the “test” index, you can use the following command:

GET /test/_search
{
  "query": {
    "match": {
      "field1": "some value"
    }
  }
}

Conclusion

In conclusion, Elasticsearch endpoints are a crucial part of interacting with an Elasticsearch cluster. They provide a wide range of functionalities that allow you to manage and manipulate your data effectively. Understanding these endpoints and how to use them is key to leveraging the full potential of Elasticsearch. 

How helpful was this guide?

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?