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

namespace Elastica\Test\Filter;

use Elastica\Document;
use Elastica\Filter\BoolNot;
use Elastica\Filter\Indices;
use Elastica\Filter\Term;
use Elastica\Index;
use Elastica\Query;
use Elastica\Test\Base as BaseTest;

class IndicesTest extends BaseTest
{
    /**
     * @var Index
     */
    protected $_index1;

    /**
     * @var Index
     */
    protected $_index2;

    protected function setUp()
    {
        parent::setUp();
        $this->_index1 = $this->_createIndex('indices_filter_1');
        $this->_index2 = $this->_createIndex('indices_filter_2');
        $this->_index1->addAlias("indices_filter");
        $this->_index2->addAlias("indices_filter");
        $docs = array(
            new Document("1", array("color" => "blue")),
            new Document("2", array("color" => "green")),
            new Document("3", array("color" => "blue")),
            new Document("4", array("color" => "yellow")),
        );
        $this->_index1->getType("test")->addDocuments($docs);
        $this->_index2->getType("test")->addDocuments($docs);
        $this->_index1->refresh();
        $this->_index2->refresh();
    }

    protected function tearDown()
    {
        $this->_index1->delete();
        $this->_index2->delete();
        parent::tearDown();
    }

    public function testToArray()
    {
        $expected = array(
            "indices" => array(
                "indices" => array("index1", "index2"),
                "filter" => array(
                    "term" => array("tag" => "wow")
                ),
                "no_match_filter" => array(
                    "term" => array("tag" => "such filter")
                )
            )
        );
        $filter = new Indices(new Term(array("tag" => "wow")), array("index1", "index2"));
        $filter->setNoMatchFilter(new Term(array("tag" => "such filter")));
        $this->assertEquals($expected, $filter->toArray());
    }

    public function testIndicesFilter()
    {
        $filter = new Indices(new BoolNot(new Term(array("color" => "blue"))), array($this->_index1->getName()));
        $filter->setNoMatchFilter(new BoolNot(new Term(array("color" => "yellow"))));
        $query = new Query();
        $query->setFilter($filter);

        // search over the alias
        $index = $this->_getClient()->getIndex("indices_filter");
        $results = $index->search($query);

        // ensure that the proper docs have been filtered out for each index
        $this->assertEquals(5, $results->count());
        foreach ($results->getResults() as $result) {
            $data = $result->getData();
            $color = $data["color"];
            if ($result->getIndex() == $this->_index1->getName()) {
                $this->assertNotEquals("blue", $color);
            } else {
                $this->assertNotEquals("yellow", $color);
            }
        }
    }
}