@@ -17,7 +17,7 @@ class Elasticsearch implements ClientInterface
17
17
/**
18
18
* Elasticsearch Client instance
19
19
*
20
- * @var \Elasticsearch\Client
20
+ * @var \Elasticsearch\Client[]
21
21
*/
22
22
protected $ client ;
23
23
@@ -53,10 +53,19 @@ public function __construct(
53
53
$ config = $ this ->buildConfig ($ options );
54
54
$ elasticsearchClient = \Elasticsearch \ClientBuilder::fromConfig ($ config , true );
55
55
}
56
- $ this ->client = $ elasticsearchClient ;
56
+ $ this ->client [ getmypid ()] = $ elasticsearchClient ;
57
57
$ this ->clientOptions = $ options ;
58
58
}
59
59
60
+ private function getClient ()
61
+ {
62
+ $ pid = getmypid ();
63
+ if (!isset ($ this ->client [$ pid ])) {
64
+ $ config = $ this ->buildConfig ($ this ->clientOptions );
65
+ $ this ->client [$ pid ] = \Elasticsearch \ClientBuilder::fromConfig ($ config , true );
66
+ }
67
+ return $ this ->client [$ pid ];
68
+ }
60
69
/**
61
70
* Ping the Elasticsearch client
62
71
*
@@ -65,7 +74,7 @@ public function __construct(
65
74
public function ping ()
66
75
{
67
76
if ($ this ->pingResult === null ) {
68
- $ this ->pingResult = $ this ->client ->ping (['client ' => ['timeout ' => $ this ->clientOptions ['timeout ' ]]]);
77
+ $ this ->pingResult = $ this ->getClient () ->ping (['client ' => ['timeout ' => $ this ->clientOptions ['timeout ' ]]]);
69
78
}
70
79
return $ this ->pingResult ;
71
80
}
@@ -110,7 +119,7 @@ private function buildConfig($options = [])
110
119
*/
111
120
public function bulkQuery ($ query )
112
121
{
113
- $ this ->client ->bulk ($ query );
122
+ $ this ->getClient () ->bulk ($ query );
114
123
}
115
124
116
125
/**
@@ -122,7 +131,7 @@ public function bulkQuery($query)
122
131
*/
123
132
public function createIndex ($ index , $ settings )
124
133
{
125
- $ this ->client ->indices ()->create ([
134
+ $ this ->getClient () ->indices ()->create ([
126
135
'index ' => $ index ,
127
136
'body ' => $ settings ,
128
137
]);
@@ -136,7 +145,7 @@ public function createIndex($index, $settings)
136
145
*/
137
146
public function deleteIndex ($ index )
138
147
{
139
- $ this ->client ->indices ()->delete (['index ' => $ index ]);
148
+ $ this ->getClient () ->indices ()->delete (['index ' => $ index ]);
140
149
}
141
150
142
151
/**
@@ -147,7 +156,7 @@ public function deleteIndex($index)
147
156
*/
148
157
public function isEmptyIndex ($ index )
149
158
{
150
- $ stats = $ this ->client ->indices ()->stats (['index ' => $ index , 'metric ' => 'docs ' ]);
159
+ $ stats = $ this ->getClient () ->indices ()->stats (['index ' => $ index , 'metric ' => 'docs ' ]);
151
160
if ($ stats ['indices ' ][$ index ]['primaries ' ]['docs ' ]['count ' ] == 0 ) {
152
161
return true ;
153
162
}
@@ -172,7 +181,7 @@ public function updateAlias($alias, $newIndex, $oldIndex = '')
172
181
$ params ['body ' ]['actions ' ][] = ['add ' => ['alias ' => $ alias , 'index ' => $ newIndex ]];
173
182
}
174
183
175
- $ this ->client ->indices ()->updateAliases ($ params );
184
+ $ this ->getClient () ->indices ()->updateAliases ($ params );
176
185
}
177
186
178
187
/**
@@ -183,7 +192,7 @@ public function updateAlias($alias, $newIndex, $oldIndex = '')
183
192
*/
184
193
public function indexExists ($ index )
185
194
{
186
- return $ this ->client ->indices ()->exists (['index ' => $ index ]);
195
+ return $ this ->getClient () ->indices ()->exists (['index ' => $ index ]);
187
196
}
188
197
189
198
/**
@@ -198,7 +207,7 @@ public function existsAlias($alias, $index = '')
198
207
if ($ index ) {
199
208
$ params ['index ' ] = $ index ;
200
209
}
201
- return $ this ->client ->indices ()->existsAlias ($ params );
210
+ return $ this ->getClient () ->indices ()->existsAlias ($ params );
202
211
}
203
212
204
213
/**
@@ -208,7 +217,7 @@ public function existsAlias($alias, $index = '')
208
217
*/
209
218
public function getAlias ($ alias )
210
219
{
211
- return $ this ->client ->indices ()->getAlias (['name ' => $ alias ]);
220
+ return $ this ->getClient () ->indices ()->getAlias (['name ' => $ alias ]);
212
221
}
213
222
214
223
/**
@@ -267,7 +276,7 @@ public function addFieldsMapping(array $fields, $index, $entityType)
267
276
foreach ($ fields as $ field => $ fieldInfo ) {
268
277
$ params ['body ' ][$ entityType ]['properties ' ][$ field ] = $ fieldInfo ;
269
278
}
270
- $ this ->client ->indices ()->putMapping ($ params );
279
+ $ this ->getClient () ->indices ()->putMapping ($ params );
271
280
}
272
281
273
282
/**
@@ -279,7 +288,7 @@ public function addFieldsMapping(array $fields, $index, $entityType)
279
288
*/
280
289
public function deleteMapping ($ index , $ entityType )
281
290
{
282
- $ this ->client ->indices ()->deleteMapping ([
291
+ $ this ->getClient () ->indices ()->deleteMapping ([
283
292
'index ' => $ index ,
284
293
'type ' => $ entityType ,
285
294
]);
@@ -293,8 +302,7 @@ public function deleteMapping($index, $entityType)
293
302
*/
294
303
public function query ($ query )
295
304
{
296
- $ params = array_merge ($ query , ['client ' => ['timeout ' => $ this ->clientOptions ['timeout ' ]]]);
297
- return $ this ->client ->search ($ params );
305
+ return $ this ->getClient ()->search ($ query );
298
306
}
299
307
300
308
/**
@@ -305,6 +313,6 @@ public function query($query)
305
313
*/
306
314
public function suggest ($ query )
307
315
{
308
- return $ this ->client ->suggest ($ query );
316
+ return $ this ->getClient () ->suggest ($ query );
309
317
}
310
318
}
0 commit comments