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
A cluster with a PolarSearch node has been created, with an administrator account for the node configured.
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
Navigate to your project directory. This example uses
/home/PolarSearchTestPython.mkdir /home/PolarSearchTestPython cd /home/PolarSearchTestPythonIn the
/home/PolarSearchTestPythondirectory, create a virtual environment (venv) to isolate project dependencies and prevent conflicts with global packages.python3 -m venv myenvActivate the virtual environment.
source myenv/bin/activateInstall 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
PolarSearch 3.x version
How to run
mvn clean compile exec:java -Dexec.mainClass=samples.OpenSearchClientExampleOpenSearch Java high-level REST client
Sample code
PolarSearch 1.x version
PolarSearch 3.x version
How to run
mvn clean compile exec:java -Dexec.mainClass=samples.OpenSearchClientExampleElasticsearch Java high-level REST client
Sample code
Low-level Python client
Dependency configuration
pip3 install opensearch-pySample 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)