123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php // vim:set ts=4 sw=4 et:
- namespace Mall\Framework\SearchClient;
- class Bulk {
- private $client;
- private $operations = array();
- /**
- * Construct a bulk operation
- *
- * @param \ElasticSearch\Client
- */
- public function __construct(Client $client) {
- $this->client = $client;
- }
- /**
- * commit this operation
- */
- public function commit() {
- return $this->client->request('/_bulk', 'POST', $this->createPayload());
- }
-
- /**
- * reset this operation
- */
- public function reset() {
- $this->operations = array();
- }
-
- /**
- * 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
- * @return \Mall\Framework\SearchClient
- */
- 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;
- }
- /**
- * delete a document
- *
- * @param mixed $id
- * @param string $index Index
- * @param string $type Type
- * @param array $options Parameters to pass to delete action
- * @return \Mall\Framework\SearchClient
- */
- 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 getOperations() {
- return $this->operations;
- }
- /**
- * count all pending operations
- * @return int
- */
- public function count() {
- return count($this->operations);
- }
- /**
- * 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";
- }
- }
|