From 20810fa4150e1c739ad2c848d81af61b738e76e0 Mon Sep 17 00:00:00 2001 From: baggachipz Date: Mon, 31 Mar 2014 15:16:32 -0400 Subject: [PATCH] Upsert-type functionality for existing ORM Entities This is an attempt to fix the issue: https://github.com/FriendsOfSymfony/FOSElasticaBundle/issues/526 It will cause a significant slowdown in a large batch, but it appears to be the only way to prevent an exception from bubbling up during a normal use case. --- Persister/ObjectPersister.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Persister/ObjectPersister.php b/Persister/ObjectPersister.php index 3592a78..aa20561 100644 --- a/Persister/ObjectPersister.php +++ b/Persister/ObjectPersister.php @@ -107,7 +107,15 @@ class ObjectPersister implements ObjectPersisterInterface { $documents = array(); foreach ($objects as $object) { - $documents[] = $this->transformToElasticaDocument($object); + $document = $this->transformToElasticaDocument($object); + + try { + $this->type->getDocument($document->getId()); + } catch (NotFoundException $e) { + $this->type->addDocument($document); + } + + $documents[] = $document; } $this->type->updateDocuments($documents); } @@ -136,4 +144,4 @@ class ObjectPersister implements ObjectPersisterInterface { return $this->transformer->transform($object, $this->fields); } -} \ No newline at end of file +}