hbryan / postgres-search-bundle
用于在Doctrine中使用全文搜索PostgreSQL的工具。
dev-master
2017-12-26 05:48 UTC
Requires
- php: >=5.3.3
- doctrine/orm: >=2.2.3
- symfony/framework-bundle: >=2.1
This package is not auto-updated.
Last update: 2024-09-29 04:43:10 UTC
README
Symfony2扩展包,提供了在Doctrine 2中实现全文搜索PostgreSQL的工具。
添加了'tsvector'类型,以便在映射中使用。
添加了'to_tsquery', 'plainto_tsquery' 和 'ts_rank' 函数,以便在DQL中使用。
步骤 1: 使用composer下载PostgreSearchBundle
在composer.json中添加PostgreSearchBundle
{ "require": { "hbryan/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 ) )