乐趣区

关于elasticsearch:PHP-如何使用-Elasticsearch-API接口

实战场景

如何在 PHP 中应用 Elasticsearch API 接口

知识点

•PHP

•Elasticsearch

•Restful 接口

菜鸟实战

装置 Elasticsearch 官网 PHP SDK

https://github.com/elastic/el…

composer require elasticsearch/elasticsearch

创立 ES Client

use Elastic\Elasticsearch\ClientBuilder;

public function info(Request $request){

// 初始化

$client = ClientBuilder::create()

    ->setHosts(['localhost:9200'])

    ->build();



$response = $client->info();



// 响应格式化

$info = $response->asArray();



$data = ['es_info' => $info,];

$this->success($data);

}

执行返回后果

JSON
{

    "code": 0,

    "message": "ok",

    "data": {

        "es_info": {

            "name": "xqdsvtvnyje9zxeo",

            "cluster_name": "elasticsearch",

            "cluster_uuid": "tNPisFAxTVaRdXHKATIUWQ",

            "version": {

                "number": "8.5.1",

                "build_flavor": "default",

                "build_type": "tar",

                "build_hash": "c1310c45fc534583afe2c1c03046491efba2bba2",

                "build_date": "2022-11-09T21:02:20.169855900Z",

                "build_snapshot": false,

                "lucene_version": "9.4.1",

                "minimum_wire_compatibility_version": "7.17.0",

                "minimum_index_compatibility_version": "7.0.0"

            },

            "tagline": "You Know, for Search"

        }

    }

}

通过上述步骤,就将 php 与 Elasticsearch 连通了,接下来应用 sdk 中的 api 即可。

退出移动版