summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/test/lib/Elastica/Test/ScanAndScrollTest.php
blob: a64670bbdb32bb94d055a8171c19355e5d1d30e5 (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
<?php

namespace Elastica\Test;

use Elastica\Document;
use Elastica\Query;
use Elastica\ResultSet;
use Elastica\ScanAndScroll;
use Elastica\Search;
use Elastica\Test\Base as BaseTest;

class ScanAndScrollTest extends BaseTest {

    public function testConstruct() {
        $scanAndScroll = $this->_prepareScanAndScroll();

        $this->assertInstanceOf('Elastica\ScanAndScroll', $scanAndScroll);
    }

    public function testDefaultProperties() {
        $scanAndScroll = $this->_prepareScanAndScroll();

        $this->assertEquals('1m', $scanAndScroll->expiryTime);
        $this->assertEquals(1000, $scanAndScroll->sizePerShard);
    }

    public function testQuerySizeOverride() {
        $query = new Query();
        $query->setSize(100);

        $index = $this->_createIndex('test_1');
        $index->refresh();  // Waits for the index to be fully created.
        $type = $index->getType('scanAndScrollTest');

        $search = new Search($this->_getClient());
        $search->addIndex($index)->addType($type);
        $search->setQuery($query);

        $scanAndScroll = new ScanAndScroll($search);
        $scanAndScroll->sizePerShard = 10;
        $scanAndScroll->rewind();

        $this->assertEquals(10, $query->getParam('size'));
    }

    public function testSizePerShard() {
        $search = $this->_prepareSearch('test_2', 2, 20);

        $scanAndScroll = new ScanAndScroll($search);
        $scanAndScroll->sizePerShard = 5;
        $scanAndScroll->rewind();

        $this->assertEquals(10, $scanAndScroll->current()->count());
    }

    public function testScrollId() {
        $search = $this->_prepareSearch('test_3', 1, 2);

        $scanAndScroll = new ScanAndScroll($search);
        $scanAndScroll->sizePerShard = 1;

        $scanAndScroll->rewind();
        $this->assertEquals(
            $scanAndScroll->current()->getResponse()->getScrollId(),
            $scanAndScroll->key()
        );
    }

    public function testForeach() {
        $search = $this->_prepareSearch('test_4', 2, 11);

        $scanAndScroll = new ScanAndScroll($search);
        $scanAndScroll->sizePerShard = 5;

        // We expect 2 scrolls:
        // 1. with 10 hits,
        // 2. with 1 hit
        // Note: there is a 3. scroll with 0 hits

        $count = 0;
        foreach($scanAndScroll as $resultSet) {
            /** @var ResultSet $resultSet */
            $count++;

            switch(true) {
                case $count == 1: $this->assertEquals(10, $resultSet->count()); break;
                case $count == 2: $this->assertEquals(1, $resultSet->count()); break;
            }
        }

        $this->assertEquals(2, $count);
    }

    private function _prepareScanAndScroll() {
        return new ScanAndScroll(new Search($this->_getClient()));
    }

    private function _prepareSearch($indexName, $indexShards, $docs) {
        $index = $this->_createIndex($indexName, true, $indexShards);
        $type = $index->getType('scanAndScrollTest');

        $insert = array();
        for ($x = 1; $x <= $docs; $x++) {
            $insert[] = new Document($x, array('id' => $x, 'key' => 'value'));
        }

        $type->addDocuments($insert);
        $index->refresh();

        $search = new Search($this->_getClient());
        $search->addIndex($index)->addType($type);

        return $search;
    }
}