3. Security/Authentication

You can connect to Elastic using Basic Authentication

 1<?php
 2
 3use ElasticSearch\ElasticSearch;
 4
 5class ExampleController extends Controller
 6{
 7    private $elastic;
 8
 9    public function __construct()
10    {
11        $this->elastic = new ElasticSearch();
12
13        $this->elastic->client()
14                    ->basicAuth('user', 'password');
15    }
16
17    public function nested()
18    {
19        $nested = $this->elastic
20                    ->nested()
21                    ->index('my-index')
22                    ->path('user')
23                    ->params('size', 1)
24                    ->addQuery('elastic.bool', function() {
25                        return $this
26                                ->must([
27                                    [
28                                        'match' => ['user.first' => 'Şero']
29                                    ]
30                                ])
31                                ->getMap();
32                    })
33                    ->search();
34
35        return $nested;
36    }
37}

You can set cloud id, hosts and api key

 1<?php
 2
 3use ElasticSearch\ElasticSearch;
 4
 5class ExampleController extends Controller
 6{
 7    private $elastic;
 8
 9    public function __construct()
10    {
11        $this->elastic = new ElasticSearch();
12
13        $hosts = [
14            'http://localhost:9200'
15        ];
16
17        $this->elastic->client()
18                    ->basicAuth('user', 'password')
19                    ->hosts($hosts)
20                    ->cloudId('cloud-id')
21		    ->apiKey('enter-your-api-key')
22	    	    ->sslVerification('path/to/cacert.pem');
23    }
24
25    public function multiMatch()
26    {
27
28        $search = $this->elastic->multiMatch()
29                        ->index('my-index')
30                        ->text('Şêro')
31                        ->fuzziness(1)
32                        ->fields(['text^3', 'name'])
33                        ->search();
34
35        return $search;
36    }
37}