PHP code example of gesof / elastic-search

1. Go to this page and download the library: Download gesof/elastic-search 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/ */

    

gesof / elastic-search example snippets


$text = 'UFOs over China';

$hosts = [
    'localhost:9200',
];
$client = \Elastic\Elasticsearch\ClientBuilder::create()
    ->setHosts($hosts)
    ->build();

$qb = new \Gesof\ElasticSearch\QueryBuilder($client);

$qb
    ->setTable('posts')
    ->orderBy('_id', 'desc')
;

$andX = $qb->expr()->andX();
$andX->add($qb->expr()->eq('is_completed', TRUE));
$andX->add($qb->expr()->gt('view_count', 10));

$orX = $qb->expr()->orX();
$orX->add($qb->expr()->matchText('title', $text));
$orX->add($qb->expr()->matchText('description', $text));

$andX->add($orX);

$qb->where($andX);
$qb
    ->setMaxResults(10)
    ->setFirstResult(0)
;

$resultCount = $qb->getQuery()->count()->getCount();
$documents = $qb->getQuery()->search()->getDocuments();

foreach ($documents as $document) {
    // echo $document->title . '<br />';
}