UpdateDocument.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace Elastica\Bulk\Action;
  3. use Elastica\Document;
  4. use Elastica\Script\AbstractScript;
  5. class UpdateDocument extends IndexDocument
  6. {
  7. /**
  8. * @var string
  9. */
  10. protected $_opType = self::OP_TYPE_UPDATE;
  11. /**
  12. * Set the document for this bulk update action.
  13. *
  14. * @param \Elastica\Document $document
  15. *
  16. * @return $this
  17. */
  18. public function setDocument(Document $document)
  19. {
  20. parent::setDocument($document);
  21. $source = ['doc' => $document->getData()];
  22. if ($document->getDocAsUpsert()) {
  23. $source['doc_as_upsert'] = true;
  24. } elseif ($document->hasUpsert()) {
  25. $upsert = $document->getUpsert()->getData();
  26. if (!empty($upsert)) {
  27. $source['upsert'] = $upsert;
  28. }
  29. }
  30. $this->setSource($source);
  31. return $this;
  32. }
  33. /**
  34. * @param \Elastica\Script\AbstractScript $script
  35. *
  36. * @return $this
  37. */
  38. public function setScript(AbstractScript $script)
  39. {
  40. parent::setScript($script);
  41. // FIXME: can we throw away toArray cast?
  42. $source = $script->toArray();
  43. if ($script->hasUpsert()) {
  44. $upsert = $script->getUpsert()->getData();
  45. if (!empty($upsert)) {
  46. $source['upsert'] = $upsert;
  47. }
  48. }
  49. $this->setSource($source);
  50. return $this;
  51. }
  52. }