summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/test/lib/Elastica/Test/PercolatorTest.php
blob: 2a5a88c3bbd291422576738a6d853b52fe1a064b (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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
<?php
namespace Elastica\Test;

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

class PercolatorTest extends BaseTest
{
    /**
     * @group functional
     */
    public function testConstruct()
    {
        $index = $this->_createIndex();
        $percolatorName = $index->getName();

        $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();
    }

    /**
     * @group functional
     */
    public function testMatchDoc()
    {
        $index = $this->_createIndex();

        $percolator = new Percolator($index);

        $percolatorName = $index->getName();

        $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.
     *
     * @group functional
     */
    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.
     *
     * @group functional
     */
    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, array('field1' => array('tag1', 'tag2')));

        return $percolator;
    }

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

        return $type;
    }

    /**
     * @group functional
     */
    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();
    }

    /**
     * @group functional
     */
    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();
    }

    /**
     * @group functional
     */
    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();
    }

    /**
     * @group functional
     */
    public function testPercolateWithAdditionalRequestBodyOptions()
    {
        $index = $this->_createIndex();
        $percolator = new Percolator($index);

        $query = new Term(array('name' => 'foo'));
        $response = $percolator->registerQuery('percotest', $query, array('field1' => array('tag1', 'tag2')));

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

        $query = new Term(array('name' => 'foo'));
        $response = $percolator->registerQuery('percotest1', $query, array('field1' => array('tag2')));

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

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

        $index->refresh();

        $options = array(
            'track_scores' => true,
            'sort' => array('_score' => 'desc'),
            'size' => 1,
        );

        $matches = $percolator->matchDoc($doc1, new Term(array('field1' => 'tag2')), 'type', $options);

        $this->assertCount(1, $matches);
        $this->assertEquals('percotest1', $matches[0]['_id']);
        $this->assertArrayHasKey('_score', $matches[0]);
    }

    /**
     * @group functional
     */
    public function testPercolateExistingDocWithAdditionalRequestBodyOptions()
    {
        $percolatorName = 'existingDoc';
        $percolator = $this->_getDefaultPercolator($percolatorName);

        $query = new Term(array('name' => 'foobar'));
        $percolator->registerQuery($percolatorName.'1', $query, array('field1' => array('tag2')));

        $index = $percolator->getIndex();
        $type = $this->_addDefaultDocuments($index);

        $options = array(
            'track_scores' => true,
            'sort' => array('_score' => 'desc'),
            'size' => 1,
        );

        $matches = $percolator->matchExistingDoc(1, $type->getName(), new Term(array('field1' => 'tag2')), $options);

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

    protected function _createIndex($name = null, $delete = true, $shards = 1)
    {
        $index = parent::_createIndex($name, $delete, $shards);
        $type = $index->getType('.percolator');

        $mapping = new Type\Mapping($type,
            array(
                'name' => array('type' => 'string'),
                'field1' => array('type' => 'string'),
            )
        );
        $mapping->disableSource();

        $type->setMapping($mapping);

        return $index;
    }
}