nzo / elastic-query-bundle
用于基于简单查询语言执行Elasticsearch系统搜索的Symfony包
v3.1.1
2021-12-01 09:09 UTC
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-09-06 14:49:56 UTC
README
- 用于基于简单查询语言执行Elasticsearch系统搜索的Symfony包。
- 此包基于FOSElasticaBundle实现,参照: https://github.com/FriendsOfSymfony/FOSElasticaBundle
版本及依赖
包含功能
- 搜索: match、notmatch、isnull、in、notin、gte、gt、lte、lt、range、wildcard。
- 排序
- 限制
- 分页
此包与 Symfony >= 4.4 兼容
安装
安装包
$ composer require nzo/elastic-query-bundle
在 config/bundles.php 中注册包(不使用Flex)
// config/bundles.php return [ // ... Nzo\ElasticQueryBundle\ElasticQueryBundle::class => ['all' => true], ];
配置包
# 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)
使用
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 ); } }
当 show_score 启用时,必须将字段 _scoreElastic 添加到所需的实体中,并添加字段 getter 和 setter(getScoreElastic & setScoreElastic)
private float $_scoreElastic; public function getScoreElastic(): float { return $this->_scoreElastic; } public function setScoreElastic(float $scoreElastic): self { $this->_scoreElastic = $scoreElastic; return $this; }
配置索引
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
填充索引
$ bin/console fos:elastica:populate
有效载荷示例
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" } ] } }
许可证
此包采用MIT许可证。请参阅包中的完整许可证
查看 LICENSE