summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/lib/Elastica/ScanAndScroll.php
blob: 6713856efdb18634e44bf1699b96e8c5fb0d8c7b (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
70
71
72
73
74
75
76
77
78
79
80
<?php
namespace Elastica;

/**
 * Scan and Scroll Iterator.
 *
 * @author Manuel Andreo Garcia <andreo.garcia@gmail.com>
 *
 * @link http://www.elastic.co/guide/en/elasticsearch/guide/current/scan-scroll.html
 */
class ScanAndScroll extends Scroll
{
    /**
     * @var int
     */
    public $sizePerShard;

    /**
     * Constructor.
     *
     * @param Search $search
     * @param string $expiryTime
     * @param int    $sizePerShard
     */
    public function __construct(Search $search, $expiryTime = '1m', $sizePerShard = 1000)
    {
        $this->sizePerShard = $sizePerShard;

        parent::__construct($search, $expiryTime);
    }

    /**
     * Initial scan search.
     *
     * @link http://php.net/manual/en/iterator.rewind.php
     */
    public function rewind()
    {
        // reset state
        $this->_nextScrollId = null;
        $this->_options = array(null, null, null, null);

        $this->_saveOptions();

        // initial scan request
        $this->_search->getQuery()->setSize($this->sizePerShard);
        $this->_search->setOption(Search::OPTION_SCROLL, $this->expiryTime);
        $this->_search->setOption(Search::OPTION_SCROLL_ID, null);
        $this->_search->setOption(Search::OPTION_SEARCH_TYPE, Search::OPTION_SEARCH_TYPE_SCAN);
        $this->_setScrollId($this->_search->search());

        $this->_revertOptions();

        // first scroll request
        $this->next();
    }

    /**
     * Save all search options manipulated by Scroll.
     */
    protected function _saveOptions()
    {
        $query = $this->_search->getQuery();
        if ($query->hasParam('size')) {
            $this->_options[3] = $query->getParam('size');
        }

        parent::_saveOptions();
    }

    /**
     * Revert search options to previously saved state.
     */
    protected function _revertOptions()
    {
        $this->_search->getQuery()->setSize($this->_options[3]);

        parent::_revertOptions();
    }
}