summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/lib/Elastica/Bulk/Action/UpdateDocument.php
blob: 4c1dbaa449025065069397901cf4730731173281 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php

namespace Elastica\Bulk\Action;

use Elastica\Document;
use Elastica\Script;

/**
 * @package Elastica\Bulk\Action
 * @link http://www.elasticsearch.org/guide/reference/api/bulk/
 */
class UpdateDocument extends IndexDocument
{
    /**
     * @var string
     */
    protected $_opType = self::OP_TYPE_UPDATE;

    /**
     * Set the document for this bulk update action.
     * @param \Elastica\Document $document
     * @return \Elastica\Bulk\Action\UpdateDocument
     */
    public function setDocument(Document $document)
    {
        parent::setDocument($document);

        $source = array('doc' => $document->getData());

        if ($document->getDocAsUpsert()) {
        	$source['doc_as_upsert'] = true;
        	
        }else if ($document->hasUpsert()) {
        	
            $upsert = $document->getUpsert()->getData();

            if (!empty($upsert)) {
                $source['upsert'] = $upsert;
            }
        }

        $this->setSource($source);

        return $this;
    }

    /**
     * @param \Elastica\Script $script
     * @return \Elastica\Bulk\Action\AbstractDocument
     */
    public function setScript(Script $script)
    {
        parent::setScript($script);

        $source = $script->toArray();

        if ($script->hasUpsert()) {
            $upsert = $script->getUpsert()->getData();

            if (!empty($upsert)) {
                $source['upsert'] = $upsert;
            }
        }

        $this->setSource($source);

        return $this;
    }
}