diff --git a/Classes/Domain/Model/AbstractType.php b/Classes/Domain/Model/AbstractType.php index c53ef00..2b708e8 100644 --- a/Classes/Domain/Model/AbstractType.php +++ b/Classes/Domain/Model/AbstractType.php @@ -92,7 +92,7 @@ public function findDocumentById($id) * @param string $content * @return Response */ - public function request($method, $path = null, array $arguments = [], $content = null) + public function request($method, $path = null, array $arguments = [], $content = null, $header = null) { $path = '/' . $this->name . ($path ?: ''); @@ -133,4 +133,30 @@ public function search(array $searchQuery) { return $this->request('GET', '/_search', [], json_encode($searchQuery)); } + + /** + * A bulk request always needs the following strukture: + * action_and_meta_data + * optional_source + * action_and_meta_data + * optional_source + * As the index and type are already in the url the meta_data part could be empty + * + * @param array $arguments + * @param string|array $content + * @return Response + */ + public function bulkRequest(array $arguments = [], $content = null) + { + $path = '/' . $this->name . '/_bulk'; + if (is_array($content)) { + $ndjsonContent = ''; + foreach ($content as $contentItem) { + // JSON_FORCE_OBJECT is important here as a empty meta_data needs to be a empty json object + $ndjsonContent = $ndjsonContent . json_encode($contentItem, JSON_FORCE_OBJECT) . "\n"; + } + $content = $ndjsonContent; + } + return $this->index->request('POST', $path, $arguments, $content, true, 'application/x-ndjson'); + } } diff --git a/Classes/Domain/Model/Client.php b/Classes/Domain/Model/Client.php index 8445aab..c89c5bf 100644 --- a/Classes/Domain/Model/Client.php +++ b/Classes/Domain/Model/Client.php @@ -95,10 +95,11 @@ public function findIndex($indexName) * @param string $path * @param array $arguments * @param string|array $content + * @param string $header * @return Response */ - public function request($method, $path = null, array $arguments = [], $content = null) + public function request($method, $path = null, array $arguments = [], $content = null, $header = null) { - return $this->requestService->request($method, $this, $path, $arguments, $content); + return $this->requestService->request($method, $this, $path, $arguments, $content, $header); } } diff --git a/Classes/Domain/Model/Index.php b/Classes/Domain/Model/Index.php index 54ecadc..1940be0 100644 --- a/Classes/Domain/Model/Index.php +++ b/Classes/Domain/Model/Index.php @@ -153,10 +153,11 @@ public function exists() * @param array $arguments * @param string $content * @param boolean $prefixIndex + * @param string $header * @return Response * @throws ElasticSearchException */ - public function request($method, $path = null, array $arguments = [], $content = null, $prefixIndex = true) + public function request($method, $path = null, array $arguments = [], $content = null, $prefixIndex = true, $header = null) { if ($this->client === null) { throw new Exception('The client of the index "' . $this->name . '" is not set, hence no requests can be done.'); @@ -168,7 +169,7 @@ public function request($method, $path = null, array $arguments = [], $content = $path = '/' . $path; } - return $this->client->request($method, $path, $arguments, $content); + return $this->client->request($method, $path, $arguments, $content, $header); } /** diff --git a/Classes/Transfer/RequestService.php b/Classes/Transfer/RequestService.php index 0fe7335..050ea54 100644 --- a/Classes/Transfer/RequestService.php +++ b/Classes/Transfer/RequestService.php @@ -61,9 +61,10 @@ public function initializeObject() * @param string $path * @param array $arguments * @param string|array $content + * @param string $header * @return Response */ - public function request($method, ElasticSearchClient $client, $path = null, $arguments = [], $content = null) + public function request($method, ElasticSearchClient $client, $path = null, $arguments = [], $content = null, $header = null) { $clientConfigurations = $client->getClientConfigurations(); $clientConfiguration = $clientConfigurations[0]; @@ -90,8 +91,9 @@ public function request($method, ElasticSearchClient $client, $path = null, $arg $requestUri->setUsername($uri->getUsername()); $requestUri->setPassword($uri->getPassword()); } - - $request->setHeader('Content-Type', 'application/json'); + + $header = $header ?: 'application/json'; + $request->setHeader('Content-Type', $header); $response = $this->browser->sendRequest($request); return new Response($response, $this->browser->getLastRequest());