/**
  *This is all horribly terible and will be ripped out as soon as possible.
  *
  * Basically, This class stores an intermediate Command that maintains state across
  * chained requests, which allows updates (scripts, params, etc) to affect it.
  *
  * The command is finalized when a new document is added, or the request is executed.
  * Finalization means collapsing the doc field data into a param, as well as updating
  * the action/suffix as necessary.
  */
 private function finalizeCurrentCommand()
 {
     if ($this->batch instanceof BatchCommand && $this->currentCommand !== null) {
         if (isset($this->params['update']) && $this->params['update'] === true) {
             $this->currentCommand->action('post')->suffix('_update');
             if ($this->params['doc'] !== null) {
                 $data["doc"] = $this->params['doc'];
             }
             if ($this->params['updateScript'] !== null) {
                 $data["script"] = $this->params['updateScript'];
             }
             if ($this->params['updateParams'] !== null) {
                 $data["params"] = $this->params['updateParams'];
             }
             if ($this->params['updateUpsert'] !== null) {
                 $data["upsert"] = $this->params['updateUpsert'];
             }
             $this->currentCommand->data($data);
         } else {
             $this->currentCommand->data($this->params['doc']);
         }
         $this->batch->addCommand($this->currentCommand);
         $this->params['update'] = false;
     }
     $this->currentCommand = new Command();
 }
Beispiel #2
0
 /**
  * Pregenerate 2000 docs to insert, just as a demonstration
  * This could easily be opening a filestream, etc
  */
 public function __construct()
 {
     for ($i = 0; $i < 2000; $i++) {
         $tDoc = new Sherlock\requests\Command();
         $tDoc->action('post')->index('testindexing')->type('tweet')->data('{"field":"test"}');
         $this->commands[] = $tDoc;
     }
 }
 /**
  * The document to delete
  *
  * @param  null                                         $id
  *
  * @throws \Sherlock\common\exceptions\RuntimeException
  * @return DeleteDocumentRequest
  */
 public function document($id)
 {
     if (!$this->batch instanceof BatchCommand) {
         throw new exceptions\RuntimeException("Cannot delete a document from an external BatchCommandInterface");
     }
     $command = new Command();
     $command->id($id)->action('delete');
     //Only doing this because typehinting is wonky without it...
     if ($this->batch instanceof BatchCommand) {
         $this->batch->addCommand($command);
     }
     return $this;
 }
Beispiel #4
0
 /**
  * Execute the RawRequest
  *
  * @throws \Sherlock\common\exceptions\RuntimeException
  * @return \Sherlock\responses\QueryResponse
  */
 public function execute()
 {
     if (!isset($this->params['uri'])) {
         throw new exceptions\RuntimeException("URI is required for RawRequest");
     }
     if (!isset($this->params['method'])) {
         throw new exceptions\RuntimeException("Method is required for RawRequest");
     }
     $command = new Command();
     $command->index($this->params['uri'])->action($this->params['method']);
     if (isset($this->params['body'])) {
         $command->data($this->params['body']);
     }
     $this->batch->clearCommands();
     $this->batch->addCommand($command);
     $ret = parent::execute();
     return $ret[0];
 }
Beispiel #5
0
 /**
  * Update/add the Mapping of an index
  *
  * @return \Sherlock\responses\IndexResponse
  * @throws exceptions\RuntimeException
  */
 public function updateMapping()
 {
     if (!isset($this->params['index'])) {
         throw new exceptions\RuntimeException("Index cannot be empty.");
     }
     if (count($this->params['indexMappings']) > 1) {
         throw new exceptions\RuntimeException("May only update one mapping at a time.");
     }
     if (!isset($this->params['type'])) {
         throw new exceptions\RuntimeException("Type must be specified.");
     }
     if (count($this->params['type']) > 1) {
         throw new exceptions\RuntimeException("Only one type may be updated at a time.");
     }
     $index = implode(',', $this->params['index']);
     $body = $this->params['indexMappings'];
     $command = new Command();
     $command->index($index)->type($this->params['type'][0])->id('_mapping')->action('put')->data(json_encode($body, JSON_FORCE_OBJECT));
     $this->batch->clearCommands();
     $this->batch->addCommand($command);
     $ret = parent::execute();
     //clear out mappings, settings
     //$this->resetIndex();
     return $ret[0];
 }
Beispiel #6
0
 /**
  * Execute the search request on the ES cluster
  *
  * @throws \Sherlock\common\exceptions\RuntimeException
  * @return \Sherlock\responses\QueryResponse
  */
 public function execute()
 {
     $finalQuery = $this->composeFinalQuery();
     if (isset($this->params['index'])) {
         $index = implode(',', $this->params['index']);
     } else {
         $index = '';
     }
     if (isset($this->params['type'])) {
         $type = implode(',', $this->params['type']);
     } else {
         $type = '';
     }
     if (isset($this->params['search_type'])) {
         $queryParams[] = $this->params['search_type'];
     }
     if (isset($this->params['routing'])) {
         $queryParams[] = $this->params['routing'];
     }
     if (isset($queryParams)) {
         $queryParams = '?' . implode("&", $queryParams);
     } else {
         $queryParams = '';
     }
     $command = new Command();
     $command->index($index)->type($type)->id('_search' . $queryParams)->action('post')->data($finalQuery);
     $this->batch->clearCommands();
     $this->batch->addCommand($command);
     $ret = parent::execute();
     return $ret[0];
 }