intaro / postgres-search-bundle
用于在 Doctrine 中使用全文搜索 PostgreSQL 的工具。
dev-master
2021-08-27 13:13 UTC
Requires
- php: >=5.3.3
- doctrine/orm: >=2.2.3
- symfony/framework-bundle: ~3.0|^4.0|^5.0
This package is not auto-updated.
Last update: 2024-09-21 03:40:35 UTC
README
Symfony2 插件,包含在 Doctrine 2 中使用全文搜索 PostgreSQL 的工具。
添加了类型 'tsvector',可用于映射。
添加了函数 'to_tsquery', 'plainto_tsquery' 和 'ts_rank',可用于 DQL。
第 1 步:使用 composer 下载 PostgreSearchBundle
在 composer.json 中添加 PostgreSearchBundle
{ "require": { "intaro/postgres-search-bundle": "dev-master" } }
现在运行命令来告诉 composer 下载插件
$ php composer.phar update intaro/postgres-search-bundle
第 2 步:启用插件
在 kernel 中启用插件
<?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 ) )