Skip to content

Commit ba33150

Browse files
committed
MAGETWO-93966: Run Catalog Search reindex in multithread mode with Elasticsearch 5
1 parent 8c58f38 commit ba33150

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

app/code/Magento/Elasticsearch/Elasticsearch5/Model/Client/Elasticsearch.php

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
76
namespace Magento\Elasticsearch\Elasticsearch5\Model\Client;
87

98
use Magento\Framework\Exception\LocalizedException;
@@ -17,7 +16,7 @@ class Elasticsearch implements ClientInterface
1716
/**
1817
* Elasticsearch Client instance
1918
*
20-
* @var \Elasticsearch\Client
19+
* @var \Elasticsearch\Client[]
2120
*/
2221
protected $client;
2322

@@ -48,7 +47,7 @@ public function __construct(
4847
$elasticsearchClient = null
4948
) {
5049
if (empty($options['hostname']) || ((!empty($options['enableAuth']) &&
51-
($options['enableAuth'] == 1)) && (empty($options['username']) || empty($options['password'])))) {
50+
($options['enableAuth'] == 1)) && (empty($options['username']) || empty($options['password'])))) {
5251
throw new LocalizedException(
5352
__('The search failed because of a search engine misconfiguration.')
5453
);
@@ -58,10 +57,25 @@ public function __construct(
5857
$config = $this->buildConfig($options);
5958
$elasticsearchClient = \Elasticsearch\ClientBuilder::fromConfig($config, true);
6059
}
61-
$this->client = $elasticsearchClient;
60+
$this->client[getmypid()] = $elasticsearchClient;
6261
$this->clientOptions = $options;
6362
}
6463

64+
/**
65+
* Get Elasticsearch Client
66+
*
67+
* @return \Elasticsearch\Client
68+
*/
69+
private function getClient()
70+
{
71+
$pid = getmypid();
72+
if (!isset($this->client[$pid])) {
73+
$config = $this->buildConfig($this->clientOptions);
74+
$this->client[$pid] = \Elasticsearch\ClientBuilder::fromConfig($config, true);
75+
}
76+
return $this->client[$pid];
77+
}
78+
6579
/**
6680
* Ping the Elasticsearch client
6781
*
@@ -70,7 +84,7 @@ public function __construct(
7084
public function ping()
7185
{
7286
if ($this->pingResult === null) {
73-
$this->pingResult = $this->client->ping(['client' => ['timeout' => $this->clientOptions['timeout']]]);
87+
$this->pingResult = $this->getClient()->ping(['client' => ['timeout' => $this->clientOptions['timeout']]]);
7488
}
7589

7690
return $this->pingResult;
@@ -116,7 +130,7 @@ private function buildConfig($options = [])
116130
*/
117131
public function bulkQuery($query)
118132
{
119-
$this->client->bulk($query);
133+
$this->getClient()->bulk($query);
120134
}
121135

122136
/**
@@ -128,7 +142,7 @@ public function bulkQuery($query)
128142
*/
129143
public function createIndex($index, $settings)
130144
{
131-
$this->client->indices()->create([
145+
$this->getClient()->indices()->create([
132146
'index' => $index,
133147
'body' => $settings,
134148
]);
@@ -142,7 +156,7 @@ public function createIndex($index, $settings)
142156
*/
143157
public function deleteIndex($index)
144158
{
145-
$this->client->indices()->delete(['index' => $index]);
159+
$this->getClient()->indices()->delete(['index' => $index]);
146160
}
147161

148162
/**
@@ -153,7 +167,7 @@ public function deleteIndex($index)
153167
*/
154168
public function isEmptyIndex($index)
155169
{
156-
$stats = $this->client->indices()->stats(['index' => $index, 'metric' => 'docs']);
170+
$stats = $this->getClient()->indices()->stats(['index' => $index, 'metric' => 'docs']);
157171
if ($stats['indices'][$index]['primaries']['docs']['count'] == 0) {
158172
return true;
159173
}
@@ -178,7 +192,7 @@ public function updateAlias($alias, $newIndex, $oldIndex = '')
178192
$params['body']['actions'][] = ['add' => ['alias' => $alias, 'index' => $newIndex]];
179193
}
180194

181-
$this->client->indices()->updateAliases($params);
195+
$this->getClient()->indices()->updateAliases($params);
182196
}
183197

184198
/**
@@ -189,7 +203,7 @@ public function updateAlias($alias, $newIndex, $oldIndex = '')
189203
*/
190204
public function indexExists($index)
191205
{
192-
return $this->client->indices()->exists(['index' => $index]);
206+
return $this->getClient()->indices()->exists(['index' => $index]);
193207
}
194208

195209
/**
@@ -204,7 +218,7 @@ public function existsAlias($alias, $index = '')
204218
if ($index) {
205219
$params['index'] = $index;
206220
}
207-
return $this->client->indices()->existsAlias($params);
221+
return $this->getClient()->indices()->existsAlias($params);
208222
}
209223

210224
/**
@@ -214,7 +228,7 @@ public function existsAlias($alias, $index = '')
214228
*/
215229
public function getAlias($alias)
216230
{
217-
return $this->client->indices()->getAlias(['name' => $alias]);
231+
return $this->getClient()->indices()->getAlias(['name' => $alias]);
218232
}
219233

220234
/**
@@ -274,7 +288,7 @@ public function addFieldsMapping(array $fields, $index, $entityType)
274288
$params['body'][$entityType]['properties'][$field] = $this->prepareFieldInfo($fieldInfo);
275289
}
276290

277-
$this->client->indices()->putMapping($params);
291+
$this->getClient()->indices()->putMapping($params);
278292
}
279293

280294
/**
@@ -311,7 +325,7 @@ private function prepareFieldInfo($fieldInfo)
311325
*/
312326
public function deleteMapping($index, $entityType)
313327
{
314-
$this->client->indices()->deleteMapping([
328+
$this->getClient()->indices()->deleteMapping([
315329
'index' => $index,
316330
'type' => $entityType,
317331
]);
@@ -327,7 +341,7 @@ public function query($query)
327341
{
328342
$query = $this->prepareSearchQuery($query);
329343

330-
return $this->client->search($query);
344+
return $this->getClient()->search($query);
331345
}
332346

333347
/**
@@ -358,7 +372,7 @@ private function prepareSearchQuery($query)
358372
*/
359373
public function suggest($query)
360374
{
361-
return $this->client->suggest($query);
375+
return $this->getClient()->suggest($query);
362376
}
363377

364378
/**
@@ -369,7 +383,7 @@ public function suggest($query)
369383
private function getServerVersion()
370384
{
371385
if ($this->serverVersion === null) {
372-
$info = $this->client->info();
386+
$info = $this->getClient()->info();
373387
$this->serverVersion = $info['version']['number'];
374388
}
375389

0 commit comments

Comments
 (0)