FOSElasticaBundle/Persister/ObjectPersister.php
Richard Fullmer 1fc94b2213 Ignore failed deletions in ObjectPersister
This probably isn't the best way to solve my problem,
but the issue is this.

Step 1:  Create a new doctrine entity for which it's `is_indexable_callback`
returns false.  When doctrine flushes this entity to the database,
elastia will not index it with elastic search.  (Correct)

Step 2:  Update your doctrine entity and change some fields so
that `is_indexable_callback` _still_ returns false.  Persist and flush
to the database.

At this point, the postUpdate listener on ElastiaBundle is called
and since the `is_indexable_callback` returns false, it believes
it needs to remove it from the elastic search index and queues it
for deletion.  The deletion of course fails because it was never there
in the first place.

This solution simply ignores failures from deletions in the index.

Perhaps a better solution would be to have a smarter listener
that could determine if the entity was previously present in the
elastic search index or not, but that would require significant
refactoring.

Addresses issues discuseed in #284

Credit to @bbeaulant for simple solution.  Opening a PR to
discuss more generally.
2013-06-04 11:45:45 -07:00

112 lines
2.8 KiB
PHP

<?php
namespace FOS\ElasticaBundle\Persister;
use Elastica\Exception\NotFoundException;
use FOS\ElasticaBundle\Transformer\ModelToElasticaTransformerInterface;
use Elastica\Type;
use Elastica\Document;
/**
* Inserts, replaces and deletes single documents in an elastica type
* Accepts domain model objects and converts them to elastica documents
*
* @author Thibault Duplessis <thibault.duplessis@gmail.com>
*/
class ObjectPersister implements ObjectPersisterInterface
{
protected $type;
protected $transformer;
protected $objectClass;
protected $fields;
public function __construct(Type $type, ModelToElasticaTransformerInterface $transformer, $objectClass, array $fields)
{
$this->type = $type;
$this->transformer = $transformer;
$this->objectClass = $objectClass;
$this->fields = $fields;
}
/**
* Insert one object into the type
* The object will be transformed to an elastica document
*
* @param object $object
*/
public function insertOne($object)
{
$document = $this->transformToElasticaDocument($object);
$this->type->addDocument($document);
}
/**
* Replaces one object in the type
*
* @param object $object
* @return null
**/
public function replaceOne($object)
{
$document = $this->transformToElasticaDocument($object);
try {
$this->type->deleteById($document->getId());
} catch (NotFoundException $e) {}
$this->type->addDocument($document);
}
/**
* Deletes one object in the type
*
* @param object $object
* @return null
**/
public function deleteOne($object)
{
$document = $this->transformToElasticaDocument($object);
try {
$this->type->deleteById($document->getId());
} catch (NotFoundException $e) {}
}
/**
* Deletes one object in the type by id
*
* @param mixed $id
*
* @return null
**/
public function deleteById($id)
{
try {
$this->type->deleteById($id);
} catch (NotFoundException $e) {}
}
/**
* Inserts an array of objects in the type
*
* @param array $objects array of domain model objects
**/
public function insertMany(array $objects)
{
$documents = array();
foreach ($objects as $object) {
$documents[] = $this->transformToElasticaDocument($object);
}
$this->type->addDocuments($documents);
}
/**
* Transforms an object to an elastica document
*
* @param object $object
* @return Document the elastica document
*/
public function transformToElasticaDocument($object)
{
return $this->transformer->transform($object, $this->fields);
}
}