我在我的项目中使用FOSElasticaBundle和Doctrine,我的代码用于使用Doctrine生命周期事件进行选择性索引更新。我遇到的问题是我是否单独更新相关实体。
例如,一个人可能通过多种关系与公司有关。如果我直接通过公司实体更新公司名称,那么与公司相关人员的索引将过时,并且仍与公司的旧名称相关。
我有点迷失如何处理这个,有没有人有任何建议?我是否必须依赖计划的索引更新并同时处理不准确的索引数据,或者我是否可以调用与已更新的实体相关的实体的更新。
我依靠JMSSerializer组来建立映射。我很欣赏这可能不是长期做事的最佳方式。
答案 0 :(得分:11)
我遇到了同样的问题。看来我的安装(Symfony 2.5.4和FOSElastica 3.0.4)与你的安装有很大不同。因此,使代码工作存在一些问题。我发布了我的解决方案,因为它可能对其他开发人员有用。
监听器不在FOS \ ElasticaBundle \ Doctrine \ ORM \中,而是在FOS \ ElasticaBundle \ Doctrine中。所以你必须使用那个。 此外,我不得不使用Doctrine \ Common \ EventArgs而不是Doctrine \ ORM \ Event \ LifecycleEventArgs,因为否则我自己的postUpdate方法与BaseListener中的方法不兼容。
在我的应用程序中,课程(研讨会)可以有很多会话,但在这个项目中,elastica将只使用这些会话。该应用程序需要了解与课程相关的课程的一些细节。所以,这是我的代码:
在config.yml中,我的elastica bundle配置如下所示:
fos_elastica:
clients:
default: { host: localhost, port: 9200 }
indexes:
courses:
index_name: courses
types:
session:
mappings:
id: ~
name: ~
course:
type: "nested"
properties:
id: ~
name: ~
再远一点,仍然在config.yml
services:
# some other services here
fos_elastica.listener.courses.course:
class: XXX\CourseBundle\EventListener\ElasticaCourseListener
arguments:
- @fos_elastica.object_persister.courses.course
- ['postPersist', 'postUpdate', 'preRemove']
- @fos_elastica.indexable
calls:
- [ setContainer, ['@service_container', @fos_elastica.object_persister.courses.session ] ]
tags:
- { name: 'doctrine.event_subscriber' }
我自己的监听器(XXX \ CourseBundle \ EventListener \ ElasticaCourseListener)看起来像这样:
<?php
namespace XXX\CourseBundle\EventListener;
use Doctrine\Common\EventArgs;
use FOS\ElasticaBundle\Doctrine\Listener as BaseListener;
use FOS\ElasticaBundle\Persister\ObjectPersister;
use Symfony\Component\DependencyInjection\ContainerInterface;
use XXX\CourseBundle\Entity\Course;
class ElasticaCourseListener extends BaseListener
{
private $container;
private $objectPersisterSession;
public function setContainer(ContainerInterface $container, ObjectPersister $objectPersisterSession)
{
$this->container = $container;
$this->objectPersisterSession = $objectPersisterSession;
}
public function postUpdate(EventArgs $args)
{
$entity = $args->getEntity();
if ($entity instanceof Course) {
$this->scheduledForUpdate[] = $entity;
foreach ($entity->getSessions() as $session) {
$this->objectPersisterSession->replaceOne($session);
}
}
}
}
现在,当我更新课程时,它将作为ElasticSearch中的嵌套对象进行更新; - )
答案 1 :(得分:4)
我想我已在此页面找到解决方案https://groups.google.com/forum/#!topic/elastica-php-client/WTONX-zBTI4 谢谢卡西亚诺
基本上你需要扩展FOS \ ElasticaBundle \ Doctrine \ ORM \ Listener,这样你就可以查找相关的实体,然后再更新它们的索引。
class CompanyListener extends BaseListener
{
/** @var \Symfony\Component\DependencyInjection\ContainerInterface */
private $container;
public function setContainer(\Symfony\Component\DependencyInjection\ContainerInterface $container) {
$this->container = $container;
}
protected function initialiseJob() {
$this->objectPersisterJob = $this->container->get('fos_elastica.object_persister.application.job');
$this->em = $this->container->get('doctrine')->getEntityManager(); //maybe move this to postUpdate function so it can be used for all
}
/**
* @param \Doctrine\ORM\Event\LifecycleEventArgs $eventArgs
*/
public function postUpdate(LifecycleEventArgs $eventArgs)
{
/** @var $entity Story */
$entity = $eventArgs->getEntity();
if ($entity instanceof $this->objectClass) {
if ($this->isObjectIndexable($entity)) {
$this->objectPersister->replaceOne($entity);
$this->initialiseJob();
foreach ($entity->getJobOpenings() as $job) {
$this->objectPersisterJob->replaceOne($job);
}
} else {
$this->scheduleForRemoval($entity, $eventArgs->getEntityManager());
$this->removeIfScheduled($entity);
}
}
}
public function preRemove(\Doctrine\Common\EventArgs $eventArgs)
{
$entity = $eventArgs->getEntity();
if ($entity instanceof $this->objectClass) {
$this->scheduleForDeletion($entity);
$this->initialiseJob();
foreach ($entity->getJobOpenings() as $job) {
$this->objectPersisterJob->replaceOne($job);
}
}
}
}
,您的服务定义如下
fos_elastica.listener.application.company:
class: 'xxx\RMSBundle\EventListener\CompanyListener'
arguments:
- '@fos_elastica.object_persister.application.company'
- 'xxx\RMSBundle\Entity\Company'
- ['postPersist', 'postUpdate', 'postRemove', 'preRemove']
- id
calls:
- [ setContainer, [ '@service_container' ] ]
tags:
- { name: 'doctrine.event_subscriber' }
然后将更新两者的索引: - )
答案 2 :(得分:4)
我使用的是FosElastica 3.1.0,我尝试过Julien Rm提供的解决方案但没有成功: - (
经过多天的研究,我终于找到了解决方案 here
$persister = $this->get('fos_elastica.object_persister.jaiuneidee.post');
$persister->insertOne($post);
希望这有帮助!
答案 3 :(得分:3)
通过所有评论和我的研究,我使用fosElastica为自动索引子对象创建了一个通用Gist:
https://gist.github.com/Nightbr/ddb586394d95877dde8ed7445c51d973
实际上,我覆盖了FOSElastica的默认侦听器,并添加了function updateRelations($entity)
。我们将搜索链接到$entity
的所有关系,如果ES中存在索引(ES类型存在),它将更新相关文档。
如果有人想看一下并做出任何改进,那就太棒了! ^^
提前致谢
答案 4 :(得分:2)
抱歉,我无法在您的回答中发表评论,但解决方案中缺少某些内容。你也必须覆盖preRemove。
public function preRemove(\Doctrine\Common\EventArgs $eventArgs)
{
$entity = $eventArgs->getEntity();
if ($entity instanceof $this->objectClass) {
$this->scheduleForDeletion($entity);
$this->initialiseJob();
foreach ($entity->getJobOpenings() as $job) {
$this->objectPersisterJob->replaceOne($job);
}
}
}
答案 5 :(得分:1)
使用FosElastica 3.1.0的BC Break#729,事情发生了变化,上面的代码无效:
BC BREAK: Removed Doctrine\Listener#getSubscribedEvents. The container configuration now configures tags with the methods to call to avoid loading this class on every request where doctrine is active. #729
对于那些试图使它与FOSElastica 3.1.X一起工作的人来说,这是我如何设法让一个嵌套的对象在持久化/更新/删除嵌套实体时被索引到他的父进入Elastic Search:
定义服务侦听器:
fos_elastica.listener.entity.nested:
class: XX\CoreBundle\EventListener\EventSubscriber\ElasticaNestedListener
arguments:
- @fos_elastica.object_persister.app.entityname
- @fos_elastica.indexable
- {"indexName" : "app", "typeName": "entityname"}
tags:
- { name: 'doctrine.event_subscriber' }
创建监听器:
<?php
class ElasticaNestedListener implements EventSubscriber
{ // some indentations missing!
public function getSubscribedEvents()
{
return array(
'postPersist',
'preRemove',
'postUpdate',
'preFlush',
'postFlush',
);
}
/**
* Object persister.
*
* @var ObjectPersisterInterface
*/
protected $objectPersister;
/**
* Configuration for the listener.
*
* @var array
*/
private $config;
/**
* Objects scheduled for insertion.
*
* @var array
*/
public $scheduledForInsertion = array();
/**
* Objects scheduled to be updated or removed.
*
* @var array
*/
public $scheduledForUpdate = array();
/**
* IDs of objects scheduled for removal.
*
* @var array
*/
public $scheduledForDeletion = array();
/**
* PropertyAccessor instance.
*
* @var PropertyAccessorInterface
*/
protected $propertyAccessor;
/**
* @var IndexableInterface
*/
private $indexable;
/**
* Constructor.
*
* @param ObjectPersisterInterface $objectPersister
* @param IndexableInterface $indexable
* @param array $config
* @param LoggerInterface $logger
*/
public function __construct(
ObjectPersisterInterface $objectPersister,
IndexableInterface $indexable,
array $config = array(),
LoggerInterface $logger = null
) {
$this->config = array_merge(array(
'identifier' => 'id',
), $config);
$this->indexable = $indexable;
$this->objectPersister = $objectPersister;
$this->propertyAccessor = PropertyAccess::createPropertyAccessor();
if ($logger && $this->objectPersister instanceof ObjectPersister) {
$this->objectPersister->setLogger($logger);
}
}
/**
* Looks for objects being updated that should be indexed or removed from the index.
*
* @param LifecycleEventArgs $eventArgs
*/
public function postUpdate(LifecycleEventArgs $eventArgs)
{
$entity = $eventArgs->getObject();
if ($entity instanceof EntityName) {
$question = $entity->getParent();
if ($this->objectPersister->handlesObject($question)) {
if ($this->isObjectIndexable($question)) {
$this->scheduledForUpdate[] = $question;
} else {
// Delete if no longer indexable
$this->scheduleForDeletion($question);
}
}
}
}
public function postPersist(LifecycleEventArgs $eventArgs)
{
$entity = $eventArgs->getObject();
if ($entity instanceof EntityName) {
$question = $entity->getParent();
if ($this->objectPersister->handlesObject($question)) {
if ($this->isObjectIndexable($question)) {
$this->scheduledForUpdate[] = $question;
} else {
// Delete if no longer indexable
$this->scheduleForDeletion($question);
}
}
}
}
/**
* Delete objects preRemove instead of postRemove so that we have access to the id. Because this is called
* preRemove, first check that the entity is managed by Doctrine.
*
* @param LifecycleEventArgs $eventArgs
*/
public function preRemove(LifecycleEventArgs $eventArgs)
{
$entity = $eventArgs->getObject();
if ($this->objectPersister->handlesObject($entity)) {
$this->scheduleForDeletion($entity);
}
}
/**
* Persist scheduled objects to ElasticSearch
* After persisting, clear the scheduled queue to prevent multiple data updates when using multiple flush calls.
*/
private function persistScheduled()
{
if (count($this->scheduledForInsertion)) {
$this->objectPersister->insertMany($this->scheduledForInsertion);
$this->scheduledForInsertion = array();
}
if (count($this->scheduledForUpdate)) {
$this->objectPersister->replaceMany($this->scheduledForUpdate);
$this->scheduledForUpdate = array();
}
if (count($this->scheduledForDeletion)) {
$this->objectPersister->deleteManyByIdentifiers($this->scheduledForDeletion);
$this->scheduledForDeletion = array();
}
}
/**
* Iterate through scheduled actions before flushing to emulate 2.x behavior.
* Note that the ElasticSearch index will fall out of sync with the source
* data in the event of a crash during flush.
*
* This method is only called in legacy configurations of the listener.
*
* @deprecated This method should only be called in applications that depend
* on the behaviour that entities are indexed regardless of if a
* flush is successful.
*/
public function preFlush()
{
$this->persistScheduled();
}
/**
* Iterating through scheduled actions *after* flushing ensures that the
* ElasticSearch index will be affected only if the query is successful.
*/
public function postFlush()
{
$this->persistScheduled();
}
/**
* Record the specified identifier to delete. Do not need to entire object.
*
* @param object $object
*/
private function scheduleForDeletion($object)
{
if ($identifierValue = $this->propertyAccessor->getValue($object, $this->config['identifier'])) {
$this->scheduledForDeletion[] = $identifierValue;
}
}
/**
* Checks if the object is indexable or not.
*
* @param object $object
*
* @return bool
*/
private function isObjectIndexable($object)
{
return $this->indexable->isObjectIndexable(
$this->config['indexName'],
$this->config['typeName'],
$object
);
}
}
EntityName可能是一个注释,而getParent()可能是拥有此评论的文章......
希望这有帮助!
答案 6 :(得分:1)
我正在使用Symphony 3和FOSElasticaBundle 3.2而且我做的事情有点不同。 在查看了其他答案中给出的代码之后,我已经决定不扩展默认侦听器。相反,我让它做它的事情,我只是添加了我自己的听众。
我有一些类别(1),可以有多个(多对多)主题(2),可以有多个(一对多) 帖子(3)。 帖子是在Elasticsearch中保存的实体,其中包含各自主题及其自己的类别的信息。
像这样:
fos_elastica:
#...
indexes:
my_index:
#...
types:
post: # (3)
mappings:
field_one: ~
# ... Other fields
subject: # (2)
type: "object"
properties:
subject_field_one: ~
# ... Other fields
categories: # (1)
type: "nested"
properties:
category_field_one: ~
# ... Other fields
服务定义(app / config / services.yml)
services:
# ...
app.update_elastica_post.listener:
class: AppBundle\EventListener\UpdateElasticaPostListener
arguments: ['@service_container']
tags:
- { name: doctrine.event_listener, event: postUpdate }
监听器AppBundle \ EventListener \ UpdateElasticaPostListener.php
namespace AppBundle\EventListener;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Symfony\Component\DependencyInjection\ContainerInterface;
use AppBundle\Entity\Category;
use AppBundle\Entity\Subject;
class UpdateElasticaPostListener
{
private $container;
private $objectPersisterPost;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
$this->objectPersisterPost = null;
}
/**
* @param \Doctrine\ORM\Event\LifecycleEventArgs $eventArgs
*/
public function postUpdate(LifecycleEventArgs $eventArgs)
{
$this->checkAndUpdate($eventArgs);
}
protected function checkAndUpdate(LifecycleEventArgs $eventArgs)
{
$entity = $eventArgs->getEntity();
if ($entity instanceof Category) {
foreach ($entity->getSubjects() as $subject) {
$this->updateSubjectPosts($subject);
}
} elseif ($entity instanceof Subject) {
$this->updateSubjectPosts($entity);
}
}
protected function updateSubjectPosts(Subject $subject)
{
$this->initPostPersister();
foreach ($subject->getPosts() as $post) {
$this->objectPersisterPost->replaceOne($post);
}
}
protected function initPostPersister()
{
if (null === $this->objectPersisterPost) {
// fos_elastica.object_persister.<index_name>.<type_name>
$this->objectPersisterPost = $this->container->get('fos_elastica.object_persister.my_index.post');
}
}
}
就是这样!我没有尝试删除事件,现在我想起来了,也许这个解决方案对它来说不是最好的......但也许它是......
非常感谢@Ben Stinton和@maercky。
我希望它有所帮助! (这是我在这里的第一个答案,所以我希望我没有搞砸了)