Exemplo n.º 1
0
 private function createESQuery($query, SearchEngineOptions $options)
 {
     $preg = preg_match('/\\s?(recordid|storyid)\\s?=\\s?([0-9]+)/i', $query, $matches, 0, 0);
     $search = [];
     if ($preg > 0) {
         $search['bool']['must'][] = ['term' => ['record_id' => $matches[2]]];
         $query = '';
     }
     if ('' !== $query) {
         if (0 < count($options->getBusinessFieldsOn())) {
             $fields = [];
             foreach ($this->app['phraseanet.appbox']->get_databoxes() as $databox) {
                 foreach ($databox->get_meta_structure() as $dbField) {
                     if ($dbField->isBusiness()) {
                         $fields[$dbField->get_name()] = ['match' => ['caption.' . $dbField->get_name() => $query]];
                     }
                 }
             }
             if (count($fields) > 0) {
                 foreach ($options->getBusinessFieldsOn() as $coll) {
                     $search['bool']['should'][] = ['bool' => ['must' => [['bool' => ['should' => array_values($fields)]], ['term' => ['base_id' => $coll->get_base_id()]]]]];
                 }
             }
         }
         if ($options->getFields()) {
             foreach ($options->getFields() as $field) {
                 $search['bool']['should'][] = ['match' => ['caption.' . $field->get_name() => $query]];
             }
         } else {
             $search['bool']['should'][] = ['match' => ['_all' => $query]];
         }
     } else {
         $search['bool']['should'][] = ['match_all' => new \stdClass()];
     }
     return $search;
 }
 private function getQueryIndex($query, SearchEngineOptions $options)
 {
     $index = '*';
     $index_keys = [];
     foreach ($options->getDataboxes() as $databox) {
         $index_keys[] = $this->CRCdatabox($databox);
     }
     if (count($index_keys) > 0) {
         if ($options->getFields() || $options->getBusinessFieldsOn()) {
             if ($query !== '' && $options->isStemmed() && $options->getLocale()) {
                 $index = 'metadatas' . implode('_stemmed_' . $options->getLocale() . ', metadatas', $index_keys) . '_stemmed_' . $options->getLocale();
                 $index .= ', metas_realtime_stemmed_' . $options->getLocale() . '_' . implode(', metas_realtime_stemmed_' . $options->getLocale() . '_', $index_keys);
             } else {
                 $index = 'metadatas' . implode(',metadatas', $index_keys);
                 $index .= ', metas_realtime' . implode(', metas_realtime', $index_keys);
             }
         } else {
             if ($query !== '' && $options->isStemmed() && $options->getLocale()) {
                 $index = 'documents' . implode('_stemmed_' . $options->getLocale() . ', documents', $index_keys) . '_stemmed_' . $options->getLocale();
                 $index .= ', docs_realtime_stemmed_' . $options->getLocale() . '_' . implode(', docs_realtime_stemmed_' . $options->getLocale() . '_', $index_keys);
             } else {
                 $index = 'documents' . implode(', documents', $index_keys);
                 $index .= ', docs_realtime' . implode(', docs_realtime', $index_keys);
             }
         }
     }
     return $index;
 }
Exemplo n.º 3
0
 /**
  * Executes the Phrasea query
  *
  * @param  string        $query
  * @return PhraseaEngine
  */
 private function executeQuery($query, SearchEngineOptions $options)
 {
     $nbanswers = $total_time = 0;
     $sort = '';
     if ($options->getSortBy()) {
         switch ($options->getSortOrder()) {
             case SearchEngineOptions::SORT_MODE_ASC:
                 $sort = '+';
                 break;
             case SearchEngineOptions::SORT_MODE_DESC:
             default:
                 $sort = '-';
                 break;
         }
         $sort .= '0' . $options->getSortBy();
     }
     foreach ($this->queries as $sbas_id => $qry) {
         $BF = [];
         foreach ($options->getBusinessFieldsOn() as $collection) {
             // limit business field query to databox local collection
             if ($sbas_id === $collection->get_sbas_id()) {
                 $BF[] = $collection->get_base_id();
             }
         }
         $results = phrasea_query2($this->app['session']->get('phrasea_session_id'), $sbas_id, $this->colls[$sbas_id], $this->arrayq[$sbas_id], $this->app['conf']->get(['main', 'key']), $this->app['session']->get('usr_id'), false, $options->getSearchType() == SearchEngineOptions::RECORD_GROUPING ? PHRASEA_MULTIDOC_REGONLY : PHRASEA_MULTIDOC_DOCONLY, $sort, $BF, $options->isStemmed() ? $options->getLocale() : null);
         if ($results) {
             $total_time += $results['time_all'];
             $nbanswers += $results["nbanswers"];
         }
     }
     $sql = 'UPDATE cache
             SET query = :query, query_time = NOW(), duration = :duration, total = :total
             WHERE session_id = :ses_id';
     $params = ['query' => $query, ':ses_id' => $this->app['session']->get('phrasea_session_id'), ':duration' => $total_time, ':total' => $nbanswers];
     $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
     $stmt->execute($params);
     $stmt->closeCursor();
     return $this;
 }