PHP code example of miquido / elasticsearch-dbal

1. Go to this page and download the library: Download miquido/elasticsearch-dbal 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/ */

    

miquido / elasticsearch-dbal example snippets




use Miquido\Elasticsearch\DBAL;

$client = new \Elastica\Client();
$type = $client->getIndex('index_name')->getType('type_name');

$dbal = new DBAL($type);



$dbal->countAll(); // count all documents in the type
$dbal->count(new \Elastica\Query(new \Elastica\Query\Terms('field_name', [1, 2, 3]))); // count documents matching query




use Miquido\Elasticsearch\DBAL;

$dbal = new DBAL($type);

$query = new \Elastica\Query(new \Elastica\Query\Terms('field_name', [1, 2, 3]));

$dbal->search($query); // returns 10 documents (default ElasticSearch setting)
$dbal->searchAll($query); // returns all documents (uses scroll api)
$dbal->findOne($query);
$dbal->findByIds('id1', 'id2', 'id2');




use Miquido\Elasticsearch\DBAL;

$dbal = new DBAL($type);

$result = $dbal->search(new \Elastica\Query(new \Elastica\Query\MatchAll()));
$result->count(); // returns number of documents in result
$result->getTotalHits(); // returns number of documents matching the query
$result->getTime(); // returns time of the query
$result->getDocuments()->getAll(); // returns instances of Documents 
$result->getDocuments()->getData(); // returns instance of Miquido\DataStructure\Map\MapCollectionInterface 

$document = $dbal->findOne(new \Elastica\Query());
$document->getId(); // string
$document->getData(); // returns instance of Miquido\DataStructure\Map\MapInterface  



use Miquido\Elasticsearch\DBAL;
use Miquido\Elasticsearch\Document\Document;
use Miquido\DataStructure\Map\Map;

$dbal = new DBAL($type);
$dbal->add(new Document(
    null /* or string if you want to choose your own ID */, 
    new Map([
        'name' => 'John',
        'surname' => 'Smith',
        'age' => 40,
    ]))
);

// you can also add many documents at once
$dbal->bulkAdd($document1, $document2, ...);




use Miquido\Elasticsearch\DBAL;
use Miquido\Elasticsearch\Document\Document;
use Miquido\DataStructure\Map\Map;

$dbal = new DBAL($type);

// this method will only update 'age' field in document with ID 'documentId'
$dbal->updatePatch(new Document('documentId', new Map([
    'age' => 41,
])));

// you can also add many documents at once
$dbal->bulkUpdatePatch($document1, $document2, ...);



use Miquido\Elasticsearch\DBAL;

$dbal = new DBAL($type);
$dbal->deleteByIds('id1', 'id2', 'id3');