nzo / elastic-query-bundle
Symfony bundle used to execute search based on simple query language for the Elasticsearch system
Installs: 767
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 1
Type:symfony-bundle
Requires
- php: >=7.2.5
- ext-json: *
- doctrine/orm: ^2.6
- doctrine/persistence: ^1.0|^2.0
- friendsofsymfony/elastica-bundle: ^6.0.0-beta4
- justinrainbow/json-schema: ^5.2
- symfony/framework-bundle: ^4.4|^5.0
- symfony/http-foundation: ^4.4|^5.0
- symfony/http-kernel: ^4.4|^5.0
- symfony/options-resolver: ^4.4|^5.0
- symfony/security-core: ^4.4|^5.2.8
- symfony/serializer: ^4.4|^5.0
- symfony/string: ^5.0
Requires (Dev)
- phpunit/phpunit: ^4.8 || ^5.0
This package is auto-updated.
Last update: 2024-12-06 15:20:46 UTC
README
- Symfony bundle used to execute search based on simple query language for the Elasticsearch system.
- This bundle is based on the FOSElasticaBundle implementation cf: https://github.com/FriendsOfSymfony/FOSElasticaBundle
Versions & Dependencies
Features included:
- Search: match, notmatch, isnull, in, notin, gte, gt, lte, lt, range, wildcard.
- Sort
- Limitation
- Pagination
This Bundle is compatible with Symfony >= 4.4
Installation
Install the bundle:
$ composer require nzo/elastic-query-bundle
Register the bundle in config/bundles.php (without Flex)
// config/bundles.php return [ // ... Nzo\ElasticQueryBundle\ElasticQueryBundle::class => ['all' => true], ];
Configure the bundle:
# config/packages/nzo_elastic_query.yaml nzo_elastic_query: elastic_index_prefix: # optional (the index prefix) default_page_number: # optional (default 1) limit_per_page: # optional (default 100) items_max_limit: # optional (default 1000) show_score: # optional (default false)
Usage
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 ); } }
When show_score is enabled, you must add the field _scoreElastic to the needed entities and add the field getter and setter (getScoreElastic & setScoreElastic)
private float $_scoreElastic; public function getScoreElastic(): float { return $this->_scoreElastic; } public function setScoreElastic(float $scoreElastic): self { $this->_scoreElastic = $scoreElastic; return $this; }
Configure index
fos_elastica: indexes: user: properties: id: type: keyword index: true createdAt: type: date persistence: driver: orm model: App\Entity\User provider: ~ finder: ~ repository: Nzo\ElasticQueryBundle\Repository\SearchRepository
Populate index
$ bin/console fos:elastica:populate
Payload Example
POST http://example.fr/search/myEnitity?page=1&limit=2
{ "query": { "search": { "or": [ { "field": "status", "match": "foo" }, { "field": "entity.title", "notmatch": "bar" }, { "field": "entity.title", "wildcard": "*toto*" }, { "and": [ { "field": "lastname", "notin": [ "titi", "tata" ] }, { "or": [ { "field": "age", "range": [ 20, 30 ] }, { "field": "parent.age", "gte": 25 }, { "and": [ { // This check is needed to make sure the 'parent' is not Null "field": "parent.id", "isnull": false }, { "field": "parent.text", "isnull": true } ] } ] } ] } ] }, "sort": [ { "field": "createdAt", "order": "ASC" } ] } }
License
This bundle is under the MIT license. See the complete license in the bundle:
See LICENSE