paul / solr-bundle
Symfony2 Solr集成包
Requires
- php: >=5.4.5
- doctrine/orm: ^2.3
- solarium/solarium: ^3.5
- symfony/config: ^2.3
- symfony/dependency-injection: ^2.3
- symfony/doctrine-bridge: ^2.3
- symfony/http-kernel: ^2.3
Requires (Dev)
- behat/behat: 3.*
This package is auto-updated.
Last update: 2024-08-29 04:17:49 UTC
README
简介
此包提供了一个简单的API来索引和查询Solr索引。
安装
安装是一个快速(我保证!)的3步过程
- 下载SolrBundle
- 启用Bundle
- 配置SolrBundle
步骤1:下载SolrBundle
此包在Packagist上可用。您可以使用Composer安装它
$ composer require floriansemm/solr-bundle
步骤2:启用Bundle
最后,在内核中启用Bundle
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new FS\SolrBundle\FSSolrBundle(), ); }
步骤3:配置SolrBundle
# app/config/config.yml fs_solr: endpoints: core1: host: host port: 8983 path: /solr/core1 core: corename timeout: 5 core2: host: host port: 8983 path: /solr/core2 core: corename timeout: 5
使用此配置,您可以设置两个核心:core1
和core2
。有关更多信息,请参阅指定核心
部分。
用法
注解
要将实体放入索引,您必须向实体添加一些注解
// your Entity // .... use FS\SolrBundle\Doctrine\Annotation as Solr; /** * @Solr\Document(repository="Full\Qualified\Class\Name") * @ORM\Table() */ class Post { /** * @Solr\Id * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * * @Solr\Field(type="string") * * @ORM\Column(name="title", type="string", length=255) */ private $title = ''; /** * * @Solr\Field(type="string") * * @ORM\Column(name="text", type="text") */ private $text = ''; /** * @Solr\Field(type="date") * * @ORM\Column(name="created_at", type="datetime") */ private $created_at = null; }
支持的字段类型
目前实现了一套基本类型。
- 字符串
- 文本
- 日期
- 整数
- 浮点数
- 双精度浮点数
- 长整型
- 布尔型
可以使用自定义字段类型(schema.xml)。
过滤器注解
在某些情况下,实体不应索引。为此,您有SynchronizationFilter
注解。
/** * @Solr\Document * @Solr\SynchronizationFilter(callback="shouldBeIndex") */ class SomeEntity { /** * @return boolean */ public function shouldBeIndex() { // put your logic here } }
回调属性指定一个可调用的函数,该函数决定是否应索引。
指定核心
您可以指定一个用于文档的核心
/** * @Solr\Document(index="core0") */ class SomeEntity { // ... }
所有文档都将索引到核心core0
。如果您的实体/文档有不同的语言,则可以设置一个回调方法,该方法返回实体的首选核心。
/** * @Solr\Document(indexHandler="indexHandler") */ class SomeEntity { public function indexHandler() { if ($this->language == 'en') { return 'core0'; } } }
每个核心必须在config.yml中的endpoints
下设置。如果留空index
或indexHandler
属性,则将使用默认核心(endpoints
列表中的第一个)。要索引所有核心的文档,请使用*
作为索引值
@Solr\Document(index="*")
Solr字段配置
Solr提供了一系列预定义的字段名/字段类型映射
- 标题(solr-type: general_text)
- 文本(solr-type: general_text)
- 分类(solr-type: general_text)
- 内容类型(solr-type: string)
有关详细信息,请参阅您的schema.xml。
因此,如果您有一个具有“分类”属性的实体,则不需要在注解中进行类型声明
/** * @Solr\Field * @ORM\Column(name="category", type="text") */ private $category = '';
在这种情况下,字段自动具有“general_text”类型。
如果您持久化此实体,它将自动放入索引。更新和删除也会自动发生。
查询文档的字段
要查询索引,您必须调用一些服务。
$query = $this->get('solr.client')->createQuery('AcmeDemoBundle:Post'); $query->addSearchTerm('title', 'my title'); $result = $query->getResult();
$result数组包含所有找到的实体。solr-service为您将SolrDocument映射到实体。
查询文档的所有字段
前面的示例仅查询了字段'title'。您还可以使用字符串查询所有字段。
$query = $this->get('solr.client')->createQuery('AcmeDemoBundle:Post'); $query->queryAllFields('my title'); $result = $query->getResult();
定义结果映射
要缩小映射范围,您可以使用addField()
方法。
$query = $this->get('solr.client')->createQuery('AcmeDemoBundle:Post'); $query->addSearchTerm('title', 'my title'); $query->addField('id'); $query->addField('text'); $result = $query->getResult();
在这种情况下,只有字段id和text将被映射(addField()
),因此title和created_at将为空。如果没有找到任何内容,$result将为空。
默认情况下,结果包含10行。您可以增加此值
$query->setRows(1000000);
配置HydrationModes
HydrationMode告诉Bundle如何从文档创建实体。
FS\SolrBundle\Doctrine\Hydration\HydrationModes::HYDRATE_INDEX
- 仅使用solr数据FS\SolrBundle\Doctrine\Hydration\HydrationModes::HYDRATE_DOCTRINE
- 将Solr数据与整个Doctrine实体合并
使用自定义查询
$query = $this->get('solr.client')->createQuery('AcmeDemoBundle:Post'); $query->setHydrationMode($mode)
使用自定义文档存储库时,您必须自己设置属性 $hydrationMode
public function find($id) { $this->hydrationMode = HydrationModes::HYDRATE_INDEX; return parent::find($id); }
手动索引实体
要手动索引实体,您可以按照以下方式进行
$this->get('solr.client')->addDocument($entity); $this->get('solr.client')->updateDocument($entity); $this->get('solr.client')->removeDocument($entity);
removeDocument()
需要设置实体ID。
使用文档存储库
如果您指定了自己的存储库,则必须扩展 FS\SolrBundle\Repository\Repository
类。用法与Doctrine存储库相同
$myRepository = $this->get('solr.client')->getRepository('AcmeDemoBundle:Post'); $result = $myRepository->mySpecialFindMethod();
如果您在实体中没有声明具体的存储库,并且调用 $this->get('solr.client')->getRepository('AcmeDemoBundle:Post')
,您将得到一个 FS\SolrBundle\Repository\Repository
的实例。
命令
此捆绑包有两个命令
solr:index:clear
- 删除索引中的所有文档solr:synchronize
- 同步数据库与索引