daanbiesterbos / solr-bundle
Symfony Solr集成包
Requires
- php: ^7.3
- doctrine/annotations: ^1.7
- minimalcode/search: ^1.0
- myclabs/deep-copy: ^1.6
- ramsey/uuid: ^3.5|^4.1
- solarium/solarium: ^4.0
- symfony/cache: ^4.4|^5
- symfony/config: ^4.4|^5
- symfony/dependency-injection: ^4.4|^5
- symfony/doctrine-bridge: ^4.4|^5
- symfony/http-kernel: ^4.4|^5
Requires (Dev)
- doctrine/mongodb-odm: ^2.2
- doctrine/mongodb-odm-bundle: ^4.2
- doctrine/orm: ^2.4
- phpunit/phpunit: ^9
- roave/security-advisories: dev-master
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
Replaces
- floriansemm/solr-bundle: ^1.7
- dev-master
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- 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-08-29 05:31:19 UTC
README
简介
这是从https://github.com/floriansemm/SolrBundle的一个分支。原始包似乎已被放弃。我分支了这个包以解决一个相当严重的性能问题。您可以自由使用这个包或使用原始包。
此包提供了一个简单的API来索引和查询Solr索引。
安装
安装是一个三步过程
- 下载SolrBundle
- 启用包
- 配置SolrBundle
- 配置实体
第1步:安装SolrBundle
此包可在Packagist上找到。您可以使用Composer安装它。如果您尚未安装Solr Bundle,这将很容易。运行以下命令
composer require daanbiesterbos/solr-bundle
注意:如果您目前正在使用原始包,则更新稍微复杂一些。
当您删除旧包或在composer.json中更改包时,您会遇到问题。Symfony将暂时不支持fs_solr配置。此外,Composer会自动从bundles.php中删除包。在我看来,这是从原始包迁移的最可靠方法。
- 打开composer.json。
- 将floriansemm/solr-bundle重命名为daanbiesterbos/solr-bundle。
- 运行
composer update --no-scripts
,这将避免失败的cache:clear命令。 - 打开config/bundles.php
- 添加包(再次)
第2步:启用包
将包添加到config/bundles.php
return [ FS\SolrBundle\FSSolrBundle::class => ['all' => true], ];
第3步:配置SolrBundle
最后,配置包
# 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
选项中的任何值都将被忽略。
第4步:配置您的实体
要使实体可索引,您必须在实体中添加一些注解。基本配置需要两个注解:@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; }
现在,包处理您配置的实体的更新/插入/删除。
注解参考
@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,捆绑包将为您生成一个ID。如果您没有底层数据库为您生成增量ID,请使用此选项。
@Solr\Field
注解
应将此注解添加到需要索引的属性上。您应指定注解的 type
选项。
type
属性
目前,已实现了基本类型集
- 字符串(s)
- 文本(s)
- 日期(s)
- 整数(s)
- 浮点数(s)
- 双精度浮点数(s)
- 长整数(s)
- 布尔(s)
如果您有自定义的 schema.xml
,则不需要设置字段类型。
fieldModifier
属性
Solr支持对现有文档中字段的局部更新。支持的值有
- set
- add(仅限多值字段,向现有列表添加值(s))
- remove(仅限多值字段,从现有列表中删除值(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 Criteria 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
- 显示配置的文档
索引大量实体
对于多达300k个实体的集合,solr:index:populate
命令运行良好,但对于更大的集合,命令会变得非常缓慢。您可以在这里找到一些解决方案,说明如何同步您的数据库与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();