-
Notifications
You must be signed in to change notification settings - Fork 196
/
AbstractElasticsearchTestCase.php
164 lines (144 loc) · 3.85 KB
/
AbstractElasticsearchTestCase.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
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
<?php
/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ONGR\ElasticsearchDSL\Tests\Functional;
use Elasticsearch\Client;
use Elasticsearch\ClientBuilder;
use ONGR\ElasticsearchDSL\Search;
use PHPUnit\Framework\TestCase;
abstract class AbstractElasticsearchTestCase extends TestCase
{
/**
* Test index name in the elasticsearch.
*/
const INDEX_NAME = 'elasticsaerch-dsl-test';
/**
* @var Client
*/
private $client;
/**
* {@inheritdoc}
*/
protected function setUp(): void
{
parent::setUp();
$this->client = ClientBuilder::create()->build();
$this->deleteIndex();
$this->client->indices()->create(
array_filter(
[
'index' => self::INDEX_NAME,
'mapping' => $this->getMapping()
]
)
);
$bulkBody = [];
foreach ($this->getDataArray() as $type => $documents) {
foreach ($documents as $id => $document) {
$bulkBody[] = [
'index' => [
'_index' => self::INDEX_NAME,
'_type' => $type,
'_id' => $id,
]
];
$bulkBody[] = $document;
}
}
$this->client->bulk(
[
'body' => $bulkBody
]
);
$this->client->indices()->refresh();
}
/**
* Defines index mapping for test index.
* Override this function in your test case and return array with mapping body.
* More info check here: https://goo.gl/zWBree
*
* @return array Mapping body
*/
protected function getMapping()
{
return [];
}
/**
* Can be overwritten in child class to populate elasticsearch index with the data.
*
* Example:
* [
* 'type_name' => [
* 'custom_id' => [
* 'title' => 'foo',
* ],
* 3 => [
* '_id' => 2,
* 'title' => 'bar',
* ]
* ]
* ]
* Document _id can be set as it's id.
*
* @return array
*/
protected function getDataArray()
{
return [];
}
/**
* {@inheritdoc}
*/
protected function tearDown(): void
{
parent::tearDown();
$this->deleteIndex();
}
/**
* Execute search to the elasticsearch and handle results.
*
* @param Search $search Search object.
* @param null $type Types to search. Can be several types split by comma.
* @param bool $returnRaw Return raw response from the client.
* @return array
*/
protected function executeSearch(Search $search, $type = null, $returnRaw = false)
{
$response = $this->client->search(
array_filter([
'index' => self::INDEX_NAME,
'type' => $type,
'body' => $search->toArray(),
])
);
if ($returnRaw) {
return $response;
}
$documents = [];
try {
foreach ($response['hits']['hits'] as $document) {
$documents[$document['_id']] = $document['_source'];
}
} catch (\Exception $e) {
return $documents;
}
return $documents;
}
/**
* Deletes index from elasticsearch.
*/
private function deleteIndex()
{
try {
$this->client->indices()->delete(['index' => self::INDEX_NAME]);
} catch (\Exception $e) {
// Do nothing.
}
}
}