summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/test/lib/Elastica/Test/PercolatorTest.php
blob: f4cfae0c065bb46185562d1d69a0656d54588895 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php

namespace Elastica\Test;

use Elastica\Client;
use Elastica\Document;
use Elastica\Index;
use Elastica\Percolator;
use Elastica\Query\Term;
use Elastica\Query;
use Elastica\Test\Base as BaseTest;

class PercolatorTest extends BaseTest
{
    public function testConstruct()
    {
        $percolatorName = 'percotest';

        $index = $this->_createIndex($percolatorName);
        $percolator = new Percolator($index);

        $query = new Term(array('field1' => 'value1'));
        $response = $percolator->registerQuery($percolatorName, $query);

        $data = $response->getData();

        $expectedArray = array(
            '_type' => '.percolator',
            '_index' => $index->getName(),
            '_id' => $percolatorName,
            '_version' => 1,
            'created' => 1
        );

        $this->assertEquals($expectedArray, $data);

        $index->delete();
    }

    public function testMatchDoc()
    {
        $index = $this->_createIndex();
        $percolator = new Percolator($index);

        $percolatorName = 'percotest';

        $query = new Term(array('name' => 'ruflin'));
        $response = $percolator->registerQuery($percolatorName, $query);

        $this->assertTrue($response->isOk());
        $this->assertFalse($response->hasError());

        $doc1 = new Document();
        $doc1->set('name', 'ruflin');

        $doc2 = new Document();
        $doc2->set('name', 'nicolas');

        $index->refresh();

        $matches1 = $percolator->matchDoc($doc1);

        $this->assertCount(1, $matches1);
        $firstPercolatorFound = false;
        foreach ($matches1 as $match) {
            if ($match['_id'] == $percolatorName) {
                $firstPercolatorFound = true;
            }
        }
        $this->assertTrue($firstPercolatorFound);

        $matches2 = $percolator->matchDoc($doc2);
        $this->assertEmpty($matches2);

        $index->delete();
    }

    /**
     * Test case for using filtered percolator queries based on the Elasticsearch documentation examples.
     */
    public function testFilteredMatchDoc()
    {
        // step one: register create index and setup the percolator query from the ES documentation.
        $index = $this->_createIndex();
        $percolator = new Percolator($index);
        $baseQuery = new Term(array('field1' => 'value1'));
        $fields = array('color' => 'blue');

        $response = $percolator->registerQuery('kuku', $baseQuery, $fields);

        $this->assertTrue($response->isOk());
        $this->assertFalse($response->hasError());

        // refreshing is required in order to ensure the query is really ready for execution.
        $index->refresh();

        // step two: match a document which should match the kuku query when filtered on the blue color
        $doc = new Document();
        $doc->set('field1', 'value1');

        $matches = $percolator->matchDoc($doc, new Term(array('color' => 'blue')));
        $this->assertCount(1, $matches, 'No or too much registered query matched.');
        $this->assertEquals('kuku', $matches[0]['_id'], 'A wrong registered query has matched.');

        // step three: validate that using a different color, no registered query matches.
        $matches = $percolator->matchDoc($doc, new Term(array('color' => 'green')));
        $this->assertCount(0, $matches, 'A registered query matched, although nothing should match at all.');

        $index->delete();
    }

    /**
     * Test case for using filtered percolator queries based on the Elasticsearch documentation examples.
     */
    public function testRegisterAndUnregisterPercolator()
    {
        // step one: register create index and setup the percolator query from the ES documentation.
        $index = $this->_createIndex();
        $percolator = new Percolator($index);
        $baseQuery = new Term(array('field1' => 'value1'));
        $fields = array('color' => 'blue');

        $response = $percolator->registerQuery('kuku', $baseQuery, $fields);

        $this->assertTrue($response->isOk());
        $this->assertFalse($response->hasError());

        // refreshing is required in order to ensure the query is really ready for execution.
        $index->refresh();

        // step two: match a document which should match the kuku query when filtered on the blue color
        $doc = new Document();
        $doc->set('field1', 'value1');

        $matches = $percolator->matchDoc($doc, new Term(array('color' => 'blue')));
        $this->assertCount(1, $matches, 'No or too much registered query matched.');
        $this->assertEquals('kuku', $matches[0]['_id'], 'A wrong registered query has matched.');

        // step three: validate that using a different color, no registered query matches.
        $matches = $percolator->matchDoc($doc, new Term(array('color' => 'green')));
        $this->assertCount(0, $matches, 'A registered query matched, although nothing should match at all.');


        // unregister percolator query
        $response = $percolator->unregisterQuery('kuku');

        $this->assertTrue($response->isOk());
        $this->assertFalse($response->hasError());

        // refreshing is required in order to ensure the query is really ready for execution.
        $index->refresh();

        $matches = $percolator->matchDoc($doc, new Term(array('color' => 'blue')));
        $this->assertCount(0, $matches, 'Percolator query did not get deleted.');

        $index->delete();
    }

    protected function _getDefaultPercolator($percolatorName = 'existingDoc')
    {
        $index = $this->_createIndex();
        $percolator = new Percolator($index);

        $query = new Term(array('name' => 'foobar'));
        $percolator->registerQuery($percolatorName, $query);
        return $percolator;
    }

    protected function _addDefaultDocuments($index, $type='testing')
    {
        $type = $index->getType('testing');
        $doc1 = new Document(1, array('name' => 'foobar'));
        $doc2 = new Document(2, array('name' => 'barbaz'));
        $type->addDocument($doc1);
        $type->addDocument($doc2);
        $index->refresh();
        return $type;
    }

    public function testPercolateExistingDocWithoutAnyParameter()
    {
        $percolator = $this->_getDefaultPercolator();
        $index      = $percolator->getIndex();
        $type       = $this->_addDefaultDocuments($index);

        $matches = $percolator->matchExistingDoc(1, $type->getName());

        $this->assertCount(1, $matches);
        $this->assertEquals('existingDoc', $matches[0]['_id']);
        $index->delete();
    }

    public function testPercolateExistingDocWithPercolateFormatIds()
    {
        $percolator = $this->_getDefaultPercolator();
        $index      = $percolator->getIndex();
        $type       = $this->_addDefaultDocuments($index);

        $parameter = array('percolate_format' => 'ids');
        $matches   = $percolator->matchExistingDoc(1, $type->getName(), null, $parameter);

        $this->assertCount(1, $matches);
        $this->assertEquals('existingDoc', $matches[0]);
        $index->delete();
    }

    public function testPercolateExistingDocWithIdThatShouldBeUrlEncoded()
    {
        $percolator = $this->_getDefaultPercolator();
        $index      = $percolator->getIndex();
        $type       = $this->_addDefaultDocuments($index);

        // id with whitespace, should be urlencoded
        $id = "foo bar 1";

        $type->addDocument(new Document($id, array('name' => 'foobar')));
        $index->refresh();

        $matches = $percolator->matchExistingDoc($id, $type->getName());

        $this->assertCount(1, $matches);
        $index->delete();
    }
}