Unlocking the Power of Elastic Search APIs: A Comprehensive Guide

Bazla Kausar
8 min readApr 3, 2023

Welcome to my blog on Elastic Search REST APIs!
If you’re looking to learn more about Elastic Search and its capabilities, then you’ve come to the right place. In this blog, we’ll dive deep into the Elastic Search REST APIs and explore how they work, how to use them effectively, and some best practices for working with them. We’ll cover topics such as CRUD operations, search queries, aggregations, mapping, and more. So, let’s get started!

Elasticsearch’s robust REST API is one of its best features. There are several instances of using this API to interface with Elasticsearch, involving various businesses and use cases. It gives you many options for integrating, managing, and querying the indexed data. Elasticsearch provides a REST API, which is accessed by JSON over HTTP.
In this blog, we will be discussing the following key points as mentioned in the diagram, providing you with a comprehensive overview and practical insights to help you apply these concepts.

1. DOCUMENT API

Elasticsearch provides single document APIs and multi-document APIs. As a part of this we will be exploring Index API, Delete API & Update API.
Index API : When a request is made to a given index with a specific mapping, the Index API helps to add or change the JSON document within that index.

Request:
POST student/_doc/
{
"student": "shenoy",
"post_date": "2023-01-01T14:12:12",
"message": "adding,don't know why!"
}

Response:
{
"_index" : "student",
"_type" : "_doc",
"_id" : "5",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1
}

Effortless Indexing : Streamlining the Process with Automatic Index Creation

When a request is made to add a JSON object to a certain index, this API will automatically build that index and the underlying mapping for that specific JSON object if that index does not already exist. By setting the following options in the elasticsearch.yml file to false, this capability can be turned off.

action.auto_create_index:false
index.mapper.dynamic:false

GET API : The specified JSON document is retrieved from an index using the GET API. You can retrieve a page from a specific index using GET, along with its source or stored attributes. To confirm the existence of a document, use HEAD. The _source resource allows you to access simply the document source or to confirm that it is actually there.

GET index_1/_doc/0?_source=false

If you’re looking to implement routing and versioning functionalities, be sure to check out the elastic documentation.
DELETE API : By making an HTTP DELETE request to Elasticsearch, you can remove a specific index, mapping, or document.

DELETE index_1/_doc/4

On hitting the above API, we get the following result :

{
"found":true, "_index":"index_1", "_type":"index", "_id":"4", "_version":2,
"_shards":{"total":2, "successful":1, "failed":0}
}

UPDATE API : Versioning is utilised to ensure that no modifications have taken place between the get and re-index operations, which are carried out using script. For instance, you can use script to update the desired details

POST students/_update/4
{
"script" : {
"source": "ctx._source.name = 'Shabby'",
"lang": "painless",
"params" : {
"name" : "shab"
}
}
}

You can check the update by sending get request to the updated document.
The contents of the upsert element are inserted as a new document if one doesn’t already exist. The script runs successfully if the document is present.
You can set doc as upsert to true to use the contents of doc as the upsert value rather than sending a partial doc along with an upsert doc.

POST index_1/_update/1
{
"doc": {
"name": "new_name"
},
"doc_as_upsert": true
}

2. SEARCH API

Elasticsearch content searches are done using Search API. A user can conduct a search by either sending a query in the message body of a post request or by sending a get request with a query string as a parameter.
Multi-Index : We can use Elasticsearch to search for documents that are present in all indices or just a few particular indices. For instance, we can follow these steps if we want to search all documents containing the word Sharma in their names.

GET /_all/_search?q=name:Sharma 

3. INDEX API

You can make an index with the use of this API. An index can either be established beforehand or automatically produced when a user passes JSON objects to any index. You only need to send a PUT request containing settings, mappings, and aliases — or simply a straightforward request without a body — to establish an index.

PUT Students
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "Students"
}

DELETE INDEX : With this API, you can remove any index. Simply pass a delete request together with the name of that specific Index (Just using _all or * will remove all indices)

DELETE /Students
OR
DELETE /_all
OR
DELETE /Students*

INDEX STATS : You can extract statistics about a specific index with the help of this API. Simply include the index URL and the _stats keyword in the body of a get request.

REQUEST:
GET /_stats

RESPONSE:
"request_cache" : {
"memory_size_in_bytes" : 820,
"evictions" : 0,
"hit_count" : 1152,
"miss_count" : 4
},
"recovery" : {
"current_as_source" : 0,
"current_as_target" : 0,
"throttle_time_in_millis" : 0
}

4. MAPPING API

The layout of the documents kept in an index is known as mapping. It specifies the fields’ formats, data types (such as geo point or string), and rules that govern how dynamically added fields are mapped.

REQUEST:
PUT Students
{
"mappings":{
"properties":{
"name": { "type":"text"}, "date":{ "type":"date"},
"Class":{ "type":"text"}, "marks":{ "type":"double"}
}
}
}
RESPONSE:
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "Students"
}

Then, based on the matching criterion, you can use dynamic templates to construct unique mappings that can be applied to dynamically added fields.

5. CLUSTER API

The cluster API is used to modify the nodes in the cluster and obtain information about them. We must provide the node name, address, or _local when calling this API.
NODE SPECIFICATIONS
👉 _all, to add all nodes to the subset
👉 _local, to add the local node to the subset
👉 node id or name, to add this node to the subset
👉 _master, to add the currently-elected master node to the subset
👉 All master-eligible nodes, all data nodes, all ingest nodes, all voting-only nodes, all machine learning nodes, and all coordinating-only nodes are added to the subset, respectively, by the values master:true, data:true, ingest:true, voting only:true, ml:true, or coordinating only:true.
👉 All master-eligible nodes, all data nodes, all ingest nodes, all voting-only nodes, all machine learning nodes, and all coordinating-only nodes are each removed from the subset when one of the following values is specified; master:false, data:false, ingest:false, voting only:true, ml:false, or coordinating only:false.

6. QUERY DSL

Elasticsearch uses JSON-based querying to carry out its searching. Two clauses — leaf query clauses and compound query clauses — make up a DSL query.
Leaf Query Clause : Leaf query clauses, like match, phrase, or range inquiries, search for a specific value in a specific pitch.

Compound Query Clause : Whether combining numerous queries logically (as in the bool or dis max query) or changing their behaviour (as in the constant score query), compound query clauses wrap other leaf or compound queries.
Elasticsearch can handle a multitude of distinct types of queries. A query begins with a query key word and then includes conditions and filters as JSON objects inside. The various query types have been detailed below.

➾ Match All Query

This is the simplest that returns all of its content with a score of 1.0 for each object.
The inverse of the match_all query, is match_none which matches no documents.

POST /students/_search
{
"query":{
"match_all":{}
}
}
OR
POST /students/_search
{
"query":{
"match_none":{}
}
}

➾ Full Text Query
With these queries, you can search a substantial body of text. This query operates in accordance with the analyzer connected to that specific index or document. We will go over the various kinds of full text queries in this section.

📍Match Query : This query compares the values of one or more fields to a text or phrase.

POST /students*/_search
{
"query":{
"match" : {
"marks":"60"
}
}
}

📍Multi Match Query : This search identifies text or phrases that appear in multiple fields.

POST /students*/_search
{
"query":{
"multi_match" : {
"query": "Banaras",
"fields": [ "city", "state" ]
}
}
}

You will see the document when you run this search if Banaras is listed in either the city or state fields.
📍String Query : This query makes use of the query_string keyword and query parser. on hitting this query, you will get the occurance of this string in your Elastic Index.

POST /students*/_search
{
"query":{
"query_string":{
"query":"functional"
}
}
}

📍Term Query : The majority of these queries deal with structured data like integers, dates, and enums.

POST /students*/_search
{
"query":{
"term":{"zip":"206001"}
}
}

📍Range Query : This search is utilized to locate objects that possess values within a specified range. To achieve this, we utilize operators including:

gte : stands for greater than or equal to
gt : stands for greater than
lte : stands for less than or equal to
lt : stands for less than

GET /students*/_count
{
"query": {
"bool": {
"filter": {
"range": {
"date" : {
"gte": "2023-01-15",
"lt" : "2023-01-16"
}
}
}
}
}
}

📍Compound Query : These queries are a group of various searches that have been combined utilising Boolean operators such as and, or, not, or for various indices, containing function calls, etc.

POST /students/_search
{
"query": {
"bool" : {
"must" : {
"term" : { "state" : "UP" }
},
"filter": {
"term" : { "rating" : "5" }
},
"minimum_should_match" : 1,
"boost" : 1.0
}
}
}

📍Geo Query : This query revolves around geo locations and geo points. These searches assist in locating schools or any other nearby geographic item. You need to use geo point data type.

WRAPPING UP…

To conclude, Elastic search provides an extensive set of REST APIs that can be used to interact with the search engine. These APIs enable developers to create, read, update, and delete data in Elastic search. With the help of Elastic search’s powerful search capabilities, developers can create high-performance applications that can search through large amounts of data in real-time.

In this blog, we have covered the basics of Elastic search REST APIs, including how to use them to create, read, update, and delete documents in an index. We have also looked at some common scenarios where REST APIs can be used to interact with Elastic search, such as performing searches, mappings etc.

If you are interested in learning more about Elastic search, please refer to my previous Introductory blogs on the Elastic Search where I have covered a wide range of Elastic search topics and provide a great starting point for developers looking to get started with this powerful search engine.

Reference Links:
🔗 Elastic Search 101
🔗 Mastering Elastic Search Index Management : Tips & Tricks

--

--

Bazla Kausar

A data enthusiast, trying to be a productive organ of Technology.