@@ -81,7 +81,7 @@ public function indexDocument(
81
81
if ($ options ) {
82
82
$ uri .= '? ' . http_build_query ($ options );
83
83
}
84
- return $ this ->doRequest ($ this ->createJsonRequest ($ method , $ uri , $ body ));
84
+ return $ this ->doRequest ($ this ->createJsonRequest ($ method , $ uri , json_encode ( $ body) ));
85
85
}
86
86
87
87
public function existsDocument (string $ index , string $ id , string $ type = '_doc ' ): Promise
@@ -177,7 +177,7 @@ public function search(array $query, string $indexOrIndices = null, array $optio
177
177
if ($ options ) {
178
178
$ uri .= '? ' . http_build_query ($ options );
179
179
}
180
- return $ this ->doRequest ($ this ->createJsonRequest ($ method , $ uri , ['query ' => $ query ]));
180
+ return $ this ->doRequest ($ this ->createJsonRequest ($ method , $ uri , json_encode ( ['query ' => $ query ]) ));
181
181
}
182
182
183
183
public function count (string $ index , array $ options = [], array $ query = null ): Promise
@@ -189,20 +189,36 @@ public function count(string $index, array $options = [], array $query = null):
189
189
if ($ options ) {
190
190
$ uri .= '? ' . http_build_query ($ options );
191
191
}
192
- $ body = null ;
193
192
if (null !== $ query ) {
194
- $ body = ['query ' => $ query ];
193
+ return $ this -> doRequest ( $ this -> createJsonRequest ( $ method , $ uri , json_encode ( ['query ' => $ query ]))) ;
195
194
}
196
- return $ this ->doRequest ($ this ->createJsonRequest ($ method , $ uri , $ body ));
195
+ return $ this ->doRequest ($ this ->createJsonRequest ($ method , $ uri ));
196
+ }
197
+
198
+ public function bulk (array $ body , string $ index = null , array $ options = []): Promise
199
+ {
200
+ $ method = 'POST ' ;
201
+ $ uri = [$ this ->baseUri ];
202
+ if ($ index ) {
203
+ $ uri [] = urlencode ($ index );
204
+ }
205
+ $ uri [] = '_bulk ' ;
206
+ $ uri = implode ('/ ' , $ uri );
207
+ if ($ options ) {
208
+ $ uri .= '? ' . http_build_query ($ options );
209
+ }
210
+ return $ this ->doRequest (
211
+ $ this ->createJsonRequest ($ method , $ uri , implode (PHP_EOL , array_map ('json_encode ' , $ body )) . PHP_EOL )
212
+ );
197
213
}
198
214
199
- private function createJsonRequest (string $ method , string $ uri , array $ body = null ): Request
215
+ private function createJsonRequest (string $ method , string $ uri , string $ body = null ): Request
200
216
{
201
217
$ request = (new Request ($ uri , $ method ))
202
218
->withHeader ('Content-Type ' , 'application/json ' )
203
219
->withHeader ('Accept ' , 'application/json ' );
204
220
if ($ body ) {
205
- $ request = $ request ->withBody (json_encode ( $ body) );
221
+ $ request = $ request ->withBody ($ body );
206
222
}
207
223
return $ request ;
208
224
}
0 commit comments