summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/lib/Elastica/ScanAndScroll.php
blob: c83054e8853f4b411c90a3b31b307397b3fdf378 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php

namespace Elastica;

/**
 * scan and scroll object
 *
 * @category Xodoa
 * @package Elastica
 * @author Manuel Andreo Garcia <andreo.garcia@gmail.com>
 * @link http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/scan-scroll.html
 */
class ScanAndScroll implements \Iterator {

    /**
     * time value parameter
     *
     * @link http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-scroll.html
     * @var string
     */
    public $expiryTime;

    /**
     * @var int
     */
    public $sizePerShard;

    /**
     * @var Search
     */
    protected $_search;

    /**
     * @var null|string
     */
    protected $_nextScrollId = null;

    /**
     * @var null|string
     */
    protected $_lastScrollId = null;

    /**
     * @var null|ResultSet
     */
    protected $_currentResultSet = null;

    /**
     * Constructs scroll iterator object
     *
     * @param Search $search
     * @param string $expiryTime
     * @param int $sizePerShard
     */
    public function __construct(Search $search, $expiryTime = '1m', $sizePerShard = 1000) {
        $this->_search = $search;
        $this->expiryTime = $expiryTime;
        $this->sizePerShard = $sizePerShard;
    }

    /**
     * Return the current result set
     *
     * @link http://php.net/manual/en/iterator.current.php
     * @return ResultSet
     */
    public function current() {
        return $this->_currentResultSet;
    }

    /**
     * Perform next scroll search
     *
     * @link http://php.net/manual/en/iterator.next.php
     * @return void
     */
    public function next() {
        $this->_scroll();
    }

    /**
     * Return the scroll id of current scroll request
     *
     * @link http://php.net/manual/en/iterator.key.php
     * @return string
     */
    public function key() {
        return $this->_lastScrollId;
    }

    /**
     * Returns true if current result set contains one hit
     *
     * @link http://php.net/manual/en/iterator.valid.php
     * @return boolean
     */
    public function valid() {
        return
            $this->_nextScrollId !== null
            && $this->_currentResultSet !== null
            && $this->_currentResultSet->count() > 0;
    }

    /**
     * Start the initial scan search
     * @link http://php.net/manual/en/iterator.rewind.php
     * @throws \Elastica\Exception\InvalidException
     * @return void
     */
    public function rewind() {
        $this->_search->getQuery()->setSize($this->sizePerShard);

        $this->_search->setOption(Search::OPTION_SEARCH_TYPE, Search::OPTION_SEARCH_TYPE_SCAN);
        $this->_search->setOption(Search::OPTION_SCROLL, $this->expiryTime);

        // initial scan request
        $this->_setScrollId($this->_search->search());

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

    /**
     * Perform next scroll search
     * @throws \Elastica\Exception\InvalidException
     * @return void
     */
    protected function _scroll() {
        $this->_search->setOption(Search::OPTION_SEARCH_TYPE, Search::OPTION_SEARCH_TYPE_SCROLL);
        $this->_search->setOption(Search::OPTION_SCROLL_ID, $this->_nextScrollId);

        $resultSet = $this->_search->search();
        $this->_currentResultSet = $resultSet;
        $this->_setScrollId($resultSet);
    }

    /**
     * Save last scroll id and extract the new one if possible
     * @param ResultSet $resultSet
     */
    protected function _setScrollId(ResultSet $resultSet) {
        $this->_lastScrollId = $this->_nextScrollId;

        $this->_nextScrollId = null;
        if($resultSet->getResponse()->isOk()) {
            $this->_nextScrollId = $resultSet->getResponse()->getScrollId();
        }
    }

}