How to do CRUD operations in OpenSearch using Java SDK

Here we will learn how to do common CRUD operations in OpenSearch using Java SDK. Basically we will learn how to create, update, index, get & delete document in OpenSearch from a Java application. By the way, this post is regarding CRUD operations on single document. For doing OpenSearch bulk operations in Java, I have already written a post earlier. You can refer to it if needed.

Create Document CURL Request:

PUT index/_create/1
{ "message": "testing create" }

Update Document CURL Request:

POST index/_update/1

{ "message": "testing update" }

Index Document CURL Request:

POST index/_doc/1
{ "message": "testing index" }

Get Document CURL Request:

GET index/_doc/1

Delete Document CURL Request:

DELETE index/_doc/1

We want to convert above CRUD operations to Java code. For Gradle project, we need below dependencies in build.gradle file. Feel free to change the versions to what you are planning to use.

	implementation 'org.opensearch.client:opensearch-rest-client: 2.3.0'
	implementation 'org.opensearch.client:opensearch-java:2.0.0'

For Maven projects, we need to add the above dependencies in pom.xml file.

Here is the complete Java program that emulates the above create, update, index, get & delete operations in OpenSearch. The code is simple & easy to understand. I have tried to cover the barebone minimum that is required to create the CRUD request successfully & also added the fields from response which should be generally useful for common use cases. You should be able to write your own Java code easily using this post as reference.

import java.io.IOException;
import java.util.Map;

import org.opensearch.client.opensearch.OpenSearchClient;
import org.opensearch.client.opensearch._types.OpenSearchException;
import org.opensearch.client.opensearch.core.CreateRequest;
import org.opensearch.client.opensearch.core.CreateResponse;
import org.opensearch.client.opensearch.core.DeleteRequest;
import org.opensearch.client.opensearch.core.DeleteResponse;
import org.opensearch.client.opensearch.core.GetRequest;
import org.opensearch.client.opensearch.core.GetResponse;
import org.opensearch.client.opensearch.core.IndexRequest;
import org.opensearch.client.opensearch.core.IndexResponse;
import org.opensearch.client.opensearch.core.UpdateRequest;
import org.opensearch.client.opensearch.core.UpdateResponse;

public class OpenSearchServiceImpl {

	private OpenSearchClient client = OpenSearchClientFactory.getInstance();

	public void doCRUDOperations() throws OpenSearchException, IOException {

		String index = "index";
		Map<String, Object> doc = Map.of("message", "testing create");
		String docId = "1";

		// create document
		CreateRequest<Map> createRequest = new CreateRequest.Builder<Map>().index(index).id(docId).document(doc)
				.build();
		CreateResponse createResponse = client.create(createRequest);
		System.out.println("created: " + createResponse.result().name() + " " + createResponse.version());

		// update document
		Map<String, Object> updatedField = Map.of("message", "testing update");
		UpdateRequest<Map, Map> updateRequest = new UpdateRequest.Builder<Map, Map>().index(index).id(docId)
				.doc(updatedField).build();
		UpdateResponse<Map> updateResponse = client.update(updateRequest, Map.class);
		System.out.println("updated: " + updateResponse.result().name() + " " + updateResponse.version());

		// index document
		Map<String, Object> indexedDoc = Map.of("message", "testing index");
		IndexRequest<Map> indexRequest = new IndexRequest.Builder<Map>().index(index).id(docId).document(indexedDoc)
				.build();
		IndexResponse indexResponse = client.index(indexRequest);
		System.out.println("indexed: " + indexResponse.result().name() + " " + indexResponse.version());

		// get document
		GetRequest getRequest = new GetRequest.Builder().index(index).id(docId).build();
		GetResponse<Map> getResponse = client.get(getRequest, Map.class);
		Map storedDoc = getResponse.source();
		System.out.println("get: " + getResponse.found() + " " + storedDoc);

		// delete document
		DeleteRequest deleteRequest = new DeleteRequest.Builder().index(index).id(docId).build();
		DeleteResponse deleteResponse = client.delete(deleteRequest);
		System.out.println("delete: " + deleteResponse.result().name());

	}
}

Leave a Comment