-
Notifications
You must be signed in to change notification settings - Fork 43
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
|
There was a problem hiding this comment.
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