PHP code example of carterzhou / elasticsearch

1. Go to this page and download the library: Download carterzhou/elasticsearch library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

carterzhou / elasticsearch example snippets


use CarterZhou\Elasticsearch\Client;

class TestController extends Controller
{
    protected $client;

    /**
     * TestController constructor.
     * @param Client $client
     */
    public function __construct(Client $client)
    {
        $this->client = $client;
    }
}

$url = $this->client->getHost() . '/indices-2019.01.28';

$this->client
    ->match('request', 'one-of-urls')
    ->match('request', 'field1 field2')
    ->setSize(500);

$this->client->search($url);

if ($this->client->hasDocuments()) {
    foreach ($this->client->getDocuments() as $document) {
        // Process your document here...
    }
}

$url = $this->client->getHost() . '/indices-2019.01.28';

$this->client
    ->match('request', 'one-of-urls')
    ->match('request', 'field1 field2')
    ->setSize(500);

do {
    $this->client->search($url);

    if ($this->client->hasDocuments()) {

        foreach ($this->client->getDocuments() as $redirect) {
            // Process your document here...
        }
    }
} while ($this->client->hasMoreDocuments());

$url = $this->client->getHost() . '/logstash*';

$this->client->matchAll()->setSize(500);

$this->client->scroll($url);

do {
    foreach ($this->client->getDocuments() as $document) {
        // Process your document here...
    }

    $this->client->scroll($url);

} while ($this->client->hasDocuments());