DEV Community

Cover image for OpenSearch CRUD operation with curl
Ankit malik
Ankit malik

Posted on

OpenSearch CRUD operation with curl

Introduction:

OpenSearch, an open-source search and analytics engine, provides a powerful platform for storing, indexing, and searching data. Its flexible architecture enables developers to interact with the engine using various programming languages and tools. In this article, we will explore how to perform CRUD (Create, Read, Update, Delete) operations in OpenSearch using the popular command-line tool cURL.

Prerequisites:

To follow the examples in this article, ensure that cURL is installed on your system and that you have access to an OpenSearch cluster.

Understanding OpenSearch and Elasticsearch Relationship:

OpenSearch and Elasticsearch share a common history, with OpenSearch being developed as a community-driven project to offer an open-source alternative to Elasticsearch. OpenSearch is designed to be compatible with Elasticsearch's API, it provide users with a familiar experience. This means that many concepts and operations used in Elasticsearch can be directly applied to OpenSearch.

Creating an Index:

To start, let's create an index in OpenSearch using cURL. Creating an index in OpenSearch is similar to Elasticsearch. An index serves as a logical container for holding a collection of documents and allows us to define the schema and settings for the index. Here's an example of creating an index named "dev-article":

curl -XPUT "http://localhost:9200/dev-article" -H 'Content-Type: application/json' -d '
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 0
  },
  "mappings": {
    "properties": {
      "title": {
        "type": "text"
      },
      "content": {
        "type": "text"
      }
    }
  }
}'
Enter fullscreen mode Exit fullscreen mode

In this example, we specify the index name as "dev-article" and define two fields, "title" and "content," along with their respective data types. This operation closely resembles the process of creating an index in Elasticsearch.

We can create an index without mappings field and when we will add the document this mappings data will be automatically updated based on the fields present in the document.

Indexing or Adding a Document:

Once the index is created, we can add documents to it. Indexing a document in OpenSearch follows similar principles as Elasticsearch. A document is a JSON object representing a record within the index. Let's index a sample article:

curl -XPOST "http://localhost:9200/dev-article/_doc" -H 'Content-Type: application/json' -d '
{
  "title": "Getting Started with OpenSearch",
  "content": "OpenSearch is a powerful open-source search and analytics engine..."
}'
Enter fullscreen mode Exit fullscreen mode

In this example, we specify the index name as "dev-article" and use the _doc type. We provide a JSON document containing the article's title and content. This process aligns with the indexing operation in Elasticsearch.

Retrieving a Document:

Retrieving a document from OpenSearch is similar to Elasticsearch. We can use the GET method along with the document's ID. Here's an example:

curl -XGET "http://localhost:9200/dev-article/_doc/{document_id}"
Enter fullscreen mode Exit fullscreen mode

Replace {document_id} with the actual ID of the document you wish to retrieve. This operation remains consistent with Elasticsearch.

Updating a Document:

Updating a document in OpenSearch is also similar to Elasticsearch. We send a partial or complete JSON object with the updated values to the OpenSearch API. We use the POST method with the _update endpoint. Here's an example of updating the content of an article:

curl -XPOST "http://localhost:9200/dev-article/_doc/{document_id}/_update" -H 'Content-Type: application/json' -d '
{
  "doc": {
    "content": "OpenSearch is a powerful open-source search and analytics platform..."
  }
}'
Enter fullscreen mode Exit fullscreen mode

In this example, we specify the document's ID and provide a JSON object with the updated field and its new value. This approach aligns with Elasticsearch's update operation.

Deleting a Document:

Deleting a document from OpenSearch is analogous to Elasticsearch. We can use the DELETE method along with the document's ID. Here's an example:

curl -XDELETE "http://localhost:9200/dev-article/_doc/{document_id}"
Enter fullscreen mode Exit fullscreen mode

Replace {document_id} with the actual ID of the document you wish to delete. This operation remains consistent with Elasticsearch's document deletion process.

Conclusion:

In this article, we explored how to perform CRUD operations with OpenSearch using cURL, while emphasizing its relationship with Elasticsearch. OpenSearch, as a community-driven fork of Elasticsearch, offers a similar experience and API compatibility. By leveraging cURL and OpenSearch's RESTful API, developers can interact with the search engine and build powerful search and analytics applications. It's important to note that while OpenSearch and Elasticsearch share a common history, they are separate projects with their own communities. Understanding this relationship enables users to effectively transition between the two platforms. Happy searching!

Top comments (0)