Skip to content
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
14 changes: 0 additions & 14 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,6 @@ $es->get($id);
$es->search('title:cool');
```

### Creating mapping

```php
$es->map(array(
'title' => array(
'type' => 'string',
'index' => 'analyzed'
)
));
```

### Search multiple indexes or types

```php
Expand Down Expand Up @@ -157,6 +146,3 @@ class FooController extends Controller
}
}
```



6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name" : "nervetattoo/elasticsearch",
"type" : "library",
"description" : "ElasticSearch client for PHP 5.3",
"description" : "ElasticSearch client for PHP 7.1+",
"keywords" : ["elasticsearch", "client"],
"homepage" : "http://github.com/nervetattoo/elasticsearch",
"license" : "MIT",
Expand All @@ -13,7 +13,9 @@
}
],
"require" : {
"php" : ">=5.3.0"
"php" : ">=7.1",
"ext-curl": "*",
"ext-json": "*"
},
"require-dev" : {
"atoum/atoum" : "^2.9"
Expand Down
203 changes: 110 additions & 93 deletions src/ElasticSearch/Bulk.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,71 @@

namespace ElasticSearch;

class Bulk {
class Bulk
{

private $client;
private $operations = array();
private $client;
private $operations = [];

/**
* Construct a bulk operation
*
* @param \ElasticSearch\Client
*/

public function __construct(Client $client) {
$this->client = $client;
}
public function __construct(Client $client)
{
$this->client = $client;
}

/**
* commit this operation
*/
public function commit() {
return $this->client->request('/_bulk', 'POST', $this->createPayload());
}

public function commit(): array
{
return $this->client->request('/_bulk', 'POST', $this->createPayload());
}

/**
* reset this operation
*/
public function reset() {
$this->operations = array();
}

public function reset()
{
$this->operations = [];
}

/**
* Index a new document or update it if existing
*
* @param array $document
* @param mixed $id Optional
* @param string $index Index
* @param string $type Type
* @param array $options Allow sending query parameters to control indexing further
* _refresh_ *bool* If set to true, immediately refresh the shard after indexing
* @param array $document
* @param string|null $id Optional
* @param string $index Index
* @param string $type Type
* @param array $options Allow sending query parameters to control indexing further
* _refresh_ *bool* If set to true, immediately refresh the shard after indexing
*
* @return \Elasticsearch\Bulk
*/
public function index($document, $id=null, $index, $type, array $options = array()) {
$params = array( '_id' => $id,
'_index' => $index,
'_type' => $type);

foreach ($options as $key => $value) {
$params['_' . $key] = $value;
}

$operation = array(
array('index' => $params),
$document
);
$this->operations[] = $operation;
return $this;
}
public function index(array $document, ?string $id, string $index, string $type, array $options = []): self
{
$params = [
'_id' => $id,
'_index' => $index,
'_type' => $type
];

foreach ($options as $key => $value) {
$params['_' . $key] = $value;
}

$operation = [
[ 'index' => $params ],
$document,
];
$this->operations[] = $operation;

return $this;
}

/**
* Update a part of a document
Expand All @@ -71,80 +80,88 @@ public function index($document, $id=null, $index, $type, array $options = array
*
* @return \Elasticsearch\Bulk
*/
public function update($partialDocument, $id, $index, $type, array $options = array()) {
$params = array(
'_id' => $id,
public function update(array $partialDocument, string $id, string $index, string $type, array $options = []): self
{
$params = [
'_id' => $id,
'_index' => $index,
'_type' => $type,
);
'_type' => $type,
];

foreach ($options as $key => $value) {
$params['_'.$key] = $value;
$params['_' . $key] = $value;
}

$operation = array(
array('update' => $params),
array('doc' => $partialDocument),
);
$operation = [
[ 'update' => $params ],
[ 'doc' => $partialDocument ],
];
$this->operations[] = $operation;

return $this;
}

/**
/**
* delete a document
*
* @param mixed $id
* @param string $index Index
* @param string $type Type
* @param array $options Parameters to pass to delete action
* @param string $id
* @param string $index Index
* @param string $type Type
* @param array $options Parameters to pass to delete action
*
* @return \Elasticsearch\Bulk
*/
public function delete($id=false, $index, $type, array $options = array()) {
$params = array( '_id' => $id,
'_index' => $index,
'_type' => $type);

foreach ($options as $key => $value) {
$params['_' . $key] = $value;
}

$operation = array(
array('delete' => $params)
);
$this->operations[] = $operation;
return $this;

}

/**
* get all pending operations
* @return array
public function delete(string $id, string $index, string $type, array $options = []): Bulk
{
$params = [
'_id' => $id,
'_index' => $index,
'_type' => $type
];

foreach ($options as $key => $value) {
$params['_' . $key] = $value;
}

$operation = [
[ 'delete' => $params ],
];
$this->operations[] = $operation;

return $this;
}

/**
* get all pending operations
* @return array
*/
public function getOperations() {
return $this->operations;
}
public function getOperations(): array
{
return $this->operations;
}

/**
* count all pending operations
* @return int
/**
* count all pending operations
* @return int
*/
public function count() {
return count($this->operations);
}
public function count(): int
{
return count($this->operations);
}

/**
* create a request payload with all pending operations
* @return string
/**
* create a request payload with all pending operations
* @return string
*/
public function createPayload()
{
$payloads = array();
foreach ($this->operations as $operation) {
foreach ($operation as $partial) {
$payloads[] = json_encode($partial);
}
}
return join("\n", $payloads)."\n";
}
public function createPayload(): string
{
$payloads = [];
foreach ($this->operations as $operation) {
foreach ($operation as $partial) {
$payloads[] = json_encode($partial);
}
}

return implode("\n", $payloads) . "\n";
}
}
Loading