floriansemm / solr-bundle
Symfony Solr集成包
Requires
- php: ^7.0
- minimalcode/search: ^1.0
- ramsey/uuid: ^3.5
- solarium/solarium: ^3.6
- symfony/config: ^2.3|^3.0
- symfony/dependency-injection: ^2.3|^3.0
- symfony/doctrine-bridge: ^2.3|^3.0
- symfony/http-kernel: ^2.3|^3.0
Requires (Dev)
- behat/behat: ^3.1
- doctrine/mongodb-odm-bundle: *
- doctrine/orm: ^2.3
- phpunit/phpunit: ^5.4
Suggests
- doctrine/mongodb-odm-bundle: Required if you want to use the MongoDB ODM
- doctrine/orm: Required if you want to use the Doctrine ORM
- dev-master
- v2.0-alpha1
- v1.7.1
- v1.7
- 1.6.x-dev
- v1.6.8
- v1.6.7
- v1.6.6.1
- v1.6.6
- v1.6.5
- v1.6.4
- v1.6.3
- v1.6.2
- v1.6.1
- v1.6
- v1.5.5
- v1.5.4
- v1.5.3
- v1.5.2
- v1.5.1
- 1.5
- 1.4
- 1.4-beta2
- 1.4-beta
- 1.3.2
- 1.3.1
- 1.3
- 1.2.2
- 1.2.1
- 1.2
- 1.1
- 1.0.2
- 1.0.1
- 1.0
- 0.9
- 0.8
- 0.7
- 0.6
- v0.5.1
- 0.5
- 0.4
- 0.3.1
- 0.3
- 0.2.1
- 0.2
- 0.1
- dev-nested_documents
- dev-master-clone
This package is auto-updated.
Last update: 2024-09-07 23:24:16 UTC
README
介绍
此包提供了一个简单的API来索引和查询Solr索引。
安装
安装是一个快速(我保证!)的3步过程
- 下载SolrBundle
- 启用Bundle
- 配置SolrBundle
- 配置你的实体
第一步:下载SolrBundle
此包在Packagist上可用。您可以使用Composer安装它
$ composer require floriansemm/solr-bundle
第二步:启用Bundle
接下来,在kernel中启用此Bundle
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new FS\SolrBundle\FSSolrBundle(), ); }
第三步:配置SolrBundle
最后,配置此Bundle
# app/config/config.yml fs_solr: endpoints: core0: schema: http host: host port: 8983 path: /solr/core0 core: corename timeout: 5
任何未指定的选项都将使用默认值。
使用DSN
# app/config/config.yml fs_solr: endpoints: core0: dsn: http://host:8983/solr core: core0 timeout: 5
如果您使用dsn选项,则将忽略schema
、host
、port
和path
选项中的任何值。
第四步:配置你的实体
要使实体可索引,您必须向实体添加一些注解。基本配置需要两个注解:@Solr\Document()
、@Solr\Id()
。要将数据索引到,请在属性中添加@Solr\Field()
。
如果您想在没有任何数据库的情况下索引文档,则必须使用相同的注解。确保您已设置ID或设置@Solr\Id(generateId=true)
。
// .... use FS\SolrBundle\Doctrine\Annotation as Solr; /** * @Solr\Document() * @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", getter="format('Y-m-d\TH:i:s.z\Z')") * * @ORM\Column(name="created_at", type="datetime") */ private $created_at = null; }
此Bundle现在处理您配置的实体的更新/插入/删除。
注解参考
@Solr\Document
注解
此注解表示实体应作为文档进行索引。它有几个可选属性
repository
index
indexHandler
使用repository
选项设置自定义仓库类
如果您指定了自己的仓库,则仓库必须扩展FS\SolrBundle\Repository\Repository
类。
/** * @Solr\Document(repository="My/Custom/Repository") */ class SomeEntity { // ... }
index
属性
您可以指定文档将被索引的核心
/** * @Solr\Document(index="core0") */ class SomeEntity { // ... }
indexHandler
属性
默认情况下,所有文档都将索引到核心core0
。如果您的实体/文档有不同的语言,则可以设置一个回调方法,该方法应返回实体将被索引到的核心。
/** * @Solr\Document(indexHandler="indexHandler") */ class SomeEntity { public function indexHandler() { if ($this->language == 'en') { return 'core0'; } } }
每个核心都必须在config.yml
中的endpoints
下设置。如果您留空index
或indexHandler
属性,则将使用默认核心(endpoints
列表中的第一个)。要索引所有核心中的文档,请使用*
作为索引值。
@Solr\Id
注解
此注解是索引实体所必需的。此注解没有属性。您应将此注解添加到用作实体/文档主标识符的字段。
class Post { /** * @Solr\Id * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; }
generateId
选项
将此选项设置为true,则Bundle将为您生成ID。如果您的底层数据库不为您生成增量ID,请使用此选项。
@Solr\Field
注解
应将此注解添加到应索引的属性。您应指定注解的type
选项。
type
属性
目前实现了基本类型集
- string(s)
- text(s)
- date(s)
- integer(s)
- float(s)
- double(s)
- long(s)
- boolean(s)
如果您有定制的schema.xml
,则不需要设置字段类型。
fieldModifier
属性
Solr支持对现有文档中的字段进行部分更新。支持值包括
- set
- add (仅适用于多值字段,将值(s)添加到现有列表中)
- 删除(仅多值字段,从现有列表中删除值)
- inc(仅整数字段)
nestedClass
属性
设置此属性以对嵌套对象集合进行索引。
对象关系
@Solr\SynchronizationFilter(callback="shouldBeIndexed")
注解
在某些情况下,实体不应被索引。为此,您有 SynchronizationFilter
注解来运行过滤回调。
/** * // .... * @Solr\SynchronizationFilter(callback="shouldBeIndexed") */ class SomeEntity { /** * @return boolean */ public function shouldBeIndexed() { // put your logic here } }
回调属性指定一个可调用的函数,该函数应返回一个布尔值,指定是否应索引具体的实体。
查询
查询文档的字段
通过 solr.client
服务进行索引查询
$query = $this->get('solr.client')->createQuery('AcmeDemoBundle:Post'); $query->addSearchTerm('title', 'my title'); $query->addSearchTerm('collection_field', array('value1', 'value2')); $result = $query->getResult();
或
$posts = $this->get('solr.client')->getRepository('AcmeDemoBundle:Post')->findOneBy(array( 'title' => 'my title', 'collection_field' => array('value1', 'value2') ));
查询文档的所有字段
前面的示例仅查询了 title
字段。您还可以使用字符串查询所有字段。
$query = $this->get('solr.client')->createQuery('AcmeDemoBundle:Post'); $query->queryAllFields('my title'); $result = $query->getResult();
定义自定义查询字符串
如果您的查询需要更多灵活性,您可以定义自己的查询字符串
$query = $this->get('solr.client')->createQuery('AcmeDemoBundle:Post'); $query->setCustomQuery('id:post_* AND (author_s:Name1 OR author_s:Name2)'); $result = $query->getResult();
QueryBuilder
基于 https://github.com/minimalcode-org/search 预测 API 的查询构建器。
$queryBuilder = $this->get('solr.client')->getQueryBuilder('AcmeDemoBundle:Post'); $result = $queryBuilder ->where('author') ->is('Name1') ->orWhere('author') ->is('Name2') ->getQuery() ->getResult();
为了保持您的代码干净,您应将选择标准移动到存储库类中
class YourRepository extends Repository { public function findAuthor($name1, $name2) { return $this->getQueryBuilder() ->where('author') ->is($name1) ->orWhere('author') ->is($name2) ->getQuery() ->getResult(); } }
配置 HydrationModes
HydrationMode 告诉包如何从文档创建实体。
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); }
存储库
您应该定义自己的存储库类,以便使自定义查询可重用。有关如何配置文档存储库的信息,请参阅 注解部分
namespace AppBundle\Search; use FS\SolrBundle\Repository\Repository; class ProviderRepository extends Repository { public function findPost($what) { $query = $this->solr->createQuery('AcmeDemoBundle:Post'); // some query-magic here return $query->getResult(); } }
在您的存储库中,您完全有权访问查询构建器。
命令
以下是此包提供的所有命令
solr:index:clear
- 删除索引中的所有文档solr:index:populate
- 同步数据库与索引solr:schema:show
- 显示您配置的文档
索引大量实体
solr:index:populate
命令适用于最多 300k 实体的集合,对于更大的集合,该命令会非常慢。您可以在 此处 找到一些解决方案,以同步您的数据库与 Solr。
扩展 Solarium
要使用您自己的插件扩展 Solarium,创建一个标记服务
<tag name="solarium.client.plugin" plugin-name="yourPluginName"/>
要挂钩到 Solarium 事件,创建一个公共 Symfony 事件监听器
<tag name="kernel.event_listener" event="solarium.core.preExecuteRequest" method="preExecuteRequest" />
文档助手
检索最后一个插入实体 ID
$helper = $this->get('solr.client')->getDocumentHelper(); $id = $helper->getLastInsertDocumentId();