Exemplo n.º 1
0
 private function getSearchedFields(SearchEngineOptions $options)
 {
     $fields = [];
     foreach ($options->getFields() as $field) {
         $fields[] = $field->get_name();
     }
     return $fields;
 }
 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
 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;
 }
Exemplo n.º 4
0
 /**
  * Prepares the query
  *
  * @param  string        $query
  * @return PhraseaEngine
  */
 private function addQuery($query, SearchEngineOptions $options)
 {
     foreach ($options->getDataboxes() as $databox) {
         $this->queries[$databox->get_sbas_id()] = $query;
     }
     $status = $options->getStatus();
     foreach ($this->queries as $sbas => $qs) {
         if ($status) {
             $requestStat = 'xxxx';
             for ($i = 4; $i <= 32; $i++) {
                 if (!isset($status[$i])) {
                     $requestStat = 'x' . $requestStat;
                     continue;
                 }
                 $val = 'x';
                 if (isset($status[$i][$sbas])) {
                     if ($status[$i][$sbas] == '0') {
                         $val = '0';
                     } elseif ($status[$i][$sbas] == '1') {
                         $val = '1';
                     }
                 }
                 $requestStat = $val . $requestStat;
             }
             $requestStat = ltrim($requestStat, 'x');
             if ($requestStat !== '') {
                 $this->queries[$sbas] .= ' AND (recordstatus=' . $requestStat . ')';
             }
         }
         if ($options->getFields()) {
             $this->queries[$sbas] .= ' IN (' . implode(' OR ', array_map(function (\databox_field $field) {
                 return $field->get_name();
             }, $options->getFields())) . ')';
         }
         if (($options->getMinDate() || $options->getMaxDate()) && $options->getDateFields()) {
             if ($options->getMinDate()) {
                 $this->queries[$sbas] .= ' AND ( ' . implode(' >= ' . $options->getMinDate()->format('Y-m-d') . ' OR  ', array_map(function (\databox_field $field) {
                     return $field->get_name();
                 }, $options->getDateFields())) . ' >= ' . $options->getMinDate()->format('Y-m-d') . ' ) ';
             }
             if ($options->getMaxDate()) {
                 $this->queries[$sbas] .= ' AND ( ' . implode(' <= ' . $options->getMaxDate()->format('Y-m-d') . ' OR  ', array_map(function (\databox_field $field) {
                     return $field->get_name();
                 }, $options->getDateFields())) . ' <= ' . $options->getMaxDate()->format('Y-m-d') . ' ) ';
             }
         }
     }
     $this->singleParse('main', $query, $options);
     foreach ($this->queries as $sbas => $db_query) {
         $this->singleParse($sbas, $this->queries[$sbas], $options);
     }
     $base_ids = array_map(function (\collection $collection) {
         return $collection->get_base_id();
     }, $options->getCollections());
     foreach ($options->getDataboxes() as $databox) {
         $sbas_id = $databox->get_sbas_id();
         $this->colls[$sbas_id] = [];
         foreach ($databox->get_collections() as $collection) {
             if (in_array($collection->get_base_id(), $base_ids)) {
                 $this->colls[$sbas_id][] = $collection->get_base_id();
             }
         }
         if (sizeof($this->colls[$sbas_id]) <= 0) {
             continue;
         }
         if ($this->needthesaurus[$sbas_id]) {
             if ($domth = $databox->get_dom_thesaurus()) {
                 $this->qp[$sbas_id]->thesaurus2($this->indep_treeq[$sbas_id], $sbas_id, $databox->get_dbname(), $domth, true);
                 $this->qp['main']->thesaurus2($this->indep_treeq['main'], $sbas_id, $databox->get_dbname(), $domth, true);
             }
         }
         $emptyw = false;
         $this->qp[$sbas_id]->set_default($this->indep_treeq[$sbas_id], $emptyw);
         $this->qp[$sbas_id]->distrib_in($this->indep_treeq[$sbas_id]);
         $this->qp[$sbas_id]->factor_or($this->indep_treeq[$sbas_id]);
         $this->qp[$sbas_id]->setNumValue($this->indep_treeq[$sbas_id], $databox->get_sxml_structure());
         $this->qp[$sbas_id]->thesaurus2_apply($this->indep_treeq[$sbas_id], $sbas_id);
         $this->arrayq[$sbas_id] = $this->qp[$sbas_id]->makequery($this->indep_treeq[$sbas_id]);
     }
     return $this;
 }