123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- namespace Mall\Framework\SearchClient;
- class Bulk {
- private $client;
- private $operations = array();
-
- public function __construct(Client $client) {
- $this->client = $client;
- }
-
- public function commit() {
- return $this->client->request('/_bulk', 'POST', $this->createPayload());
- }
-
-
- public function reset() {
- $this->operations = array();
- }
-
-
- public function index($document, $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('index' => $params),
- $document
- );
- $this->operations[] = $operation;
- return $this;
- }
-
- 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;
- }
-
- public function getOperations() {
- return $this->operations;
- }
-
- public function count() {
- return count($this->operations);
- }
-
- public function createPayload()
- {
- $payloads = array();
- foreach ($this->operations as $operation) {
- foreach ($operation as $partial) {
- $payloads[] = json_encode($partial);
- }
- }
- return join("\n", $payloads)."\n";
- }
- }
|