lekarna/search
Composer 安装命令:
composer require lekarna/search
包简介
ElasticSearch and Solr entity mapping library
README 文档
README
Note: This project is a prototype at the moment. See demo folder for practical implementation example.
Supported search engines
- ElasticSearch (functional)
- Solr (partial implementation)
Features
- SearchManager
- Can be used stand-alone or in a hybrid configuration
- Configurable search manager supports aggregate entity manager
- supports direct API calls through search engine adapters such as Elastica
- transforms returned ID's via batch operation into hydrated objects as required
- Supports event manager listeners for customizable entity handling
- Support for indexing through event listeners via JMS Serializer or simple entity callback.
- Annotations for index and data type creation using ObjectManager::getClassMetadata() as the base structure
#Usage#
Configuration
The search manager connection can be configured as shown in the following example:
$config = new Doctrine\Search\Configuration(); $config->setMetadataCacheImpl(new Doctrine\Common\Cache\ArrayCache()); $config->setEntitySerializer( new Doctrine\Search\Serializer\JMSSerializer( JMS\Serializer\SerializationContext::create()->setGroups('search') ) ); $eventManager = new Doctrine\Search\EventManager(); $eventManager->addListener($listener); $searchManager = new Doctrine\Search\SearchManager( $config, new Doctrine\Search\ElasticSearch\Client( new Elastica\Client(array( array('host' => 'localhost', 'port' => '9200') ) ), $eventManager );
Mappings
Basic entity mappings for index and type generation can be annotated as shown in the following example. Mappings can be rendered into a format suitable for automatically generating indexes and types using a build script (advanced setup required).
<?php namespace Entities; use Doctrine\Search\Mapping\Annotations as MAP; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity * @MAP\ElasticSearchable(index="indexname", type="post", source=true) */ class Post { /** * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") * @MAP\ElasticField(type="integer", includeInAll=false) */ private $id; /** * @ORM\Column(type="string") * @MAP\ElasticField(type="string", includeInAll=true, boost=5.0) */ private $title; /** * @ORM\Column(type="text") * @MAP\ElasticField(type="string", includeInAll=true) */ private $content; /** * @MAP\ElasticField(name="tags", type="string", includeInAll=false, index="not_analyzed") */ public function getTags() { return $this->tags->slice(0,3); } }
Indexing
Documents can be serialized for indexing currently in the following ways. If required an event listener can be used with your ORM as shown in this example. If an event listener is not needed, entities can be persisted or removed directly using the search manager.
<?php namespace Entities\Listener; use Doctrine\ORM\Event\LifecycleEventArgs; use Entities\Behaviour\SearchableEntityInterface; class SearchableListener implements { protected function getSearchManager() { return $this->getDatabaseConnection('elasticsearch'); } public function postPersist(LifecycleEventArgs $oArgs) { $oEntity = $oArgs->getEntity(); if($oEntity instanceof SearchableEntityInterface) { $this->getSearchManager()->persist($oEntity); } } public function postRemove(LifecycleEventArgs $oArgs) { $oEntity = $oArgs->getEntity(); if($oEntity instanceof SearchableEntityInterface) { $this->getSearchManager()->remove($oEntity); } } }
CallbackSerializer
This approach simply expects a toArray() method on the entity, although this method be configured as required.
The interface suggested in this example can be any interface you desire, as long as your event listener can identify
entities that need to be persisted to the search engine (see above example).
... use Entities\Behaviour\SearchableEntityInterface class Post implements SearchableEntityInterface { ... public function toArray() { return array( 'id' => $this->id, 'title' => $this->title, 'content' => $this->content ... ); } }
JMS Serializer
You can alternatively use the advanced serialization power of the JMS Serializer to automatically handle
serialization for you based on annotations such as those shown in this example.
... use JMS\Serializer\Annotation as JMS; use Entities\Behaviour\SearchableEntityInterface /** * @ORM\Entity * @MAP\ElasticSearchable(index="indexname", type="post", source=true) * @JMS\ExclusionPolicy("all") */ class Post implements SearchableEntityInterface { ... /** * @ORM\Column(type="string") * @MAP\ElasticField(type="string", includeInAll=true, boost=5.0) * @JMS\Expose * @JMS\Groups({"public", "search"}) */ private $title; /** * @ORM\Column(type="text") * @MAP\ElasticField(type="string", includeInAll=true) * @JMS\Expose * @JMS\Groups({"public", "search"}) */ private $content; ... }
AnnotationSerializer
Not yet available.
Queries
Queries can be executed through the search manager as shown below. Use of the result cache refers to using the
cache for the hydration query. Search engine specific adapter interfaces are exposed magically so as in this
example, Elastica\Query::addSort method is amalgamated with Doctrine\Search\Query methods. Thus any complexity
of query supported by the search engine client library is supported.
$hydrationQuery = $entityManager->createQueryBuilder() ->select(array('p', 'field(p.id, :ids) as HIDDEN field')) ->from('Entities\Post', 'p') ->where('p.id IN (:ids)') ->orderBy('field') ->getQuery(); $query = $searchManager->createQuery() ->from('Entities\Post') ->searchWith(new Elastica\Query()) ->hydrateWith($hydrationQuery) ->addSort('_score') ->setFrom(0) ->setLimit(10) ->getResult();
Simple Repository ID queries and Term search can by done using the following technique. Deserialization is done
independently of Doctrine\ORM but the same models are hydrated according to the registered SerializerInterface.
$entity = $searchManager->getRepository('Entities\Post')->find($id); $entity = $searchManager->getRepository('Entities\Post')->findOneBy(array($key => $term));
lekarna/search 适用场景与选型建议
lekarna/search 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 15.02k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 05 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「orm」 「solr」 「search」 「odm」 「lucene」 「elasticsearch」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lekarna/search 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lekarna/search 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lekarna/search 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A bundle which extend ibexa solr search handler
A Laravel package to retrieve data from Google Search Console
Kinikit - PHP Application development framework MVC component
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
PHP Database ORM for Symfony1. Do NOT use for new projects: please move to a newest Symfony release and Doctrine2
统计信息
- 总下载量: 15.02k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-05-28