php7.4 + ElasticSearch-8.7.0
composer:
"require": {
"elasticsearch/elasticsearch": "^8.7",
"ramsey/uuid": "^4.2"
}
<?php
include './vendor/autoload.php';
use Ramsey\Uuid\Uuid;
use Elastic\Elasticsearch\ClientBuilder;
$client = ClientBuilder::create()
->setHosts(['https://localhost:9200'])
->setBasicAuthentication('elastic', '5CC3OMHRI1jniz72+QjZ')
->setCABundle('./http_ca.crt')
->build();
// Info API
//$response = $client->info();
//echo $response['version']['number']; // 8.0.0
// 创建索引
$params = [
'index' => 'test_index',
'body' => [
'mappings' => [
'properties' => [
'title' => ['type' => 'text'],
'content' => ['type' => 'text'],
'create_time' => ['type' => 'long'],
'update_time' => ['type' => 'long']
]
]
]
];
$client->indices()->create($params);
// 添加文档
$params = [
'index' => 'test_index',
'id' => $uuid = Uuid::uuid4()->toString(),
'body' => [
'title'=>'使用PHP来创建一个RPC服务',
'content'=>'RPC全称为Remote Procedure Call,翻译过来为"远程过程调用"。主要应用于不同的系统之间的远程通信和相互调用。
比如有两个系统,一个是PHP写的,一个是JAVA写的,而PHP想要调用JAVA中的某个类的某个方法,这时候就需要用到RPC了。
怎么调?直接调是不可能,只能是PHP通过某种自定义协议请求JAVA的服务,JAVA解析该协议,在本地实例化类并调用方法,然后把结果返回给PHP。
这里我们用PHP的socket扩展来创建一个服务端和客户端,演示调用过程。',
'create_time'=>time(),
'update_time'=>time(),
]
];
$response = $client->create($params);
// 查询
$params = [
'index' => 'test_index',
'body' => [
'query' => [
'bool' => [
'should' => [
[
'match' => [
'title' => '调用'
]
],
[
'match' => [
'content' => '调用'
]
]
]
]
]
]
];
$response = $client->search($params);
var_dump($response->asArray());