1
|
|
|
<?php |
2
|
|
|
namespace tests; |
3
|
|
|
|
4
|
|
|
use Soupmix\ElasticSearch; |
5
|
|
|
use Elasticsearch\ClientBuilder; |
6
|
|
|
|
7
|
|
|
class ElasticsearchTest extends \PHPUnit_Framework_TestCase |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var \Soupmix\ElasticSearch $client |
11
|
|
|
*/ |
12
|
|
|
protected $client = null; |
13
|
|
|
|
14
|
|
|
protected function setUp() |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
$config =[ |
18
|
|
|
'db_name' => 'test', |
19
|
|
|
'hosts' => ['127.0.0.1:9200'], |
20
|
|
|
]; |
21
|
|
|
|
22
|
|
|
$client = ClientBuilder::create()->setHosts($config['hosts'])->build(); |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
$this->client = new ElasticSearch($config, $client); |
26
|
|
|
$this->client->drop('test'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testInsertGetDocument() |
30
|
|
|
{ |
31
|
|
|
$docId = $this->client->insert('test', ['id' => 1, 'title' => 'test']); |
32
|
|
|
$document = $this->client->get('test', $docId); |
33
|
|
|
$this->assertArrayHasKey('title', $document); |
34
|
|
|
$this->assertArrayHasKey('id', $document); |
35
|
|
|
$result = $this->client->delete('test', ['_id' => $docId]); |
36
|
|
|
$this->assertTrue($result == 1); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testFindDocuments() |
40
|
|
|
{ |
41
|
|
|
$docIds = []; |
42
|
|
|
$data = $this->bulkData(); |
43
|
|
|
foreach ($data as $d) { |
44
|
|
|
$docId = $this->client->insert('test', $d); |
45
|
|
|
$this->assertNotNull($docId, 'Document could not inserted to ES while testing find'); |
46
|
|
|
if ($docId) { |
47
|
|
|
$docIds[] = $docId; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
sleep(2); // waiting to be able to be searchable on elasticsearch. |
51
|
|
|
$results = $this->client->find('test', ['title' => 'test1']); |
52
|
|
|
$this->assertArrayHasKey('total', $results); |
53
|
|
|
$this->assertArrayHasKey('data', $results); |
54
|
|
|
$this->assertGreaterThanOrEqual(1, $results['total'], 'Total not equal for field term filtering'); |
55
|
|
|
|
56
|
|
|
$results = $this->client->find('test', ['count.max__gte' => 6]); |
57
|
|
|
$this->assertGreaterThanOrEqual(2, $results['total'], |
58
|
|
|
'Total not greater than or equal to 2 on count_gte filtering'); |
59
|
|
|
|
60
|
|
|
$results = $this->client->find('test', ['count.max__gte' => 6, 'count.min__gte' => 2]); |
61
|
|
|
$this->assertGreaterThanOrEqual(1, $results['total'], |
62
|
|
|
'Total not greater than or equal to 2 on count.max__gte and count.max__gte filtering'); |
63
|
|
|
|
64
|
|
|
$results = $this->client->find('test', [[['count.max__gte' => 6], ['count.min__gte' => 2]], 'title' => 'test4']); |
65
|
|
|
$this->assertGreaterThanOrEqual(1, $results['total'], |
66
|
|
|
'Total not greater than or equal to 2 on count.max__gte and count.max__gte filtering'); |
67
|
|
|
|
68
|
|
|
foreach ($docIds as $docId) { |
69
|
|
|
$result = $this->client->delete('test', ['_id' => $docId]); |
70
|
|
|
$this->assertTrue($result == 1); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testInsertUpdateGetDocument() |
75
|
|
|
{ |
76
|
|
|
$docId = $this->client->insert('test', ['id' => 1, 'title' => 'test']); |
77
|
|
|
sleep(2); // waiting to be able to be searchable on elasticsearch. |
78
|
|
|
$modifiedCount = $this->client->update('test', ['title' => 'test'], ['title' => 'test2']); |
79
|
|
|
$this->assertTrue($modifiedCount >= 1); |
80
|
|
|
sleep(2); // waiting to be able to be searchable on elasticsearch. |
81
|
|
|
$document = $this->client->get('test', $docId); |
82
|
|
|
$this->assertArrayHasKey('title', $document); |
83
|
|
|
$this->assertEquals('test2', $document['title']); |
84
|
|
|
|
85
|
|
|
$result = $this->client->delete('test', ['_id' => $docId]); |
86
|
|
|
$this->assertTrue($result == 1); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testInsertUpdateMultipleDocument() |
90
|
|
|
{ |
91
|
|
|
$docIds = array(); |
92
|
|
|
$docIds[] = $this->client->insert('test', ['id' => 1, 'title' => 'test']); |
93
|
|
|
$docIds[] = $this->client->insert('test', ['id' => 2, 'title' => 'test']); |
94
|
|
|
sleep(2); // waiting to be able to be searchable on elasticsearch. |
95
|
|
|
$modifiedCount = $this->client->update('test', ['title' => 'test'], ['title' => 'test2']); |
96
|
|
|
$this->assertTrue($modifiedCount >= 2); |
97
|
|
|
sleep(1); // waiting to be able to be searchable on elasticsearch. |
98
|
|
|
$documents = $this->client->get('test', $docIds); |
99
|
|
|
foreach ($documents as $document) { |
100
|
|
|
$this->assertArrayHasKey('title', $document); |
101
|
|
|
$this->assertEquals('test2', $document['title']); |
102
|
|
|
} |
103
|
|
|
$result = $this->client->delete('test', ['title' => 'test2']); |
104
|
|
|
$this->assertTrue($result == 1); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function tearDown() |
108
|
|
|
{ |
109
|
|
|
$this->client->drop('test'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
private function bulkData() |
113
|
|
|
{ |
114
|
|
|
return [ |
115
|
|
|
['id' => 1, 'date' => '2015-04-10 00:00:00', 'title' => 'test1', 'balance' => 100.0, 'count' => ['min' => 1, 'max' => 1]], |
116
|
|
|
['id' => 2, 'date' => '2015-04-11 00:00:00', 'title' => 'test2', 'balance' => 120.0, 'count' => ['min' => 1, 'max' => 1]], |
117
|
|
|
['id' => 3, 'date' => '2015-04-12 00:00:00', 'title' => 'test3', 'balance' => 101.5, 'count' => ['min' => 1, 'max' => 7]], |
118
|
|
|
['id' => 4, 'date' => '2015-04-12 00:00:00', 'title' => 'test4', 'balance' => 200.5, 'count' => ['min' => 3, 'max' => 6]], |
119
|
|
|
['id' => 5, 'date' => '2015-04-13 00:00:00', 'title' => 'test5', 'balance' => 150.0, 'count' => ['min' => 1, 'max' => 5]], |
120
|
|
|
['id' => 6, 'date' => '2015-04-14 00:00:00', 'title' => 'test6', 'balance' => 400.8, 'count' => ['min' => 1, 'max' => 4]], |
121
|
|
|
['id' => 7, 'date' => '2015-04-15 00:00:00', 'title' => 'test7', 'balance' => 240.0, 'count' => ['min' => 1, 'max' => 4]], |
122
|
|
|
['id' => 8, 'date' => '2015-04-20 00:00:00', 'title' => 'test8', 'balance' => 760.0, 'count' => ['min' => 1, 'max' => 5]], |
123
|
|
|
['id' => 9, 'date' => '2015-04-20 00:00:00', 'title' => 'test9', 'balance' => 50.0, 'count' => ['min' => 1, 'max' => 2]], |
124
|
|
|
['id' => 10, 'date' => '2015-04-21 00:00:00', 'title' => 'test0', 'balance' => 55.5, 'count' => ['min' => 1, 'max' => 2]], |
125
|
|
|
]; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|