3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
-
7
6
namespace Magento \Elasticsearch \Elasticsearch5 \Model \Client ;
8
7
9
8
use Magento \Framework \Exception \LocalizedException ;
@@ -17,7 +16,7 @@ class Elasticsearch implements ClientInterface
17
16
/**
18
17
* Elasticsearch Client instance
19
18
*
20
- * @var \Elasticsearch\Client
19
+ * @var \Elasticsearch\Client[]
21
20
*/
22
21
protected $ client ;
23
22
@@ -48,7 +47,7 @@ public function __construct(
48
47
$ elasticsearchClient = null
49
48
) {
50
49
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 ' ])))) {
52
51
throw new LocalizedException (
53
52
__ ('The search failed because of a search engine misconfiguration. ' )
54
53
);
@@ -58,10 +57,25 @@ public function __construct(
58
57
$ config = $ this ->buildConfig ($ options );
59
58
$ elasticsearchClient = \Elasticsearch \ClientBuilder::fromConfig ($ config , true );
60
59
}
61
- $ this ->client = $ elasticsearchClient ;
60
+ $ this ->client [ getmypid ()] = $ elasticsearchClient ;
62
61
$ this ->clientOptions = $ options ;
63
62
}
64
63
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
+
65
79
/**
66
80
* Ping the Elasticsearch client
67
81
*
@@ -70,7 +84,7 @@ public function __construct(
70
84
public function ping ()
71
85
{
72
86
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 ' ]]]);
74
88
}
75
89
76
90
return $ this ->pingResult ;
@@ -116,7 +130,7 @@ private function buildConfig($options = [])
116
130
*/
117
131
public function bulkQuery ($ query )
118
132
{
119
- $ this ->client ->bulk ($ query );
133
+ $ this ->getClient () ->bulk ($ query );
120
134
}
121
135
122
136
/**
@@ -128,7 +142,7 @@ public function bulkQuery($query)
128
142
*/
129
143
public function createIndex ($ index , $ settings )
130
144
{
131
- $ this ->client ->indices ()->create ([
145
+ $ this ->getClient () ->indices ()->create ([
132
146
'index ' => $ index ,
133
147
'body ' => $ settings ,
134
148
]);
@@ -142,7 +156,7 @@ public function createIndex($index, $settings)
142
156
*/
143
157
public function deleteIndex ($ index )
144
158
{
145
- $ this ->client ->indices ()->delete (['index ' => $ index ]);
159
+ $ this ->getClient () ->indices ()->delete (['index ' => $ index ]);
146
160
}
147
161
148
162
/**
@@ -153,7 +167,7 @@ public function deleteIndex($index)
153
167
*/
154
168
public function isEmptyIndex ($ index )
155
169
{
156
- $ stats = $ this ->client ->indices ()->stats (['index ' => $ index , 'metric ' => 'docs ' ]);
170
+ $ stats = $ this ->getClient () ->indices ()->stats (['index ' => $ index , 'metric ' => 'docs ' ]);
157
171
if ($ stats ['indices ' ][$ index ]['primaries ' ]['docs ' ]['count ' ] == 0 ) {
158
172
return true ;
159
173
}
@@ -178,7 +192,7 @@ public function updateAlias($alias, $newIndex, $oldIndex = '')
178
192
$ params ['body ' ]['actions ' ][] = ['add ' => ['alias ' => $ alias , 'index ' => $ newIndex ]];
179
193
}
180
194
181
- $ this ->client ->indices ()->updateAliases ($ params );
195
+ $ this ->getClient () ->indices ()->updateAliases ($ params );
182
196
}
183
197
184
198
/**
@@ -189,7 +203,7 @@ public function updateAlias($alias, $newIndex, $oldIndex = '')
189
203
*/
190
204
public function indexExists ($ index )
191
205
{
192
- return $ this ->client ->indices ()->exists (['index ' => $ index ]);
206
+ return $ this ->getClient () ->indices ()->exists (['index ' => $ index ]);
193
207
}
194
208
195
209
/**
@@ -204,7 +218,7 @@ public function existsAlias($alias, $index = '')
204
218
if ($ index ) {
205
219
$ params ['index ' ] = $ index ;
206
220
}
207
- return $ this ->client ->indices ()->existsAlias ($ params );
221
+ return $ this ->getClient () ->indices ()->existsAlias ($ params );
208
222
}
209
223
210
224
/**
@@ -214,7 +228,7 @@ public function existsAlias($alias, $index = '')
214
228
*/
215
229
public function getAlias ($ alias )
216
230
{
217
- return $ this ->client ->indices ()->getAlias (['name ' => $ alias ]);
231
+ return $ this ->getClient () ->indices ()->getAlias (['name ' => $ alias ]);
218
232
}
219
233
220
234
/**
@@ -274,7 +288,7 @@ public function addFieldsMapping(array $fields, $index, $entityType)
274
288
$ params ['body ' ][$ entityType ]['properties ' ][$ field ] = $ this ->prepareFieldInfo ($ fieldInfo );
275
289
}
276
290
277
- $ this ->client ->indices ()->putMapping ($ params );
291
+ $ this ->getClient () ->indices ()->putMapping ($ params );
278
292
}
279
293
280
294
/**
@@ -311,7 +325,7 @@ private function prepareFieldInfo($fieldInfo)
311
325
*/
312
326
public function deleteMapping ($ index , $ entityType )
313
327
{
314
- $ this ->client ->indices ()->deleteMapping ([
328
+ $ this ->getClient () ->indices ()->deleteMapping ([
315
329
'index ' => $ index ,
316
330
'type ' => $ entityType ,
317
331
]);
@@ -327,7 +341,7 @@ public function query($query)
327
341
{
328
342
$ query = $ this ->prepareSearchQuery ($ query );
329
343
330
- return $ this ->client ->search ($ query );
344
+ return $ this ->getClient () ->search ($ query );
331
345
}
332
346
333
347
/**
@@ -358,7 +372,7 @@ private function prepareSearchQuery($query)
358
372
*/
359
373
public function suggest ($ query )
360
374
{
361
- return $ this ->client ->suggest ($ query );
375
+ return $ this ->getClient () ->suggest ($ query );
362
376
}
363
377
364
378
/**
@@ -369,7 +383,7 @@ public function suggest($query)
369
383
private function getServerVersion ()
370
384
{
371
385
if ($ this ->serverVersion === null ) {
372
- $ info = $ this ->client ->info ();
386
+ $ info = $ this ->getClient () ->info ();
373
387
$ this ->serverVersion = $ info ['version ' ]['number ' ];
374
388
}
375
389
0 commit comments