shopsys / postgres-search-bundle
使用 Doctrine 在 PostgreSQL 中进行全文搜索的工具。
0.4
2023-01-16 12:57 UTC
Requires
- php: >=8.1
- doctrine/orm: >=2.2.3
- symfony/framework-bundle: >=2.1
This package is auto-updated.
Last update: 2024-08-30 01:11:37 UTC
README
注意用户和贡献者
该项目不再积极维护和开发。
PostgreSearchBundle
Symfony2 扩展包,提供在 Doctrine 2 中使用 PostgreSQL 全文搜索的工具 全文搜索 PostgreSQL。
添加了 'tsvector' 类型,以便在映射中使用。
添加了 'to_tsquery', 'plainto_tsquery' 和 'ts_rank' 函数,以便在 DQL 中使用。
步骤 1: 使用 composer 下载 PostgreSearchBundle
在您的 composer.json 中添加 PostgreSearchBundle
{ "require": { "shopsys/postgres-search-bundle": "0.1" } }
现在,通过运行以下命令让 composer 下载扩展包
$ php composer.phar update shopsys/postgres-search-bundle
步骤 2: 启用扩展包
在内核中启用扩展包
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new Intaro\PostgresSearchBundle\IntaroPostgresSearchBundle(), ); }
步骤 3: 映射示例
/** * @var string * * @ORM\Column(name="search_fts", type="tsvector", options={"customSchemaOptions": {"searchFields":{"name", "genre"}}}, nullable=true) */ protected $searchFts;
步骤 4: 在 DQL 中使用
$searchQuery = 'family | history'; $em = $this->getDoctrine()->getManager(); $query = $em->createQuery( 'SELECT b.id, sum(TSRANK(b.searchFts, :searchQuery)) as rank FROM DemoSearchBundle:Books b WHERE TSQUERY( b.searchFts, :searchQuery, \'simple\' ) = true GROUP BY b.id ORDER BY rank DESC') ->setParameter('searchQuery', $searchQuery) ; $result = $query->getArrayResult();
结果示例
Array ( [0] => Array ( [id] => 2 [rank] => 0.0607927 ) [1] => Array ( [id] => 3 [rank] => 0.0303964 ) )