floriansemm/solr-bundle
Composer 安装命令:
composer require floriansemm/solr-bundle
包简介
Symfony Solr integration bundle
README 文档
README
Introduction
This Bundle provides a simple API to index and query a Solr Index.
Installation
Installation is a quick (I promise!) 3 step process:
- Download SolrBundle
- Enable the Bundle
- Configure the SolrBundle
- configure your entity
Step 1: Download SolrBundle
This bundle is available on Packagist. You can install it using Composer:
$ composer require floriansemm/solr-bundle
Step 2: Enable the bundle
Next, enable the bundle in the kernel:
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new FS\SolrBundle\FSSolrBundle(), ); }
Step 3: Configure the SolrBundle
Finally, configure the bundle:
# app/config/config.yml fs_solr: endpoints: core0: schema: http host: host port: 8983 path: /solr/core0 core: corename timeout: 5
Default values will be used for any option left out.
With DSN
# app/config/config.yml fs_solr: endpoints: core0: dsn: http://host:8983/solr core: core0 timeout: 5
Any values in schema, host, port and path option, will be ignored if you use the dsn option.
Step 4: Configure your entities
To make an entity indexed, you must add some annotations to your entity. Basic configuration requires two annotations:
@Solr\Document(), @Solr\Id(). To index data add @Solr\Field() to your properties.
If you want to index documents without any database, then you have to use the same annotations. Make sure you have set a Id or
set @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; }
The bundle handles now updates / inserts / deletions for your configured entity.
Annotation reference
@Solr\Document annotation
This annotation denotes that an entity should be indexed as a document. It has several optional properties:
repositoryindexindexHandler
Setting custom repository class with repository option
If you specify your own repository, the repository must extend the FS\SolrBundle\Repository\Repository class.
/** * @Solr\Document(repository="My/Custom/Repository") */ class SomeEntity { // ... }
index property
It is possible to specify a core the document will be indexed in:
/** * @Solr\Document(index="core0") */ class SomeEntity { // ... }
indexHandler property
By default, all documents will be indexed in the core core0. If your entities/documents have different languages, then you can setup
a callback method, which should return the core the entity will be indexed in.
/** * @Solr\Document(indexHandler="indexHandler") */ class SomeEntity { public function indexHandler() { if ($this->language == 'en') { return 'core0'; } } }
Each core must be set up in config.yml under endpoints. If you leave the index or indexHandler property empty,
then the default core will be used (first one in the endpoints list). To index a document in all cores, use * as index value.
@Solr\Id annotation
This annotation is required to index an entity. The annotation has no properties. You should add this annotation to the field that will be used as the primary identifier for the entity/document.
class Post { /** * @Solr\Id * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; }
generateId option
Set this option to true and a the bundle will generate a Id for you. Use this option if you have no underlying DB which generates incremental Ids for you.
@Solr\Field annotation
This annotation should be added to properties that should be indexed. You should specify the type option for the annotation.
type property
Currently, a basic set of types is implemented:
- string(s)
- text(s)
- date(s)
- integer(s)
- float(s)
- double(s)
- long(s)
- boolean(s)
If you have a customized schema.xml than you don't need to setup a field-type.
fieldModifier property
Solr supports partial updates of fields in an existing document. Supported values are:
- set
- add (multivalue field only, adds a value(s) to a existing list)
- remove (multivalue field only, removes a value(s) from existing list)
- inc (integer field only)
nestedClass property
Set this property if you want to index collections with nested Objects.
Object relations
For more information read the more detailed "How to index relation" guide
@Solr\SynchronizationFilter(callback="shouldBeIndexed") annotation
In some cases, an entity should not be indexed. For this, you have the SynchronizationFilter annotation to run a filter-callback.
/** * // .... * @Solr\SynchronizationFilter(callback="shouldBeIndexed") */ class SomeEntity { /** * @return boolean */ public function shouldBeIndexed() { // put your logic here } }
The callback property specifies an callable function, which should return a boolean value, specifying whether a concrete entity should be indexed.
Queries
Query a field of a document
Querying the index is done via the solr.client service:
$query = $this->get('solr.client')->createQuery('AcmeDemoBundle:Post'); $query->addSearchTerm('title', 'my title'); $query->addSearchTerm('collection_field', array('value1', 'value2')); $result = $query->getResult();
or
$posts = $this->get('solr.client')->getRepository('AcmeDemoBundle:Post')->findOneBy(array( 'title' => 'my title', 'collection_field' => array('value1', 'value2') ));
Query all fields of a document
The previous examples were only querying the title field. You can also query all fields with a string.
$query = $this->get('solr.client')->createQuery('AcmeDemoBundle:Post'); $query->queryAllFields('my title'); $result = $query->getResult();
Define a custom query string
If you need more flexiblity in your queries you can define your own query strings:
$query = $this->get('solr.client')->createQuery('AcmeDemoBundle:Post'); $query->setCustomQuery('id:post_* AND (author_s:Name1 OR author_s:Name2)'); $result = $query->getResult();
The QueryBuilder
The query-builder based on 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();
To keep your code clean you should move the select-criteria in a repository-class:
class YourRepository extends Repository { public function findAuthor($name1, $name2) { return $this->getQueryBuilder() ->where('author') ->is($name1) ->orWhere('author') ->is($name2) ->getQuery() ->getResult(); } }
Configure HydrationModes
HydrationMode tells the bundle how to create an entity from a document.
FS\SolrBundle\Doctrine\Hydration\HydrationModes::HYDRATE_INDEX- use only the data from solrFS\SolrBundle\Doctrine\Hydration\HydrationModes::HYDRATE_DOCTRINE- merge the data from solr with the entire doctrine-entity
With a custom query:
$query = $this->get('solr.client')->createQuery('AcmeDemoBundle:Post'); $query->setHydrationMode($mode)
With a custom document-repository you have to set the property $hydrationMode itself:
public function find($id) { $this->hydrationMode = HydrationModes::HYDRATE_INDEX; return parent::find($id); }
Repositories
Your should define your own repository-class to make your custom queries reuseable. How to configure a repository for a document have a look at the annotation section
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(); } }
In your repository you have full access to the querybuilder.
Commands
Here's all the commands provided by this bundle:
solr:index:clear- delete all documents in the indexsolr:index:populate- synchronize the db with the indexsolr:schema:show- shows your configured documents
Indexing huge sets of entities
The solr:index:populate command works well for sets up to 300k entities, everthing large makes the command very slow. You can find here some solution how to sync your DB with Solr.
Extend Solarium
To extend Solarium with your own plugins, create a tagged service:
<tag name="solarium.client.plugin" plugin-name="yourPluginName"/>
To hook into the Solarium events create a common Symfony event-listener:
<tag name="kernel.event_listener" event="solarium.core.preExecuteRequest" method="preExecuteRequest" />
Document helper
Retrieve the last insert entity-id
$helper = $this->get('solr.client')->getDocumentHelper(); $id = $helper->getLastInsertDocumentId();
floriansemm/solr-bundle 适用场景与选型建议
floriansemm/solr-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 80.56k 次下载、GitHub Stars 达 122, 最近一次更新时间为 2012 年 10 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「solr」 「search」 「index」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 floriansemm/solr-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 floriansemm/solr-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 floriansemm/solr-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A bundle which extend ibexa solr search handler
A Laravel package to retrieve data from Google Search Console
This extension provides integration with solr to output content from TYPO3 in JSON format.
Laminas Module for interaction between a laminas application and a SOLR search engine using Solarium
The bundle for easy using json-rpc api on your project
Indexed Search Autocomplete - Extends the TYPO3 Core Extension Indexed_Search searchform with an autocomplete feature.
统计信息
- 总下载量: 80.56k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 124
- 点击次数: 29
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2012-10-20