上云无忧 > 文档中心 > 如何通过其他客户端访问百度智能云Elasticsearch?
Elasticsearch
如何通过其他客户端访问百度智能云Elasticsearch?

文档简介:
本文介绍使用PHP、Python和Go客户端访问Baidu Elasticsearch的方法,并提供了示例代码和注意事项供您参考。 PHP语言: 警告: Elasticsearch的PHP客户端提供的默认连接池并不适合云上环境,可能在长连接断开时出现连接异常的问题。推荐PHP客户端访问程序使用SimpleConnectionPool作为连接池,并做好失败重连的机制。
*此产品及展示信息均由百度智能云官方提供。免费试用 咨询热线:400-826-7010,为您提供专业的售前咨询,让您快速了解云产品,助您轻松上云! 微信咨询
  免费试用、价格特惠

本文介绍使用PHP、Python和Go客户端访问Baidu Elasticsearch的方法,并提供了示例代码和注意事项供您参考。

PHP语言

警告

Elasticsearch的PHP客户端提供的默认连接池并不适合云上环境,可能在长连接断开时出现连接异常的问题。推荐PHP客户端访问程序使用SimpleConnectionPool作为连接池,并做好失败重连的机制。

通过PHP客户端访问Elasticsearch进行测试,示例代码如下,详情请参见elasticsearch-php。

<?php
require 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;

$client = ClientBuilder::create()->setHosts([
  [
    'host'   => '<HOST>',
    'port'   => '8200',
    '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);
?>
参数 说明
HOST ES HTTP URL,在Baidu Elasticsearch界面中可以查询
USER_NAME 用户名。
PASSWORD 密码。

Python语言

通过Python客户端访问Elasticsearch进行测试,示例代码如下,详情请参见elasticsearch-py。

from elasticsearch import Elasticsearch, RequestsHttpConnection
import certifi
es = Elasticsearch(
    ['<HOST>'],
    http_auth=('<USER_NAME>', '<PASSWORD>'),
    port=8200,
    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'])

Go语言

通过Go语言访问Elasticsearch进行测试,示例代码如下:

  • 安装Go编译环境,详情请参见官方文档。以下示例代码使用Go1.13.4版本。
  • 创建Baidu Elasticsearch实例。
package main

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

const (
    url      = "http://<HOST>:8200"     //<1>
    username = "<USER_NAME>"        //<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)
}
参数 说明
HOST ES HTTP URL,在Baidu Elasticsearch界面中可以查询
USER_NAME 用户名。
PASSWORD 密码。

连接成功后,返回如下结果。

2019/11/29 10:07:37 true

更多语言

更多语言示例请参见HTTP/REST Clients and Security。

相似文档
官方微信
联系客服
400-826-7010
7x24小时客服热线
分享
  • QQ好友
  • QQ空间
  • 微信
  • 微博
返回顶部