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