Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature bulk request #81

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion Classes/Domain/Model/AbstractType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please specify the the type of the $header attribute and mention it in the commet

{
$path = '/' . $this->name . ($path ?: '');

Expand Down Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return type missing

{
$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');
}
}
5 changes: 3 additions & 2 deletions Classes/Domain/Model/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the header is a string, wouldn't be an empty string a better default?

{
return $this->requestService->request($method, $this, $path, $arguments, $content);
return $this->requestService->request($method, $this, $path, $arguments, $content, $header);
}
}
5 changes: 3 additions & 2 deletions Classes/Domain/Model/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand All @@ -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);
}

/**
Expand Down
8 changes: 5 additions & 3 deletions Classes/Transfer/RequestService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't be 'application/json' a better default then?

$request->setHeader('Content-Type', $header);
$response = $this->browser->sendRequest($request);

return new Response($response, $this->browser->getLastRequest());
Expand Down