Stores all information from a result
Автор: Nicolas Ruflin (spam@ruflin.com)
 /**
  * Takes either an array or a Result object form a search and constructs
  * a document representing an entity in a elastic search type,
  *
  * @param array|Elastica\Result $data An array or Result object that
  *  represents an Elasticsearch document
  * @param array $options An array of options to set the state of the
  *  document
  */
 public function __construct($data = [], $options = [])
 {
     if ($data instanceof Result) {
         $options['result'] = $data;
         $id = $data->getId();
         $data = $data->getData();
         if ($id !== []) {
             $data['id'] = $id;
         }
     }
     $options += ['useSetters' => true, 'markClean' => false, 'markNew' => null, 'guard' => false, 'source' => null, 'result' => null];
     if (!empty($options['source'])) {
         $this->source($options['source']);
     }
     if ($options['markNew'] !== null) {
         $this->isNew($options['markNew']);
     }
     if ($options['result'] !== null) {
         $this->_result = $options['result'];
     }
     if (!empty($data) && $options['markClean'] && !$options['useSetters']) {
         $this->_properties = $data;
         return;
     }
     if (!empty($data)) {
         $this->set($data, ['setter' => $options['useSetters'], 'guard' => $options['guard']]);
     }
     if ($options['markClean']) {
         $this->clean();
     }
 }
Пример #2
0
 /**
  * {@inheritDoc}
  */
 public function toModel(Result $result, Model $model)
 {
     $exclude = ['location', 'full_address'];
     $filtered = array_diff_key($result->getSource(), array_flip($exclude));
     $model->exists = true;
     $model->id = $result->getId();
     $model->fill($filtered);
     return $model;
 }
 /**
  * Returns the explanation array for this document as returned from ElasticSearch.
  *
  * If this is a new document, or the query used to create it did not ask for
  * explanation, this function will return an empty array.
  *
  * @return array
  */
 public function explanation()
 {
     if ($this->_result) {
         return $this->_result->getExplanation();
     }
     return [];
 }
Пример #4
0
 protected function formatResult(Result $result, $showField, $showSource, $showId, $explain)
 {
     $source = $result->getSource();
     if ($showField) {
         $toString = isset($source[$showField]) ? $source[$showField] : '-';
     } else {
         $toString = reset($source);
     }
     $string = sprintf('[%0.2f] %s', $result->getScore(), var_export($toString, true));
     if ($showSource) {
         $string = sprintf('%s %s', $string, json_encode($source));
     }
     if ($showId) {
         $string = sprintf('{%s} %s', $result->getId(), $string);
     }
     if ($explain) {
         $string = sprintf('%s %s', $string, json_encode($result->getExplanation()));
     }
     return $string;
 }
Пример #5
0
 /**
  * @param  Result $authorObject
  * @return string
  */
 private function generateAuthorUrl(Result $authorObject)
 {
     $source = $authorObject->getSource();
     if (!empty($source['user'])) {
         return $this->router->generate('ojs_user_profile', ['slug' => $source['user']['username']], true);
     } else {
         if (!isset($source['articleAuthors'][0]['article'])) {
             return false;
         }
         $article = $source['articleAuthors'][0]['article'];
         if (!isset($article['issue']['id'])) {
             return false;
         }
         return $this->router->generate('ojs_article_page', ['slug' => $article['journal']['slug'], 'article_id' => $article['id'], 'issue_id' => $article['issue']['id'], 'publisher' => $article['journal']['publisher']['slug']], true);
     }
 }
 /**
  * Return _source
  * @return array
  */
 public function getSource()
 {
     return $this->elasticaResult->getSource();
 }
Пример #7
0
 /**
  * @param  Result $authorObject
  * @return string
  */
 private function generateAuthorUrl(Result $authorObject)
 {
     $source = $authorObject->getSource();
     //check article count
     if (count($source['articleAuthors']) < 1) {
         return false;
     }
     //check article id is exists
     if (isset($source['articleAuthors'][0]['article']['id'])) {
         $article = $source['articleAuthors'][0]['article'];
     } else {
         return false;
     }
     //check article issue is exists
     if (isset($article['issue']['id'])) {
         $issue = $article['issue'];
     } else {
         return false;
     }
     //check article journal is exists
     if (isset($article['journal'])) {
         $journal = $article['journal'];
     } else {
         return false;
     }
     return $this->router->generate('ojs_article_page', ['slug' => $journal['slug'], 'article_id' => $article['id'], 'issue_id' => $issue['id']], true);
 }
Пример #8
0
 /**
  * Wrapper around formatBean based on Result
  *
  * @param \RestService $api
  * @param Result       $result
  *
  * @return array
  */
 protected function formatBeanFromResult(\RestService $api, array $args, \Elastica\Result $result, SugarBean $bean)
 {
     // pass in field list from available data fields on result
     if (empty($args['fields'])) {
         $args['fields'] = array_keys($result->getSource());
     }
     $bean->retrieve($result->getId());
     return $this->formatBean($api, $args, $bean);
 }
Пример #9
0
 /**
  * @group unit
  */
 public function testHasFields()
 {
     $data = array('value set');
     $result = new Result(array());
     $this->assertFalse($result->hasFields());
     $result = new Result(array('_source' => $data));
     $this->assertFalse($result->hasFields());
     $result = new Result(array('fields' => $data));
     $this->assertTrue($result->hasFields());
     $this->assertEquals($data, $result->getFields());
 }
Пример #10
0
 /**
  * @param Result $result
  * @return Html
  */
 private function getHighlightingTextFromResult(Result $result)
 {
     $highlights = $result->getHighlights();
     return Html::el()->setHtml(reset($highlights['text']));
 }
 /**
  * Set interwiki and interwikiNamespace properties
  * @param \Elastica\Result $result containing the given search result
  * @param string $interwiki Interwiki prefix, if any
  */
 private function setInterwiki($result, $interwiki)
 {
     $resultIndex = $result->getIndex();
     $indexBase = InterwikiSearcher::getIndexForInterwiki($interwiki);
     $pos = strpos($resultIndex, $indexBase);
     if ($pos === 0 && $resultIndex[strlen($indexBase)] == '_') {
         $this->interwiki = $interwiki;
         $this->interwikiNamespace = $result->namespace_text ? $result->namespace_text : '';
     }
 }
Пример #12
0
 /**
  * {@inheritDoc}
  */
 public function toModel(Result $result, Model $model)
 {
     $model = $model->find($result->getId());
     return $model;
 }