Use a PHP, Python, Java, or Go client to connect to your Alibaba Cloud Elasticsearch cluster and perform index and search operations.
Prerequisites
Before you begin, ensure that you have:
An Alibaba Cloud Elasticsearch cluster. See Create an Alibaba Cloud Elasticsearch cluster
An Elasticsearch client installed for your programming language. Install a client version that matches your cluster version to avoid compatibility issues. See Compatibility
Auto Indexing enabled for the cluster. Configure this in the YML file
Network connectivity configured between your server and the cluster (see Network access below)
Network access
Choose your access method based on where your server is located.
Access via internal endpoint — If your server is in the same virtual private cloud (VPC) as the cluster, connect using the internal endpoint. The private IP address whitelist includes 0.0.0.0/0 by default, so no additional configuration is required.
Access via public endpoint — If your server is on the public network, enable the Public Network Access feature and add your server's public IP address to the public IP address whitelist. See Configure a public or private IP address whitelist for an Elasticsearch cluster.
Keep the following in mind when using public network access:
If you connect through Wi-Fi or a broadband network, add the IP address of the jump server that controls outbound traffic to the whitelist.
Adding
0.0.0.0/0to the whitelist allows all IPv4 addresses to access the cluster, which poses security risks. Evaluate the risks before making this change.If no whitelist is configured or the whitelist is incorrectly configured, the cluster returns a
Timeout connectingerror.To access the Kibana node from a client, configure a separate IP address whitelist for Kibana. See Configure a public or private IP address whitelist for Kibana.
Sample code
All examples connect to the cluster using basic authentication with username and password. Replace the placeholders in each example with your actual values.
| Placeholder | Description |
|---|---|
<YourEsHost> | The public or internal endpoint of the cluster. Find it on the Basic Information page of your cluster in the Elasticsearch console. |
<UserName> | The username for the cluster. The default username is elastic. |
<YourPassword> | The password for the cluster. If you forget the password, reset it on the Security page. See Reset the access password for an Elasticsearch cluster. |
<YourEsIndex> | The name of the index. |
<YourEsType> | The document type. For clusters earlier than V7.0, specify a type based on your requirements. For V7.0 and later, the type is fixed as _doc. |
<YourEsId> | The document ID. |
<YourEsField> / <YourEsFieldValue> | The field name and its value. |
Go
This example uses Go 1.19.1. Before running it, install a Go compilation environment. See The Go Programming Language.
package main
import (
"log"
"github.com/elastic/go-elasticsearch/v7"
)
func main() {
cfg := elasticsearch.Config{
Addresses: []string{
"<YourEsHost>", // Public or internal endpoint, port 9200
},
Username: "<UserName>",
Password: "<YourPassword>",
}
es, err := elasticsearch.NewClient(cfg)
if err != nil {
log.Fatalf("Error creating the client: %s", err)
}
res, err := es.Info()
if err != nil {
log.Fatalf("Error getting response: %s", err)
}
defer res.Body.Close()
log.Println(res)
}For the full list of Go client APIs, see Elasticsearch Go Client.
Java
This example uses High Level REST Client 6.7. Java clients also include Transport Client, Low Level REST Client, and Java API Client. For sample code covering all client types, see Overview.
Transport Client communicates with the cluster over TCP and is deprecated in later Elasticsearch versions. If you use Transport Client 5.5 to access a V5.5 cluster, or Transport Client 5.6 to access a V5.6 cluster, a NoNodeAvailableException error occurs. Use Transport Client 5.3.3 or Java Low Level REST Client instead to avoid version compatibility issues.
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class RestClientTest67 {
private static final RequestOptions COMMON_OPTIONS;
static {
RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
// Reduce the default response buffer from 100 MiB to 30 MiB.
builder.setHttpAsyncResponseConsumerFactory(
new HttpAsyncResponseConsumerFactory
.HeapBufferedResponseConsumerFactory(30 * 1024 * 1024));
COMMON_OPTIONS = builder.build();
}
public static void main(String[] args) {
// Configure basic authentication. Use the same credentials as your Kibana console login.
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("<UserName>", "<YourPassword>"));
// Build the REST client. Find the endpoint on the Basic Information page of your cluster.
RestClientBuilder builder = RestClient.builder(new HttpHost("<YourEsHost>", 9200, "http"))
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
});
// Create a RestHighLevelClient using the low-level client builder.
RestHighLevelClient highClient = new RestHighLevelClient(builder);
try {
// Index a document.
Map<String, Object> jsonMap = new HashMap<>();
jsonMap.put("<YourEsField1>", "<YourEsFieldValue1>");
jsonMap.put("<YourEsField2>", "<YourEsFieldValue2>");
IndexRequest indexRequest = new IndexRequest("<YourEsIndex>", "<YourEsType>", "<YourEsId>")
.source(jsonMap);
// Execute synchronously using the custom RequestOptions.
IndexResponse indexResponse = highClient.index(indexRequest, COMMON_OPTIONS);
long version = indexResponse.getVersion();
System.out.println("Index document successfully! " + version);
highClient.close();
} catch (IOException ioException) {
// Handle exceptions.
}
}
}For the full list of Java client APIs, see Elasticsearch Java API Client.
PHP
The default connection pool in the open source Elasticsearch PHP client is not suitable for cloud-hosted services. Alibaba Cloud Elasticsearch distributes requests using load balancing, so the PHP client must use SimpleConnectionPool as the connection pool. If SimpleConnectionPool is not used, a connection error occurs when the cluster restarts. Additionally, configure the client to reconnect automatically after disconnection — even with SimpleConnectionPool, a No enabled connection error may occur during a cluster restart.
<?php
require 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$client = ClientBuilder::create()->setHosts([
[
'host' => '<YourEsHost>',
'port' => '9200',
'scheme' => 'http',
'user' => '<UserName>',
'pass' => '<YourPassword>'
]
])
// Use SimpleConnectionPool to support load balancing in Alibaba Cloud Elasticsearch.
->setConnectionPool('\Elasticsearch\ConnectionPool\SimpleConnectionPool', [])
// Enable auto-retry to handle connection errors during cluster restarts.
->setRetries(10)
->build();
// Index a document.
$indexParams = [
'index' => '<YourEsIndex>',
'type' => '<YourEsType>',
'id' => '<YourEsId>',
'body' => ['<YourEsField>' => '<YourEsFieldValue>'],
'client' => [
'timeout' => 10,
'connect_timeout' => 10
]
];
$indexResponse = $client->index($indexParams);
print_r($indexResponse);
// Search for documents.
$searchParams = [
'index' => '<YourEsIndex>',
'type' => '<YourEsType>',
'body' => [
'query' => [
'match' => [
'<YourEsField>' => '<YourEsFieldValue>'
]
]
],
'client' => [
'timeout' => 10,
'connect_timeout' => 10
]
];
$searchResponse = $client->search($searchParams);
print_r($searchResponse);
?>For the full list of PHP client APIs, see Elasticsearch PHP Client.
Python
The following examples show how to connect to a cluster with HTTP or HTTPS enabled.
HTTP connection:
from elasticsearch import Elasticsearch, RequestsHttpConnection
import certifi
es = Elasticsearch(
['<YourEsHost>'],
http_auth=('<UserName>', '<YourPassword>'),
port=9200,
use_ssl=False # Set to True for HTTPS-enabled clusters
)
# Index a document.
res = es.index(
index="<YourEsIndex>",
doc_type="<YourEsType>",
id=<YourEsId>,
body={"<YourEsField1>": "<YourEsFieldValue1>", "<YourEsField2>": "<YourEsFieldValue2>"}
)
# Retrieve a document.
res = es.get(index="<YourEsIndex>", doc_type="<YourEsType>", id=<YourEsId>)
print(res['_source'])HTTPS connection:
es = Elasticsearch(
['<YourEsHost>'],
http_auth=('<UserName>', '<YourPassword>'),
port=9200,
use_ssl=True, # Enable SSL/TLS
verify_certs=True # Verify the server certificate
)For the full list of Python client APIs, see Elasticsearch Python Client.
Next steps
For other supported Elasticsearch client libraries, see Elasticsearch Clients.