This topic describes how to use the PHP, Python, and Java clients to connect to Alibaba Cloud Elasticsearch. Sample code and usage guidelines for these clients are provided.

PHP client

Warning
  • The default connection pool provided by a PHP client is not suitable for connecting to Elasticsearch clusters on the cloud. Alibaba Cloud Elasticsearch handles requests sent from clients based on load balancing. Therefore, a PHP client must use SimpleConnectionPool as the connection pool. Otherwise, when Alibaba Cloud Elasticsearch is restarted, a connection error occurs.
  • A PHP client is configured to reconnect to Elasticisearch after it is disconnected. This is because connection errors may occur, for example, the "No enabled connection" error message may be reported during a restart of the Elasticsearch cluster even if the connection pool is created by using SimpleConnectionPool.
To test the connectivity to the Elasticsearch cluster, connect a PHP client to your cluster through port 9200. The sample code is as follows. For more information, see elasticsearch-php.
<? php
require 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;

$client = ClientBuilder::create()->setHosts([
  [
    'host'   => '<HOST>',
    'port'   => '9200',
    'scheme' => 'http',
    'user'   => '<USER NAME>',
    'pass'   => '<PASSWORD>'
  ]
])->setConnectionPool('\Elasticsearch\ConnectionPool\SimpleConnectionPool', [])
  ->setRetries(10)->build();

$indexParams = [
  'index'  => 'my_index',
  'type'   => 'my_type',
  'id'     => '1',
  'body'   => ['testField' => 'abc'],
  'client' => [
    'timeout'         => 10,
    'connect_timeout' => 10
  ]
];
$indexResponse = $client->index($indexParams);
print_r($indexResponse);

$searchParams = [
  'index'  => 'my_index',
  'type'   => 'my_type',
  'body'   => [
    'query' => [
      'match' => [
        'testField' => 'abc'
      ]
    ]
  ],
  'client' => [
    'timeout'         => 10,
    'connect_timeout' => 10
  ]
];
$searchResponse = $client->search($searchParams);
print_r($searchResponse);
? >
Table 1. Variables
Variable Description
<USER NAME> Replace it with the username of the Elasticsearch cluster.
<PASSWORD> Replace it with the password of the Elasticsearch cluster.
<HOST> Replace it with the public or internal endpoint that you can find on the Basic Information page of the Elasticsearch cluster.

Python client

To test the connectivity to the Elasticsearch cluster, connect a Python client to your cluster through port 9200. The sample code is as follows. For more information, see elasticsearch-py.

from elasticsearch import Elasticsearch, RequestsHttpConnection
import certifi
es = Elasticsearch(
    ['<HOST>'],
    http_auth=('username', 'password'),
    port=9200,
    use_ssl=False
)
res = es.index(index="my_index", doc_type="my_type", id=1, body={"title": "One", "tags": ["ruby"]})
print(res['created'])
res = es.get(index="my_index", doc_type="my_type", id=1)
print(res['_source'])

Java client

Java clients include Transport Client, Low Level REST Client, and High Level REST Client. For more information about the sample code, see Java API.
Notice Java Transport Client communicates with Elaticsearch over TCP. If the client communicates with different versions of Elasticsearch, incompatibilities may occur. In the later versions of open-source Elasticsearch, Transport Client is deprecated. If the Elasticsearch cluster runs V5.5 or V5.6, the "NoNodeAvailableException" error message is displayed when you use Transport Client 5.5 to access Elasticsearch V5.5 and use Transport Client 5.6 to access Elasticsearch V5.6. We recommend that you use Transport Client 5.3.3 or Java Low Level REST Client to access the Elasticsearch cluster to ensure compatibility. If you use Transport Client, set client.transport.sniff to false.

Go client

If you use a Go client to connect to the Elasticsearch cluster through port 9200, refer to the following sample code.

Before you use a Go client to connect to Elasticsearch, complete the following tasks:
package main

import (
    "context"
    "gopkg.in/olivere/elastic.v5"
    "gopkg.in/olivere/elastic.v5/config"
    "log"
)

const (
    url      = "http://es-cn-xxxxxx.public.elasticsearch.aliyuncs.com:9200"     //<1>
    username = "elastic"        //<2>
    password = "password"       //<3>
)

func main() {
    var sniff = false    //<4>  
    cfg := &config.Config{
        URL:      url,
        Username: username,
        Password: password,
    }

    cfg.Sniff = &sniff          
    var client, err = elastic.NewClientFromConfig(cfg)
    if err ! = nil {
        log.Println(err)
        return
    }

    exists, err := client.IndexExists("index_test").Do(context.Background())   //<5>
    if err ! = nil {
        log.Println(err)
    }
    log.Println(exists)
}
Table 2. Parameters
Parameter Description
<1>: url Replace it with the endpoint of the Elasticsearch cluster. You can find the endpoint information on the Basic Information page of the Elasticsearch cluster.
<2>: username Replace it with the username of the Elasticsearch cluster. The default username is elastic.
<3>: password Replace it with the password of the Elasticsearch cluster. The password is specified when you create the cluster.
<4>: sniff Set the value to false to prevent sniffing on connections. If this feature is enabled, connection failures occur.
<5>: exists, err This parameter creates index_test on the Elasticsearch cluster, and returns log data that contains the connection information. Replace the index name with the actual name of your index.

If the client is connected to the Elasticsearch cluster, the following result is returned:

2019/11/29 10:07:37 true

For more information about the clients in other languages, see HTTP/REST Clients and Security.