2
2
- [ Example usage] ( #example-usage )
3
3
# User Guide
4
4
5
- Install this client using Composer into your project ` composer req opensearch-project/opensearch-php `
5
+ Install this client using Composer into your project
6
+ ``` bash
7
+ composer require opensearch-project/opensearch-php`
8
+ ` ` `
9
+
10
+ Install a PSR-18 compatible HTTP client. For example, to use Guzzle:
11
+ ` ` ` bash
12
+ composer require guzzlehttp/guzzle
13
+ ` ` `
14
+
15
+ Alternatively, you can use Symfony HTTP Client:
16
+ ` ` ` bash
17
+ composer require symfony/http-client
18
+ ` ` `
6
19
7
20
# # Example usage
8
21
@@ -25,23 +38,11 @@ class MyOpenSearchClass
25
38
public function __construct()
26
39
{
27
40
// Simple Setup
28
- $this->client = OpenSearch\ClientBuilder::fromConfig([
29
- 'hosts' => [
30
- 'https://localhost:9200'
31
- ],
32
- 'retries' => 2,
33
- 'handler' => OpenSearch\ClientBuilder::multiHandler()
41
+ $client = (new \O penSearch\G uzzleClientFactory())-> create([
42
+ ' base_uri' => ' https://localhost:9200' ,
43
+ ' auth' => [' admin' , getenv(' OPENSEARCH_PASSWORD' )],
44
+ ' verify' => false,
34
45
]);
35
-
36
- // OR via Builder
37
- // $this->client = (new \OpenSearch\ClientBuilder())
38
- // ->setHosts(['https://localhost:9200'])
39
- // ->setBasicAuthentication('admin', 'admin') // For testing only. Don't store credentials in code.
40
- // // or, if using AWS SigV4 authentication:
41
- // ->setSigV4Region('us-east-2')
42
- // ->setSigV4CredentialProvider(true)
43
- // ->setSSLVerification(false) // For testing only. Use certificate for validation
44
- // ->build();
45
46
}
46
47
47
48
@@ -359,7 +360,64 @@ try {
359
360
360
361
` ` `
361
362
362
- ## ClientBuilder
363
+ # # Client Factories
364
+
365
+ You can create an OpenSearch Client using any [PSR-18](https://www.php-fig.org/psr/psr-18/) compatible
366
+ HTTP client. Two factories are provided to reduce the boilerplate code required to create a client
367
+ using [Guzzle](https://docs.guzzlephp.org/en/stable/) or [Symfony](https://symfony.com/doc/current/http_client.html)
368
+ HTTP clients.
369
+
370
+ The ` GuzzleClientFactory` and ` SymfonyClientFactory` classes are used to create an OpenSearch Client instance.
371
+
372
+ # ## Guzzle Client Factory
373
+
374
+ This factory creates an OpenSearch Client instance using the Guzzle HTTP client.
375
+
376
+ ` ` ` bash
377
+ composer require guzzlehttp/guzzle
378
+ ` ` `
379
+
380
+ ` ` ` php
381
+ $client = (new \O penSearch\G uzzleClientFactory())-> create([
382
+ ' base_uri' => ' https://localhost:9200' ,
383
+ ' auth' => [' admin' , getenv(' OPENSEARCH_PASSWORD' )],
384
+ ' verify' => false,
385
+ ]);
386
+ ` ` `
387
+ ` GuzzleClientFactory::__construct()` accepts an ` int $maxRetries ` as the first argument to enable retrying a request
388
+ when a ` 500 Server Error` status code is received in the response, or network connection error occurs. A PSR-3 Logger
389
+ can be passed as the second argument to log the retries.
390
+
391
+ [Guzzle Request options](https://docs.guzzlephp.org/en/stable/request-options.html) can be passed to the
392
+ ` create()` method. The ` base_uri` option is * required* .
393
+
394
+ # ## Symfony Client Factory
395
+
396
+ This factory creates an OpenSearch Client instance using the Symfony HTTP client.
397
+
398
+ ` ` ` bash
399
+ composer require symfony/http-client
400
+ ` ` `
401
+
402
+ ` ` ` php
403
+ $client = (new \O penSearch\S ymfonyClientFactory())-> create([
404
+ ' base_uri' => ' https://localhost:9200' ,
405
+ ' auth_basic' => [' admin' , getenv(' OPENSEARCH_PASSWORD' )],
406
+ ' verify_peer' => false,
407
+ ]);
408
+ ` ` ` `
409
+
410
+ `SymfonyClientFactory::__construct ()` accepts an ` int $maxRetries ` as the first argument to enable retrying a request
411
+ when a ` 500 Server Error` status code is received in the response, or network connection error occurs. A PSR-3 Logger
412
+ can be passed as the second argument to log the retries.
413
+
414
+ [Symfony HTTP Client configuration options](https://symfony.com/doc/current/http_client.html#configuration-options) can
415
+ be passed to the ` create()` method. The ` base_uri` option is * required* .
416
+
417
+ # # ClientBuilder (Deprecated)
418
+
419
+ ** ` ClientBuilder` is deprecated in 2.4.0 and will be removed in 3.0.0. Use the ` GuzzleClientFactory` or ` SymfonyClientFactory` instead.**
420
+
363
421
364
422
The ` \O penSearch\C lientBuilder` class is used to create a ` \O penSearch\C lient` instance. It provides a fluent interface for configuring the client.
365
423
0 commit comments