PHP code example of nzo / elastic-query-bundle

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

    

nzo / elastic-query-bundle example snippets


use Nzo\ElasticQueryBundle\Query\ElasticQuerySearch;

class MyClass
{
    /**
     * @var ElasticQuerySearch
     */
    private $elasticQuerySearch;
    
    public function __construct(ElasticQuerySearch $elasticQuerySearch)
    {
        $this->elasticQuerySearch = $elasticQuerySearch;
    }
    
    /**
     * @param string|array $query  (json or array)
     * @param string $entityNamespace The FQCN (fully qualified class name) of the entity to execute the search on.
     * @param null|int $page
     * @param null|int $limit
     * @return array
     */
    public funtion foo($query, $entityNamespace, $page = null, $limit = null)
    {
        // $entityNamespace === 'App\Entity\FooBar'
        
        return $this->elasticQuerySearch->search($query, $entityNamespace, $page, $limit);
        
        // check access permission on the search
        return $this->elasticQuerySearch->search(
            $query,
            $entityNamespace,
            $page,
            $limit,
            ['role' => 'ROLE_SEARCH', 'message' => 'Search not authorized'] // 'message' is optional
        );
    }
}

    private float $_scoreElastic;

    public function getScoreElastic(): float
    {
        return $this->_scoreElastic;
    }

    public function setScoreElastic(float $scoreElastic): self
    {
        $this->_scoreElastic = $scoreElastic;

        return $this;
    }
 php
// config/bundles.php

return [
    // ...
    Nzo\ElasticQueryBundle\ElasticQueryBundle::class => ['all' => true],
];