Skip to content

Latest commit

History

19 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Elasticsearch

中文说明

This component integrates the official Elasticsearch PHP client with Hyperf's Guzzle client and provides a static facade for configured connections.

Requirements

The package targets Hyperf 3.2 and requires elasticsearch/elasticsearch version 8 or 9, hyperf/config, hyperf/context, and hyperf/guzzle. It does not declare optional dependencies.

Installation

composer require friendsofhyperf/elasticsearch
php bin/hyperf.php vendor:publish friendsofhyperf/elasticsearch

The publish command creates config/autoload/elasticsearch.php.

Configuration

The published configuration defines the default connection. ELASTICSEARCH_HOST is split on commas, so it may contain multiple hosts. Additional named connections may be added under elasticsearch:

return [
'default' => [
'hosts' => explode(',', env('ELASTICSEARCH_HOST', '')),
],
'analytics' => [
'hosts' => ['http://127.0.0.1:9200'],
],
];

For facade connections, hosts may be an array or a comma-separated string. The facade only reads the hosts key; configure other client behavior through ClientBuilderFactory and the upstream builder.

Usage

Client Builder Factory

ClientBuilderFactory::create(array $options = []) passes $options to Hyperf's GuzzleClientFactory and returns an unbuilt upstream ClientBuilder. Configure the builder before calling build():

useFriendsOfHyperf\Elasticsearch\ClientBuilderFactory;
class Foo
{
publicfunction__construct(protectedClientBuilderFactory$clientBuilderFactory)
{
}
publicfunctionhandle()
{
$client = $this->clientBuilderFactory
->create(['timeout' => 5])
->setHosts(['http://127.0.0.1:9200'])
->build();
return$client->info();
}
}

The factory does not read config/autoload/elasticsearch.php; hosts and other builder settings must be applied explicitly.

Facade

Static facade calls use the default connection. Use connection() to select a named connection:

useFriendsOfHyperf\Elasticsearch\Facade\Elasticsearch;
$info = Elasticsearch::info();
$results = Elasticsearch::connection('analytics')->search([
'index' => 'articles',
'body' => [
'query' => [
'match_all' => (object) [],
],
],
]);

Each facade call builds a new client. If the selected connection is missing or null, the component throws InvalidArgumentException with the connection name.

Upstream API

Client methods, request parameters, response objects, authentication, and additional builder options are provided by the installed elasticsearch/elasticsearch version. Refer to the matching upstream client documentation when using them.

About

[READ-ONLY] Elasticsearch client for Hyperf.

Resources

Security policy

Stars

3 stars

Watchers

2 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages