gesof/elastic-search

Elastic Search utility

1.1.0 2023-04-27 13:18 UTC

This package is auto-updated.

Last update: 2024-04-27 15:58:19 UTC


README

$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 />';
}