All Products
Search
Document Center

PolarDB:Connect to PolarSearch by using a client

Last Updated:Apr 21, 2026

PolarSearch is fully compatible with official OpenSearch clients. You can use popular programming languages, such as Java and Python, to manage indexes, perform document operations (including creating, reading, updating, and deleting), run complex searches, and seamlessly integrate the search feature into your application.

Prerequisites

  1. A cluster with a PolarSearch node has been created, with an administrator account for the node configured.

  2. Obtain a Connection Address: In the Database Nodes section of the cluster, hover over the Search Node to obtain the private or public address of the PolarSearch node based on your business environment.

Connect to a cluster

OpenSearch Java client

The OpenSearch Java client lets you interact with an OpenSearch cluster by using Java methods and data structures instead of HTTP methods and raw JSON. For example, you can submit requests as objects and use the client's built-in methods to create an index, index documents, or perform other operations. For a full API reference and more examples, see the javadoc.

1. Select a transport and add dependencies

The OpenSearch Java client requires a transport framework to handle HTTP requests.

  • PolarSearch 1.x: The Java client supports only the RestClient Transport.

  • PolarSearch 3.x: The Java client can use either the Apache HttpClient 5 Transport or the RestClient Transport.

PolarSearch 1.x

Maven example: Add the following dependencies to your pom.xml file:

<!-- OpenSearch Java client core library -->
<dependency>
    <groupId>org.opensearch.client</groupId>
    <artifactId>opensearch-java</artifactId>
    <version>1.0.0</version>
</dependency>
<!-- RestClient Transport -->
<dependency>
    <groupId>org.opensearch.client</groupId>
    <artifactId>opensearch-rest-client</artifactId>
    <version>1.3.20</version>
</dependency>

PolarSearch 3.x

Apache HttpClient 5 Transport Maven example: Add the following dependencies to your pom.xml file:

<!-- OpenSearch Java client core library -->
<dependency>
    <groupId>org.opensearch.client</groupId>
    <artifactId>opensearch-java</artifactId>
    <version>3.3.0</version>
</dependency>
<!-- Apache HttpClient 5 transport -->
<dependency>
    <groupId>org.apache.httpcomponents.client5</groupId>
    <artifactId>httpclient5</artifactId>
    <version>5.4.3</version>
</dependency>

2. Define a data class

Create a test data class to use in the following PolarSearch examples.

static class IndexData {
    private String title;
    private String text;

    public IndexData() {}

    public IndexData(String title, String text) {
        this.title = title;
        this.text = text;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    @Override
    public String toString() {
        return String.format("IndexData{title='%s', text='%s'}", title, text);
    }
}

3. Initialize the client

Based on the transport you selected, use your PolarSearch connection details to initialize the client. In a production environment, you can set these details as environment variables. For testing, you can also assign them as default parameter values.

PolarSearch 1.x

The following example shows how to initialize a client using the RestClient Transport. This example disables SSL.

public static OpenSearchTransport createTransport() throws Exception {
    var env = System.getenv();
    var hostname = env.getOrDefault("HOST", "<polarsearch_host>");
    var port = Integer.parseInt(env.getOrDefault("PORT", "<polarsearch_port>"));
    var scheme = env.getOrDefault("SCHEME", "http");
    var user = env.getOrDefault("USERNAME", "<polarsearch_username>");
    var pass = env.getOrDefault("PASSWORD", "<polarsearch_password>");

    final HttpHost host = new HttpHost(hostname, port, scheme);
    final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials(user, pass));

    final SSLContext sslContext = SSLContextBuilder.create()
            .loadTrustMaterial(null, (chains, authType) -> true)
            .build();

    RestClientBuilder builder = RestClient.builder(host)
            .setHttpClientConfigCallback(httpClientBuilder ->
                    httpClientBuilder
                            .setDefaultCredentialsProvider(credentialsProvider)
                            .setSSLContext(sslContext));

    final RestClient restClient = builder.build();
    return new RestClientTransport(restClient, new JacksonJsonpMapper());
}

transport = createTransport();
var client = new OpenSearchClient(transport);

PolarSearch 3.x

The following example shows how to initialize a client using the Apache HttpClient 5 Transport. This example disables SSL.

public static OpenSearchTransport createTransport() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
    var env = System.getenv();
    var hostname = env.getOrDefault("HOST", "<polarsearch_host>");
    var port = Integer.parseInt(env.getOrDefault("PORT", "<polarsearch_port>"));
    var scheme = env.getOrDefault("SCHEME", "http");
    var user = env.getOrDefault("USERNAME", "<polarsearch_username>");
    var pass = env.getOrDefault("PASSWORD", "<polarsearch_password>");

    final HttpHost host = new HttpHost(scheme, hostname, port);
    final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(new AuthScope(host), new UsernamePasswordCredentials(user, pass.toCharArray()));

    final SSLContext sslContext = SSLContextBuilder
            .create()
            .loadTrustMaterial(null, (chains, authType) -> true)
            .build();

    final ApacheHttpClient5TransportBuilder builder = ApacheHttpClient5TransportBuilder.builder(host);
    builder.setHttpClientConfigCallback(httpClientBuilder -> {
        final TlsStrategy tlsStrategy = ClientTlsStrategyBuilder.create()
                .setSslContext(sslContext)
                .setTlsDetailsFactory(new Factory<SSLEngine, TlsDetails>() {
                    @Override
                    public TlsDetails create(final SSLEngine sslEngine) {
                        return new TlsDetails(sslEngine.getSession(), sslEngine.getApplicationProtocol());
                    }
                })
                .build();

        final PoolingAsyncClientConnectionManager connectionManager = PoolingAsyncClientConnectionManagerBuilder
                .create()
                .setTlsStrategy(tlsStrategy)
                .build();

        return httpClientBuilder
                .setDefaultCredentialsProvider(credentialsProvider)
                .setConnectionManager(connectionManager);
    });

    final OpenSearchTransport transport = builder.build();
    return transport;
}

transport = createTransport();
var client = new OpenSearchClient(transport);

4. Perform basic operations

The following code snippets demonstrate how to perform common index and document operations.

Create an index

// 1. Creating an index
final var index = "my-index";
if (!client.indices().exists(r -> r.index(index)).value()) {
    CreateIndexRequest createIndexRequest = new CreateIndexRequest.Builder().index(index)
            .build();
    client.indices().create(createIndexRequest);
}

Index a document

// 2. Indexing data
IndexData indexData = new IndexData("first_name", "Bruce");
IndexRequest<IndexData> indexRequest = new IndexRequest.Builder<IndexData>().index(index).id("1").document(indexData).build();
client.index(indexRequest);

Search for documents

// 3. Searching for documents
SearchResponse<IndexData> searchResponse = client.search(s -> s.index(index), IndexData.class);
for (int i = 0; i< searchResponse.hits().hits().size(); i++) {
    LOGGER.info(searchResponse.hits().hits().get(i).source());
}        

Delete a document

// 4. Deleting a document
client.delete(b -> b.index(index).id("1"));

Delete an index

// 5. Deleting an index
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest.Builder().index(index).build();
DeleteIndexResponse deleteIndexResponse = client.indices().delete(deleteIndexRequest);

OpenSearch Java high-level REST client

We recommend using the OpenSearch Java client. The Java high-level REST client is deprecated in OpenSearch and will be removed in a future version.

1. Add dependencies

Add the following dependency to your pom.xml file:

PolarSearch 1.x

<!-- OpenSearch Java rest high-level client core library -->
<dependency>
    <groupId>org.opensearch.client</groupId>
    <artifactId>opensearch-rest-high-level-client</artifactId>
    <version>1.3.20</version>
</dependency>

PolarSearch 3.x

<!-- OpenSearch Java rest high-level client core library -->
<dependency>
    <groupId>org.opensearch.client</groupId>
    <artifactId>opensearch-rest-high-level-client</artifactId>
    <version>3.3.2</version>
</dependency>

2. Initialize the client

In a production environment, you can set these details as environment variables. For testing, you can also assign them as default parameter values.

PolarSearch 1.x

The following example shows how to initialize a version 1.x client. This example disables SSL.

public static RestHighLevelClient createClient() throws Exception {
    var env = System.getenv();
    var hostname = env.getOrDefault("HOST", "<polarsearch_host>");
    var port = Integer.parseInt(env.getOrDefault("PORT", "<polarsearch_port>"));
    var scheme = env.getOrDefault("SCHEME", "http");
    var user = env.getOrDefault("USERNAME", "<polarsearch_username>");
    var pass = env.getOrDefault("PASSWORD", "<polarsearch_password>");

    final HttpHost host = new HttpHost(hostname, port, scheme);
    final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials(user, pass));

    final SSLContext sslContext = SSLContextBuilder.create()
            .loadTrustMaterial(null, (chains, authType) -> true)
            .build();

    RestClientBuilder builder = RestClient.builder(host)
            .setHttpClientConfigCallback(httpClientBuilder ->
                    httpClientBuilder
                            .setDefaultCredentialsProvider(credentialsProvider)
                            .setSSLContext(sslContext));

    return new RestHighLevelClient(builder);
}

RestHighLevelClient client = createClient();

PolarSearch 3.x

The following example shows how to initialize a version 3.x client. This example disables SSL.

public static RestHighLevelClient createClient() throws Exception {
    var env = System.getenv();
    var hostname = env.getOrDefault("HOST", "<polarsearch_host>");
    var port = Integer.parseInt(env.getOrDefault("PORT", "<polarsearch_port>"));
    var scheme = env.getOrDefault("SCHEME", "http");
    var user = env.getOrDefault("USERNAME", "<polarsearch_username>");
    var pass = env.getOrDefault("PASSWORD", "<polarsearch_password>");

    final HttpHost host = new HttpHost(scheme, hostname, port);
    final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(new AuthScope(host),
            new UsernamePasswordCredentials(user, pass.toCharArray()));

    final SSLContext sslContext = SSLContextBuilder.create()
            .loadTrustMaterial(null, (chains, authType) -> true)
            .build();

    final TlsStrategy tlsStrategy = ClientTlsStrategyBuilder.create()
            .setSslContext(sslContext)
            .build();

    final PoolingAsyncClientConnectionManager connectionManager = PoolingAsyncClientConnectionManagerBuilder
            .create()
            .setTlsStrategy(tlsStrategy)
            .build();

    RestClientBuilder builder = RestClient.builder(host)
            .setHttpClientConfigCallback(httpClientBuilder ->
                    httpClientBuilder
                            .setDefaultCredentialsProvider(credentialsProvider)
                            .setConnectionManager(connectionManager));

    return new RestHighLevelClient(builder);
}

RestHighLevelClient client = createClient();

3. Perform basic operations

The following code snippets demonstrate how to perform common index and document operations.

Create an index

// 1. Creating an index
final var index = "my-index";
boolean exists = client.indices().exists(new GetIndexRequest(index), RequestOptions.DEFAULT);
if (!exists) {
    CreateIndexRequest createIndexRequest = new CreateIndexRequest(index);
    client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
}

Index a document

// 2. Indexing data
IndexData indexData = new IndexData("first_name", "Bruce");
Map<String, Object> document = new HashMap<>();
document.put("title", indexData.getTitle());
document.put("text", indexData.getText());
IndexRequest indexRequest = new IndexRequest(index).id("1").source(document);
client.index(indexRequest, RequestOptions.DEFAULT);

Search for documents

// 3. Searching for documents
SearchRequest searchRequest = new SearchRequest(index);
searchRequest.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
for (SearchHit hit : searchResponse.getHits().getHits()) {
    Map<String, Object> sourceMap = hit.getSourceAsMap();
    IndexData data = new IndexData((String) sourceMap.get("title"), (String) sourceMap.get("text"));
    LOGGER.info(data);
}

Delete a document

// 4. Deleting a document
client.delete(new DeleteRequest(index, "1"), RequestOptions.DEFAULT);

Delete an index

// 5. Deleting an index
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(index);
AcknowledgedResponse deleteIndexResponse = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);

ElasticSearch Java High Level REST Client

PolarSearch 1.x is fully compatible with ElasticSearchJava High Level REST Client versions 7.0.0 to 7.13.4. If you are using a compatible client version, you can change the connection address without modifying your application code.

1. Add dependencies

Add the following dependency to your pom.xml file:

<!-- ElasticSearch Java rest high-level client core library -->
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.13.4</version>
</dependency>

2. Initialize the client

In a production environment, you can set these details as environment variables. For testing, you can also assign them as default parameter values.

The following example shows how to initialize a version 1.x client. This example disables SSL.

public static RestHighLevelClient createClient() throws Exception {
    var env = System.getenv();
    var hostname = env.getOrDefault("HOST", "<polarsearch_host>");
    var port = Integer.parseInt(env.getOrDefault("PORT", "<polarsearch_port>"));
    var scheme = env.getOrDefault("SCHEME", "http");
    var user = env.getOrDefault("USERNAME", "<polarsearch_username>");
    var pass = env.getOrDefault("PASSWORD", "<polarsearch_password>");

    final HttpHost host = new HttpHost(hostname, port, scheme);
    final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials(user, pass));

    final SSLContext sslContext = SSLContextBuilder.create()
            .loadTrustMaterial(null, (chains, authType) -> true)
            .build();

    RestClientBuilder builder = RestClient.builder(host)
            .setHttpClientConfigCallback(httpClientBuilder ->
                    httpClientBuilder
                            .setDefaultCredentialsProvider(credentialsProvider)
                            .setSSLContext(sslContext));

    return new RestHighLevelClient(builder);
}

RestHighLevelClient client = createClient();

3. Perform basic operations

The following code snippets demonstrate how to perform common index and document operations.

Create an index

// 1. Creating an index
final var index = "my-index";
boolean exists = client.indices().exists(new GetIndexRequest(index), RequestOptions.DEFAULT);
if (!exists) {
    CreateIndexRequest createIndexRequest = new CreateIndexRequest(index);
    client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
}

Index a document

// 2. Indexing data
IndexData indexData1 = new IndexData("first_name", "Bruce");
Map<String, Object> document1 = new HashMap<>();
document1.put("title", indexData1.getTitle());
document1.put("text", indexData1.getText());
client.index(new IndexRequest(index).id("1").source(document1), RequestOptions.DEFAULT);

Search for documents

// 3. Searching for documents
SearchRequest searchRequest = new SearchRequest(index);
searchRequest.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
for (SearchHit hit : searchResponse.getHits().getHits()) {
    Map<String, Object> sourceMap = hit.getSourceAsMap();
    IndexData data = new IndexData((String) sourceMap.get("title"), (String) sourceMap.get("text"));
    LOGGER.info(data);
}

Delete a document

// 4. Deleting a document
client.delete(new DeleteRequest(index, "1"), RequestOptions.DEFAULT);

Delete an index

// 5. Deleting an index
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(index);
AcknowledgedResponse deleteIndexResponse = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);

Low-level Python client

We recommend using the low-level Python client because the high-level Python client was deprecated in OpenSearch 2.1.0.

1. Set up environment

  1. Navigate to your project directory. This example uses /home/PolarSearchTestPython.

    mkdir /home/PolarSearchTestPython
    cd /home/PolarSearchTestPython
  2. In the /home/PolarSearchTestPython directory, create a virtual environment (venv) to isolate project dependencies and prevent conflicts with global packages.

    python3 -m venv myenv
  3. Activate the virtual environment.

    source myenv/bin/activate
  4. Install the required Python dependencies.

    pip3 install opensearch-py

2. Connect to PolarSearch

In your Python code, import the OpenSearch class and create a client instance using your PolarSearch connection details. In a production environment, you can set these details as environment variables. For testing, you can also assign them as default values.

from opensearchpy import OpenSearch

host = os.getenv("HOST", default="<polarsearch_host>")
port = int(os.getenv("PORT", "<polarsearch_port>"))
auth = (os.getenv("USERNAME", "<polarsearch_username>"), os.getenv("PASSWORD", "<polarsearch_password>"))

client = OpenSearch(
    hosts=[{"host": host, "port": port}],
    http_auth=auth,
    use_ssl=False,
    verify_certs=False,
    ssl_show_warn=False,
)

3. Example operations

The following code snippets demonstrate how to perform common index and document operations.

Create an index

Use the client.indices.create() method to create a new index.

index_name = 'python-test-index'
index_body = {
  'settings': {
    'index': {
      'number_of_shards': 4
    }
  }
}

response = client.indices.create(index=index_name, body=index_body)

Index a document

Use the client.index() method to add a document to a specified index.

document = {
  'title': 'Moneyball',
  'director': 'Bennett Miller',
  'year': '2011'
}

response = client.index(
    index = 'python-test-index',
    body = document,
    id = '1',
    refresh = True
)

Search for documents

Use the client.search() method to search for documents based on your query.

q = 'miller'
query = {
  'size': 5,
  'query': {
    'multi_match': {
      'query': q,
      'fields': ['title^2', 'director']
    }
  }
}

response = client.search(
    body = query,
    index = 'python-test-index'
)

Delete a document

Use the client.delete() method to delete a document by its ID.

response = client.delete(
    index = 'python-test-index',
    id = '1'
)

Delete an index

Use the client.indices.delete() method to delete an entire index.

response = client.indices.delete(
    index = 'python-test-index'
)

Complete sample code

This example demonstrates the full lifecycle of an index, from creation to deletion.

OpenSearch Java client

Sample code

PolarSearch 1.x version

Dependency configuration (pom.xml)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>OpenSearchJavaClientSample</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.opensearch.client</groupId>
            <artifactId>opensearch-java</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.opensearch.client</groupId>
            <artifactId>opensearch-rest-client</artifactId>
            <version>1.3.20</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.21.0</version>
        </dependency>
    </dependencies>
</project>

Sample program (OpenSearchClientExample.java)

package samples;

import java.io.IOException;

import javax.net.ssl.SSLContext;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.client.RestClient;
import org.opensearch.client.RestClientBuilder;
import org.opensearch.client.json.jackson.JacksonJsonpMapper;
import org.opensearch.client.opensearch.OpenSearchClient;
import org.opensearch.client.opensearch.core.IndexRequest;
import org.opensearch.client.opensearch.core.SearchResponse;
import org.opensearch.client.opensearch.indices.CreateIndexRequest;
import org.opensearch.client.opensearch.indices.DeleteIndexRequest;
import org.opensearch.client.opensearch.indices.DeleteIndexResponse;
import org.opensearch.client.transport.OpenSearchTransport;
import org.opensearch.client.transport.rest_client.RestClientTransport;


public class OpenSearchClientExample {
    static class IndexData {
        private String title;
        private String text;
    
        public IndexData() {}
    
        public IndexData(String title, String text) {
            this.title = title;
            this.text = text;
        }
    
        public String getTitle() {
            return title;
        }
    
        public void setTitle(String title) {
            this.title = title;
        }
    
        public String getText() {
            return text;
        }
    
        public void setText(String text) {
            this.text = text;
        }
    
        @Override
        public String toString() {
            return String.format("IndexData{title='%s', text='%s'}", title, text);
        }
    }
    
    private static final Logger LOGGER = LogManager.getLogger(OpenSearchClientExample.class);

    public static OpenSearchTransport createTransport() throws Exception {
        var env = System.getenv();
        var hostname = env.getOrDefault("HOST", "<polarsearch_host>");
        var port = Integer.parseInt(env.getOrDefault("PORT", "<polarsearch_port>"));
        var scheme = env.getOrDefault("SCHEME", "http");
        var user = env.getOrDefault("USERNAME", "<polarsearch_username>");
        var pass = env.getOrDefault("PASSWORD", "<polarsearch_password>");

        final HttpHost host = new HttpHost(hostname, port, scheme);
        final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(user, pass));

        final SSLContext sslContext = SSLContextBuilder.create()
                .loadTrustMaterial(null, (chains, authType) -> true)
                .build();

        RestClientBuilder builder = RestClient.builder(host)
                .setHttpClientConfigCallback(httpClientBuilder ->
                        httpClientBuilder
                                .setDefaultCredentialsProvider(credentialsProvider)
                                .setSSLContext(sslContext));

        final RestClient restClient = builder.build();
        return new RestClientTransport(restClient, new JacksonJsonpMapper());
    }

    public static void main(String[] args) {
        OpenSearchTransport transport = null;
        try {
            LOGGER.info("start test...");

            transport = createTransport();
            var client = new OpenSearchClient(transport);
            LOGGER.info("client create");
            final var index = "my-index";
            // 1. Create an index
            LOGGER.info("1. Creating an index {} start", index);
            if (!client.indices().exists(r -> r.index(index)).value()) {
                CreateIndexRequest createIndexRequest = new CreateIndexRequest.Builder().index(index)
                        .build();
                client.indices().create(createIndexRequest);
            }
            LOGGER.info("1. Creating an index {} finished", index);
    
            // 2. Index data
            LOGGER.info("2. Indexing data start");
            IndexData indexData = new IndexData("first_name", "Bruce");
            IndexRequest<IndexData> indexRequest = new IndexRequest.Builder<IndexData>().index(index).id("1").document(indexData).build();
            client.index(indexRequest);
            LOGGER.info("2. Indexing data finished");
    
            Thread.sleep(1500);
    
            // 3. Search for documents
            LOGGER.info("3. Searching for documents start");
            SearchResponse<IndexData> searchResponse = client.search(s -> s.index(index), IndexData.class);
            for (int i = 0; i< searchResponse.hits().hits().size(); i++) {
                LOGGER.info(searchResponse.hits().hits().get(i).source());
            }        
            LOGGER.info("3. Searching for documents finished");
            
            // 4. Delete a document
            LOGGER.info("4. Deleting a document start");
            client.delete(b -> b.index(index).id("1"));
            LOGGER.info("4. Deleting a document finished");
    
            // 5. Delete an index
            LOGGER.info("5. Deleting an index {} start", index);
            DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest.Builder().index(index).build();
            DeleteIndexResponse deleteIndexResponse = client.indices().delete(deleteIndexRequest);
            LOGGER.info("5. Deleting an index {} finished", index);

            LOGGER.info("end test...");
        } catch (Exception e) {
            LOGGER.error(e.toString());
        } finally {
            if (transport != null) {
                try {
                    transport.close();
                } catch (Exception e) {
                    LOGGER.error("Failed to close transport", e);
                }
            }
        }
    }
}

PolarSearch 3.x version

Dependency configuration (pom.xml)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>OpenSearchJavaClientSample</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.opensearch.client</groupId>
            <artifactId>opensearch-java</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents.client5</groupId>
            <artifactId>httpclient5</artifactId>
            <version>5.4.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.21.0</version>
        </dependency>
    </dependencies>
</project>

Sample program (OpenSearchClientExample.java)

package samples;

import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;

import org.apache.hc.client5.http.auth.AuthScope;
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager;
import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder;
import org.apache.hc.client5.http.ssl.ClientTlsStrategyBuilder;
import org.apache.hc.core5.function.Factory;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
import org.apache.hc.core5.reactor.ssl.TlsDetails;
import org.apache.hc.core5.ssl.SSLContextBuilder;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.client.opensearch.OpenSearchClient;
import org.opensearch.client.opensearch.core.IndexRequest;
import org.opensearch.client.opensearch.core.SearchResponse;
import org.opensearch.client.opensearch.indices.CreateIndexRequest;
import org.opensearch.client.opensearch.indices.DeleteIndexRequest;
import org.opensearch.client.opensearch.indices.DeleteIndexResponse;
import org.opensearch.client.transport.OpenSearchTransport;
import org.opensearch.client.transport.httpclient5.ApacheHttpClient5TransportBuilder;


public class OpenSearchClientExample {
    static class IndexData {
        private String title;
        private String text;
    
        public IndexData() {}
    
        public IndexData(String title, String text) {
            this.title = title;
            this.text = text;
        }
    
        public String getTitle() {
            return title;
        }
    
        public void setTitle(String title) {
            this.title = title;
        }
    
        public String getText() {
            return text;
        }
    
        public void setText(String text) {
            this.text = text;
        }
    
        @Override
        public String toString() {
            return String.format("IndexData{title='%s', text='%s'}", title, text);
        }
    }
    
    private static final Logger LOGGER = LogManager.getLogger(OpenSearchClientExample.class);

    public static OpenSearchTransport createTransport() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
        var env = System.getenv();
        var hostname = env.getOrDefault("HOST", "<polarsearch_host>");
        var port = Integer.parseInt(env.getOrDefault("PORT", "<polarsearch_port>"));
        var scheme = env.getOrDefault("SCHEME", "http");
        var user = env.getOrDefault("USERNAME", "<polarsearch_username>");
        var pass = env.getOrDefault("PASSWORD", "<polarsearch_password>");

        final HttpHost host = new HttpHost(scheme, hostname, port);
        final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(new AuthScope(host), new UsernamePasswordCredentials(user, pass.toCharArray()));

        final SSLContext sslContext = SSLContextBuilder
                .create()
                .loadTrustMaterial(null, (chains, authType) -> true)
                .build();

        final ApacheHttpClient5TransportBuilder builder = ApacheHttpClient5TransportBuilder.builder(host);
        builder.setHttpClientConfigCallback(httpClientBuilder -> {
            final TlsStrategy tlsStrategy = ClientTlsStrategyBuilder.create()
                    .setSslContext(sslContext)
                    .setTlsDetailsFactory(new Factory<SSLEngine, TlsDetails>() {
                        @Override
                        public TlsDetails create(final SSLEngine sslEngine) {
                            return new TlsDetails(sslEngine.getSession(), sslEngine.getApplicationProtocol());
                        }
                    })
                    .build();

            final PoolingAsyncClientConnectionManager connectionManager = PoolingAsyncClientConnectionManagerBuilder
                    .create()
                    .setTlsStrategy(tlsStrategy)
                    .build();

            return httpClientBuilder
                    .setDefaultCredentialsProvider(credentialsProvider)
                    .setConnectionManager(connectionManager);
        });

        final OpenSearchTransport transport = builder.build();
        return transport;
    }

    public static void main(String[] args) {
        OpenSearchTransport transport = null;
        try {
            LOGGER.info("start test...");

            transport = createTransport();
            var client = new OpenSearchClient(transport);
            LOGGER.info("client create");
            final var index = "my-index";
            // 1. Create an index
            LOGGER.info("1. Creating an index {} start", index);
            if (!client.indices().exists(r -> r.index(index)).value()) {
                CreateIndexRequest createIndexRequest = new CreateIndexRequest.Builder().index(index)
                        .build();
                client.indices().create(createIndexRequest);
            }
            LOGGER.info("1. Creating an index {} finished", index);
    
            // 2. Index data
            LOGGER.info("2. Indexing data start");
            IndexData indexData = new IndexData("first_name", "Bruce");
            IndexRequest<IndexData> indexRequest = new IndexRequest.Builder<IndexData>().index(index).id("1").document(indexData).build();
            client.index(indexRequest);
            LOGGER.info("2. Indexing data finished");
    
            Thread.sleep(1500);
    
            // 3. Search for documents
            LOGGER.info("3. Searching for documents start");
            SearchResponse<IndexData> searchResponse = client.search(s -> s.index(index), IndexData.class);
            for (int i = 0; i< searchResponse.hits().hits().size(); i++) {
                LOGGER.info(searchResponse.hits().hits().get(i).source());
            }        
            LOGGER.info("3. Searching for documents finished");
            
            // 4. Delete a document
            LOGGER.info("4. Deleting a document start");
            client.delete(b -> b.index(index).id("1"));
            LOGGER.info("4. Deleting a document finished");
    
            // 5. Delete an index
            LOGGER.info("5. Deleting an index {} start", index);
            DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest.Builder().index(index).build();
            DeleteIndexResponse deleteIndexResponse = client.indices().delete(deleteIndexRequest);
            LOGGER.info("5. Deleting an index {} finished", index);

            LOGGER.info("end test...");
        } catch (Exception e) {
            LOGGER.error(e.toString());
        } finally {
            if (transport != null) {
                try {
                    transport.close();
                } catch (Exception e) {
                    LOGGER.error("Failed to close transport", e);
                }
            }
        }
    }
}

How to run

mvn clean compile exec:java -Dexec.mainClass=samples.OpenSearchClientExample

OpenSearch Java high-level REST client

Sample code

PolarSearch 1.x version

Dependency configuration (pom.xml)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>OpenSearchJavaClientSample</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.opensearch.client</groupId>
            <artifactId>opensearch-rest-high-level-client</artifactId>
            <version>1.3.20</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.21.0</version>
        </dependency>
    </dependencies>
</project>

Sample program (OpenSearchClientExample.java)

package samples;

import java.util.HashMap;
import java.util.Map;

import javax.net.ssl.SSLContext;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.action.delete.DeleteRequest;
import org.opensearch.action.index.IndexRequest;
import org.opensearch.action.admin.indices.delete.DeleteIndexRequest;
import org.opensearch.action.search.SearchRequest;
import org.opensearch.action.search.SearchResponse;
import org.opensearch.action.support.master.AcknowledgedResponse;
import org.opensearch.client.RequestOptions;
import org.opensearch.client.RestClient;
import org.opensearch.client.RestClientBuilder;
import org.opensearch.client.RestHighLevelClient;
import org.opensearch.client.indices.CreateIndexRequest;
import org.opensearch.client.indices.GetIndexRequest;
import org.opensearch.index.query.QueryBuilders;
import org.opensearch.search.SearchHit;
import org.opensearch.search.builder.SearchSourceBuilder;


public class OpenSearchClientExample {

    static class IndexData {
        private String title;
        private String text;

        public IndexData() {}

        public IndexData(String title, String text) {
            this.title = title;
            this.text = text;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

        @Override
        public String toString() {
            return String.format("IndexData{title='%s', text='%s'}", title, text);
        }
    }

    private static final Logger LOGGER = LogManager.getLogger(OpenSearchClientExample.class);

    public static RestHighLevelClient createClient() throws Exception {
        var env = System.getenv();
        var hostname = env.getOrDefault("HOST", "<polarsearch_host>");
        var port = Integer.parseInt(env.getOrDefault("PORT", "<polarsearch_port>"));
        var scheme = env.getOrDefault("SCHEME", "http");
        var user = env.getOrDefault("USERNAME", "<polarsearch_username>");
        var pass = env.getOrDefault("PASSWORD", "<polarsearch_password>");

        final HttpHost host = new HttpHost(hostname, port, scheme);
        final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(user, pass));

        final SSLContext sslContext = SSLContextBuilder.create()
                .loadTrustMaterial(null, (chains, authType) -> true)
                .build();

        RestClientBuilder builder = RestClient.builder(host)
                .setHttpClientConfigCallback(httpClientBuilder ->
                        httpClientBuilder
                                .setDefaultCredentialsProvider(credentialsProvider)
                                .setSSLContext(sslContext));

        return new RestHighLevelClient(builder);
    }

    public static void main(String[] args) {
        try (RestHighLevelClient client = createClient()) {
            LOGGER.info("start test...");
            LOGGER.info("client create");
            final String index = "my-index";

            // 1. Create an index
            LOGGER.info("1. Creating an index {} start", index);
            boolean exists = client.indices().exists(new GetIndexRequest(index), RequestOptions.DEFAULT);
            if (!exists) {
                CreateIndexRequest createIndexRequest = new CreateIndexRequest(index);
                client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
            }
            LOGGER.info("1. Creating an index {} finished", index);

            // 2. Index data
            LOGGER.info("2. Indexing data start");
            IndexData indexData = new IndexData("first_name", "Bruce");
            Map<String, Object> document = new HashMap<>();
            document.put("title", indexData.getTitle());
            document.put("text", indexData.getText());
            IndexRequest indexRequest = new IndexRequest(index).id("1").source(document);
            client.index(indexRequest, RequestOptions.DEFAULT);
            LOGGER.info("2. Indexing data finished");

            Thread.sleep(1500);

            // 3. Search for documents
            LOGGER.info("3. Searching for documents start");
            SearchRequest searchRequest = new SearchRequest(index);
            searchRequest.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));
            SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            for (SearchHit hit : searchResponse.getHits().getHits()) {
                Map<String, Object> sourceMap = hit.getSourceAsMap();
                IndexData data = new IndexData((String) sourceMap.get("title"), (String) sourceMap.get("text"));
                LOGGER.info(data);
            }
            LOGGER.info("3. Searching for documents finished");

            // 4. Delete a document
            LOGGER.info("4. Deleting a document start");
            client.delete(new DeleteRequest(index, "1"), RequestOptions.DEFAULT);
            LOGGER.info("4. Deleting a document finished");

            // 5. Delete an index
            LOGGER.info("5. Deleting an index {} start", index);
            DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(index);
            AcknowledgedResponse deleteIndexResponse = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
            LOGGER.info("5. Deleting an index {} finished", index);

            LOGGER.info("end test...");
        } catch (Exception e) {
            LOGGER.error(e.toString(), e);
        }
    }
}

PolarSearch 3.x version

Dependency configuration (pom.xml)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>OpenSearchJavaClientSample</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.opensearch.client</groupId>
            <artifactId>opensearch-rest-high-level-client</artifactId>
            <version>3.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.21.0</version>
        </dependency>
    </dependencies>
</project>

Sample program (OpenSearchClientExample.java)

package samples;

import java.util.HashMap;
import java.util.Map;

import javax.net.ssl.SSLContext;

import org.apache.hc.client5.http.auth.AuthScope;
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager;
import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder;
import org.apache.hc.client5.http.ssl.ClientTlsStrategyBuilder;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
import org.apache.hc.core5.ssl.SSLContextBuilder;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.action.delete.DeleteRequest;
import org.opensearch.action.index.IndexRequest;
import org.opensearch.action.search.SearchRequest;
import org.opensearch.action.search.SearchResponse;
import org.opensearch.action.support.clustermanager.AcknowledgedResponse;
import org.opensearch.client.RequestOptions;
import org.opensearch.client.RestClient;
import org.opensearch.client.RestClientBuilder;
import org.opensearch.client.RestHighLevelClient;
import org.opensearch.client.indices.CreateIndexRequest;
import org.opensearch.client.indices.GetIndexRequest;
import org.opensearch.index.query.QueryBuilders;
import org.opensearch.search.SearchHit;
import org.opensearch.search.builder.SearchSourceBuilder;
import org.opensearch.action.admin.indices.delete.DeleteIndexRequest;


public class OpenSearchClientExample {

    static class IndexData {
        private String title;
        private String text;

        public IndexData() {}

        public IndexData(String title, String text) {
            this.title = title;
            this.text = text;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

        @Override
        public String toString() {
            return String.format("IndexData{title='%s', text='%s'}", title, text);
        }
    }

    private static final Logger LOGGER = LogManager.getLogger(OpenSearchClientExample.class);

    public static RestHighLevelClient createClient() throws Exception {
        var env = System.getenv();
        var hostname = env.getOrDefault("HOST", "<polarsearch_host>");
        var port = Integer.parseInt(env.getOrDefault("PORT", "<polarsearch_port>"));
        var scheme = env.getOrDefault("SCHEME", "http");
        var user = env.getOrDefault("USERNAME", "<polarsearch_username>");
        var pass = env.getOrDefault("PASSWORD", "<polarsearch_password>");

        final HttpHost host = new HttpHost(scheme, hostname, port);
        final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(new AuthScope(host),
                new UsernamePasswordCredentials(user, pass.toCharArray()));

        final SSLContext sslContext = SSLContextBuilder.create()
                .loadTrustMaterial(null, (chains, authType) -> true)
                .build();

        final TlsStrategy tlsStrategy = ClientTlsStrategyBuilder.create()
                .setSslContext(sslContext)
                .build();

        final PoolingAsyncClientConnectionManager connectionManager = PoolingAsyncClientConnectionManagerBuilder
                .create()
                .setTlsStrategy(tlsStrategy)
                .build();

        RestClientBuilder builder = RestClient.builder(host)
                .setHttpClientConfigCallback(httpClientBuilder ->
                        httpClientBuilder
                                .setDefaultCredentialsProvider(credentialsProvider)
                                .setConnectionManager(connectionManager));

        return new RestHighLevelClient(builder);
    }

    public static void main(String[] args) {
        try (RestHighLevelClient client = createClient()) {
            LOGGER.info("start test...");
            LOGGER.info("client create");
            final String index = "my-index";

            // 1. Create an index
            LOGGER.info("1. Creating an index {} start", index);
            boolean exists = client.indices().exists(new GetIndexRequest(index), RequestOptions.DEFAULT);
            if (!exists) {
                CreateIndexRequest createIndexRequest = new CreateIndexRequest(index);
                client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
            }
            LOGGER.info("1. Creating an index {} finished", index);

            // 2. Index data
            LOGGER.info("2. Indexing data start");
            IndexData indexData = new IndexData("first_name", "Bruce");
            Map<String, Object> document = new HashMap<>();
            document.put("title", indexData.getTitle());
            document.put("text", indexData.getText());
            IndexRequest indexRequest = new IndexRequest(index).id("1").source(document);
            client.index(indexRequest, RequestOptions.DEFAULT);
            LOGGER.info("2. Indexing data finished");

            Thread.sleep(1500);

            // 3. Search for documents
            LOGGER.info("3. Searching for documents start");
            SearchRequest searchRequest = new SearchRequest(index);
            searchRequest.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));
            SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            for (SearchHit hit : searchResponse.getHits().getHits()) {
                Map<String, Object> sourceMap = hit.getSourceAsMap();
                IndexData data = new IndexData((String) sourceMap.get("title"), (String) sourceMap.get("text"));
                LOGGER.info(data);
            }
            LOGGER.info("3. Searching for documents finished");

            // 4. Delete a document
            LOGGER.info("4. Deleting a document start");
            client.delete(new DeleteRequest(index, "1"), RequestOptions.DEFAULT);
            LOGGER.info("4. Deleting a document finished");

            // 5. Delete an index
            LOGGER.info("5. Deleting an index {} start", index);
            DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(index);
            AcknowledgedResponse deleteIndexResponse = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
            LOGGER.info("5. Deleting an index {} finished", index);

            LOGGER.info("end test...");
        } catch (Exception e) {
            LOGGER.error(e.toString(), e);
        }
    }
}

How to run

mvn clean compile exec:java -Dexec.mainClass=samples.OpenSearchClientExample

Elasticsearch Java high-level REST client

Sample code

Dependency configuration (pom.xml)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>OpenSearchJavaSample</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.13.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.20.0</version>
        </dependency>
    </dependencies>
</project>

Sample program (ElasticSearchClientExample.java)

package samples;

import java.util.HashMap;
import java.util.Map;

import javax.net.ssl.SSLContext;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;


public class ElasticSearchClientExample {

    static class IndexData {
        private String title;
        private String text;

        public IndexData() {}

        public IndexData(String title, String text) {
            this.title = title;
            this.text = text;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

        @Override
        public String toString() {
            return String.format("IndexData{title='%s', text='%s'}", title, text);
        }
    }

    private static final Logger LOGGER = LogManager.getLogger(ElasticSearchClientExample.class);

    public static RestHighLevelClient createClient() throws Exception {
        var env = System.getenv();
        var hostname = env.getOrDefault("HOST", "<polarsearch_host>");
        var port = Integer.parseInt(env.getOrDefault("PORT", "<polarsearch_port>"));
        var scheme = env.getOrDefault("SCHEME", "http");
        var user = env.getOrDefault("USERNAME", "<polarsearch_username>");
        var pass = env.getOrDefault("PASSWORD", "<polarsearch_password>");

        final HttpHost host = new HttpHost(hostname, port, scheme);
        final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(user, pass));

        final SSLContext sslContext = SSLContextBuilder.create()
                .loadTrustMaterial(null, (chains, authType) -> true)
                .build();

        RestClientBuilder builder = RestClient.builder(host)
                .setHttpClientConfigCallback(httpClientBuilder ->
                        httpClientBuilder
                                .setDefaultCredentialsProvider(credentialsProvider)
                                .setSSLContext(sslContext));

        return new RestHighLevelClient(builder);
    }

    public static void main(String[] args) {
        try (RestHighLevelClient client = createClient()) {
            LOGGER.info("start test...");
            LOGGER.info("client create");
            final String index = "my-index";

            // 0. Ping cluster
            LOGGER.info("0. Ping cluster start");
            Response response = client.getLowLevelClient().performRequest(new Request("HEAD", "/"));
            int statusCode = response.getStatusLine().getStatusCode();
            LOGGER.info("Ping status code: {}", statusCode);
            if (statusCode == 200) {
                Response infoResponse = client.getLowLevelClient().performRequest(new Request("GET", "/"));
                String responseBody = EntityUtils.toString(infoResponse.getEntity());
                LOGGER.info("Cluster info: {}", responseBody);
            }
            LOGGER.info("0. Ping cluster finished");

            // 1. Create an index
            LOGGER.info("1. Creating an index {} start", index);
            boolean exists = client.indices().exists(new GetIndexRequest(index), RequestOptions.DEFAULT);
            if (!exists) {
                CreateIndexRequest createIndexRequest = new CreateIndexRequest(index);
                client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
            }
            LOGGER.info("1. Creating an index {} finished", index);

            // 2. Index data
            LOGGER.info("2. Indexing data start");
            IndexData indexData1 = new IndexData("first_name", "Bruce");
            Map<String, Object> document1 = new HashMap<>();
            document1.put("title", indexData1.getTitle());
            document1.put("text", indexData1.getText());
            client.index(new IndexRequest(index).id("1").source(document1), RequestOptions.DEFAULT);

            IndexData indexData2 = new IndexData("last_name", "Wayne");
            Map<String, Object> document2 = new HashMap<>();
            document2.put("title", indexData2.getTitle());
            document2.put("text", indexData2.getText());
            client.index(new IndexRequest(index).id("2").source(document2), RequestOptions.DEFAULT);
            LOGGER.info("2. Indexing data finished");

            Thread.sleep(1500);

            // 3. Get a document by ID
            LOGGER.info("3. Getting a document by ID start");
            GetResponse getResponse = client.get(new GetRequest(index, "1"), RequestOptions.DEFAULT);
            if (getResponse.isExists()) {
                Map<String, Object> sourceMap = getResponse.getSourceAsMap();
                IndexData data = new IndexData((String) sourceMap.get("title"), (String) sourceMap.get("text"));
                LOGGER.info(data);
            }
            LOGGER.info("3. Getting a document by ID finished");

            // 4. Search for documents
            LOGGER.info("4. Searching for documents start");
            SearchRequest searchRequest = new SearchRequest(index);
            searchRequest.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));
            SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            for (SearchHit hit : searchResponse.getHits().getHits()) {
                Map<String, Object> sourceMap = hit.getSourceAsMap();
                IndexData data = new IndexData((String) sourceMap.get("title"), (String) sourceMap.get("text"));
                LOGGER.info(data);
            }
            LOGGER.info("4. Searching for documents finished");

            // 5. Delete a document
            LOGGER.info("5. Deleting a document start");
            client.delete(new DeleteRequest(index, "1"), RequestOptions.DEFAULT);
            LOGGER.info("5. Deleting a document finished");

            // 6. Delete an index
            LOGGER.info("6. Deleting an index {} start", index);
            DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(index);
            AcknowledgedResponse deleteIndexResponse = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
            LOGGER.info("6. Deleting an index {} finished", index);

            LOGGER.info("end test...");
        } catch (Exception e) {
            LOGGER.error(e.toString(), e);
        }
    }
}

Low-level Python client

Dependency configuration

pip3 install opensearch-py

Sample code

import os
from opensearchpy import OpenSearch

host = os.getenv("HOST", default="<polarsearch_host>")
port = int(os.getenv("PORT", '<polarsearch_port>'))
auth = (os.getenv("USERNAME", "<polarsearch_username>"), os.getenv("PASSWORD", "<polarsearch_password>"))


client = OpenSearch(
    hosts=[{"host": host, "port": port}],
    http_auth=auth,
    use_ssl=False,
    verify_certs=False,
    ssl_show_warn=False,
)

# Create an index with non-default settings.
index_name = 'python-test-index'
index_body = {
  'settings': {
    'index': {
      'number_of_shards': 4
    }
  }
}

response = client.indices.create(index=index_name, body=index_body)
print('\nCreating index:')
print(response)

# Add a document to the index.
document = {
  'title': 'Moneyball',
  'director': 'Bennett Miller',
  'year': '2011'
}
id = '1'

response = client.index(
    index = index_name,
    body = document,
    id = id,
    refresh = True
)

print('\nAdding document:')
print(response)

# Perform bulk operations

movies = '{ "index" : { "_index" : "my-dsl-index", "_id" : "2" } } \n { "title" : "Interstellar", "director" : "Christopher Nolan", "year" : "2014"} \n { "create" : { "_index" : "my-dsl-index", "_id" : "3" } } \n { "title" : "Star Trek Beyond", "director" : "Justin Lin", "year" : "2015"} \n { "update" : {"_id" : "3", "_index" : "my-dsl-index" } } \n { "doc" : {"year" : "2016"} }'

client.bulk(body=movies)

# Search for the document.
q = 'miller'
query = {
  'size': 5,
  'query': {
    'multi_match': {
      'query': q,
      'fields': ['title^2', 'director']
    }
  }
}

response = client.search(
    body = query,
    index = index_name
)
print('\nSearch results:')
print(response)

# Delete the document.
response = client.delete(
    index = index_name,
    id = id
)

print('\nDeleting document:')
print(response)

# Delete the index.
response = client.indices.delete(
    index = index_name
)

print('\nDeleting index:')
print(response)

Related documents