![]() |
VOOZH | about |
Elasticsearch is a powerful tool for managing and analyzing data, offering a RESTful API that allows developers to interact with it using simple HTTP requests.
This API is built on the principles of Representational State Transfer (REST) making it accessible and intuitive for developers of all levels of expertise. In this article, We will learn about the How to Interacting with Elasticsearch via REST API with the help of examples in detail.
Before we perform the examples of interacting with Elasticsearch via its REST API, let's cover some basic concepts:
To understand the Interacting with Elasticsearch in well manner. We will consider below sample documents to perform operations and queries for better understanding.
[
{
"_id": 1,
"name": "Elasticsearch Beginner's Guide",
"price": 29.99,
"category": "Books"
},
{
"_id": 2,
"name": "Learning Elasticsearch",
"price": 39.99,
"category": "Books"
},
{
"_id": 3,
"name": "Mastering Elasticsearch",
"price": 49.99,
"category": "Books"
},
{
"_id": 4,
"name": "Elasticsearch in Action",
"price": 34.99,
"category": "Books"
},
{
"_id": 5,
"name": "Advanced Elasticsearch Techniques",
"price": 44.99,
"category": "Books"
},
{
"_id": 6,
"name": "The Definitive Guide to Elasticsearch",
"price": 59.99,
"category": "Books"
}
]Suppose we have to Add a new book to the library.
POST /products/_doc/1
{
"name": "Elasticsearch Beginner's Guide",
"price": 29.99,
"category": "Books"
}Response:
{
"result": "created",
"_index": "products",
"_id": "1",
"_version": 1,
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}Use the POST method to send the new book's details to Elasticsearch, specifying the index name and unique ID for the document.
Suppose we have to Find all books in the library with "Elasticsearch" in their title.
GET /products/_search
{
"query": {
"match": {
"name": "Elasticsearch"
}
}
}Response:
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.15342641,
"hits": [
{
"_index": "products",
"_id": "1",
"_score": 0.15342641,
"_source": {
"name": "Elasticsearch Beginner's Guide",
"price": 29.99,
"category": "Books"
}
}
]
}
}
Use the GET method to search the index for documents where the "name" field matches the term "Elasticsearch".
Suppose we have to update the price of a book in the library.
POST /products/_update/1
{
"doc": {
"price": 39.99
}
}Response:
{
"_index": "products",
"_id": "1",
"_version": 2,
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1
}
Use the POST method to update the price of the book by specifying its ID and the new price
Suppose we have to Remove a book from the library.
DELETE /products/_doc/1Response:
{
"_index": "products",
"_id": "1",
"_version": 3,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 1
}
Use the DELETE method to delete the document by specifying its ID.
Overall, the Elasticsearch REST API provides a straightforward and effective means of interacting with Elasticsearch and allowing developers to manage data with ease. By understanding the fundamental concepts of indexes, documents, queries, and mappings, developers can take help from the full potential of Elasticsearch for their projects. With examples defining common operations like indexing, searching, updating, and deleting documents, this guide has provided a solid foundation for working with Elasticsearch's REST API