-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapisearch-client-test.php
106 lines (89 loc) · 2.41 KB
/
apisearch-client-test.php
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
<?php
/*
* This file is part of the Apisearch Server
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Feel free to edit as you please, and have fun.
*
* @author Marc Morera <yuhu@mmoreram.com>
*/
require __DIR__.'/apisearch-client.php';
/**
* Asserts both values are equal.
*
* @param $expected
* @param $value
*
* @throws Exception
*/
function assertEquals($expected, $value)
{
if ($expected !== $value) {
throw new Exception(sprintf('Expected to get %d. Had %d',
$expected,
$value
));
}
echo '.';
}
$client = new ApisearchClient(
'http://127.0.0.1:8100',
'v1'
);
$client->setCredentials(
'app1',
'index1',
'0e4d75ba-c640-44c1-a745-06ee51db4e93'
);
$client->resetIndex();
$result = $client->query(['q' => 'nobita']);
assertEquals(0, $result['total_hits']);
assertEquals(0, $result['total_items']);
$client->putItem([
'uuid' => ['id' => '1', 'type' => 'element'],
'metadata' => ['m' => 'm1'],
'indexed_metadata' => ['im' => 'im1'],
'searchable_metadata' => ['s' => 'nobita'],
]);
$client->flush();
sleep(1);
$result = $client->query([]);
assertEquals(1, $result['total_hits']);
assertEquals(1, $result['total_items']);
$result = $client->query(['q' => 'nobita']);
assertEquals(1, $result['total_hits']);
assertEquals(1, $result['total_items']);
$client->putItems([[
'uuid' => ['id' => '2', 'type' => 'element'],
'metadata' => ['m' => 'm1'],
'indexed_metadata' => ['im' => 'im1'],
'searchable_metadata' => ['s' => 'nobita'],
], [
'uuid' => ['id' => '3', 'type' => 'element'],
'metadata' => ['m' => 'm2'],
'indexed_metadata' => ['im' => 'im2'],
'searchable_metadata' => ['s' => 'suneo'],
]]);
$client->flush();
sleep(1);
$result = $client->query(['q' => 'nobita']);
assertEquals(2, $result['total_hits']);
assertEquals(3, $result['total_items']);
$client->deleteItem(['id' => '2', 'type' => 'element']);
$client->flush();
sleep(1);
$result = $client->query(['q' => 'nobita']);
assertEquals(1, $result['total_hits']);
assertEquals(2, $result['total_items']);
$client->deleteItems([
['id' => '3', 'type' => 'element'],
['id' => '2', 'type' => 'element'],
]);
$client->flush();
sleep(1);
$result = $client->query(['q' => 'nobita']);
assertEquals(1, $result['total_hits']);
assertEquals(1, $result['total_items']);
echo PHP_EOL;