Esempio n. 1
0
 /**
  * @param JobInterface $job
  * @return string jobId
  */
 public function update(JobInterface $job)
 {
     $job->validate();
     $params = ['index' => $job->getIndex(), 'type' => $job->getType(), 'id' => $job->getId(), 'body' => ['doc' => $job->getData()]];
     $response = null;
     $i = 0;
     while ($i < 5) {
         try {
             $response = $this->client->update($params);
             break;
         } catch (ServerErrorResponseException $e) {
             // ES server error, try again
             $this->log('error', 'Elastic server error response', ['attemptNo' => $i, 'jobId' => $job->getId(), 'exception' => $e]);
         }
         sleep(1 + intval(pow(2, $i) / 2));
         $i++;
     }
     $i = 0;
     while ($i < 5) {
         $resJob = $this->get($job->getId());
         if ($resJob != null && $resJob->getVersion() >= $response['_version']) {
             return $response['_id'];
         }
         sleep(1 + intval(pow(2, $i) / 2));
         $i++;
     }
     throw new ApplicationException("Unable to find job after update", null, ['job' => $job->getData(), 'elasticResponse' => $response]);
 }
 /**
  * @param SavableModelInterface $model
  * @throws CouldNotPersistException
  */
 public function persist(SavableModelInterface $model)
 {
     $params = ['index' => $this->index, 'type' => $this->type];
     if ($model->getId()) {
         $params['body'] = ['doc' => $model->toArray()];
         $params['id'] = $model->getId();
         $this->client->update($params);
         $model->markAsStored();
         return;
     }
     $params['body'] = $model->toArray();
     $response = $this->client->index($params);
     $model->id = $response['_id'];
     $model->markAsStored();
 }
Esempio n. 3
0
 /**
  * @param string $snsid
  * @param int    $status
  *
  * @return array
  */
 protected function updateUserStatus($snsid, $status)
 {
     assert(is_numeric($status));
     $params = ['index' => $this->index, 'type' => $this->type, 'id' => $snsid, 'body' => '{"doc": {"status": "' . $status . '"}}'];
     try {
         $ret = $this->client->update($params);
         return ['snsid' => $snsid, 'version' => $ret['_version']];
     } catch (\Exception $e) {
         $errMsg = $e->getMessage();
         $decodedArray = json_decode($errMsg, true);
         if (is_array($decodedArray)) {
             return ['snsid' => $snsid, 'error' => $decodedArray['status']];
         }
         return ['snsid' => $snsid, 'error' => $errMsg];
     }
 }
Esempio n. 4
0
 /**
  * {@inheritdoc}
  */
 public function saveDocument(Searchable $model)
 {
     $document = $this->constructDocument($model);
     if (!$this->indexIsNested($model->getSearchIndex())) {
         $this->client->index($document);
         return;
     }
     list($index, $type) = $this->retrieveNestedIndex($model->getSearchIndex());
     $class = $this->retrieveParentClass($model->getSearchIndex());
     $parent = $model->belongsTo($class, null, null, class_basename($class))->getResults();
     $parentData = $this->client->get(['id' => $parent->getKey(), 'type' => $parent->getSearchType(), 'index' => $parent->getSearchIndex()])['_source'];
     if (!isset($parentData[$type])) {
         $parentData[$type] = [];
     }
     $children = Collection::make($parentData[$type]);
     if ($child = $children->first(function ($child) use($model) {
         return $child[$model->getKeyName()] == $model->getKey();
     })) {
         $newChildren = $children->map(function ($child) use($model) {
             if ($child[$model->getKeyName()] == $model->getKey()) {
                 $child = $model->documentToArray();
                 if (!isset($document[$model->getKeyName()])) {
                     $child[$model->getKeyName()] = $model->getKey();
                 }
             }
             return $child;
         });
     } else {
         $newChildren = $children->push($model->documentToArray());
     }
     $this->client->update(['id' => $parent->getKey(), 'type' => $parent->getSearchType(), 'index' => $parent->getSearchIndex(), 'body' => ['doc' => [$type => $newChildren]]]);
 }
Esempio n. 5
0
 /**
  * @param Searchable $type
  */
 public function update(Searchable $type)
 {
     //clone object so we do not touch original one.
     //this was messing with translatable library.
     //since the parent model saves before the translations,
     //translations wouldn't be in database and we overwrite them here by loading those translations
     $type = clone $type;
     $params = $this->getBaseParams($type);
     $type->load(array_keys($this->config->getWith($type->getSearchableType())));
     $params = array_merge($params, ['id' => $type->getSearchableId(), 'body' => ['doc' => $type->getSearchableDocument()]]);
     if ($type->useSearchableRouting()) {
         $params['routing'] = $type->getSearchableRouting();
     }
     $this->client->update($params);
 }
Esempio n. 6
0
 /**
  * Execute a update statement on index;.
  *
  * @param $params
  *
  * @return array
  */
 public function updateStatement(array $params)
 {
     return $this->elastic->update($this->setStatementIndex($params));
 }
Esempio n. 7
0
 /**
  * Update a document index.
  *
  * @param array $params
  *
  * @return array
  */
 public function update(array $params)
 {
     return $this->client->update($this->prepareParams($params));
 }
Esempio n. 8
0
 /**
  * Perform an insertion into the engine.
  *
  * @param  array $payload
  * @return array
  */
 public function documentUpdate($payload)
 {
     return $this->client->update($payload);
 }
 public function updateDoc($type, $id, array $data)
 {
     return parent::update(['index' => $this->index, 'type' => $type, 'id' => $id, 'body' => ['doc' => $data]]);
 }