Exemplo n.º 1
0
 /**
  * 1) Обрабатвает термин текстовыми фильтрами, преобразовывает полученные
  *    формы в коды терминов.
  * 2) Добавляет запрос на ранжирование по этому термину (если необходимо).
  * 3) Возвращает массив с кодами, соответствующими всем формам термина (базовые
  *    формы, синонимы).
  *
  * Возвращает массив (zero-based) с кодами всех форм, соответствующих термину.
  * Если термин является стоп-словом, возвращает массив с единственным элементом "" (пустая строка).
  * Если в индексе нет ни одной формы термина, возвращает массив с элементом "____".
  *
  * Таким образом, в возвращаемом массиве всегда должен быть по крайней мере один
  * элемент.
  *
  * @param nc_search_query_expression_term $expression
  * @return array
  */
 protected function process_term(nc_search_query_expression_term $expression)
 {
     $string = $expression->get_value();
     // convert term to base forms
     $base_forms = $this->text_filters->apply('filter', array($string));
     if (!count($base_forms)) {
         // it's a stop-word obviously
         return array("");
     }
     // get codes
     $codes = array_unique($this->provider->get_term_codes($base_forms, false));
     // add the term to the ranking if it is not excluded
     $is_excluded = $this->is_inside("not") || $expression->is_excluded();
     // check whether there is at least one code
     if (count($codes) == 0) {
         // is this term required for all documents?
         $is_required = !$is_excluded && ($this->is_root() || $expression->is_required() || $this->parent_is("phrase") || $this->parent_is("and") && !$this->is_inside("or"));
         if ($is_required) {
             // this query won't produce any results anyway, so we could
             // spare a database request later
             $this->unknown_required_terms[] = $string;
         }
         $codes[] = "____";
         // dummy "non-existent term" code
     } elseif (!$is_excluded) {
         $this->query_builder->add_term_ranking($expression->get_field(), $codes, $expression->get_boost());
     }
     return $codes;
 }
Exemplo n.º 2
0
 /**
  *
  * @param string $interface
  * @param nc_search_context $context
  * @return nc_search_extension_chain с экземплярами классов расширения, подходящих под данный контекст
  * @throws nc_search_exception
  */
 public static function get($interface, nc_search_context $context)
 {
     $cache_id = $interface . "__" . $context->get_hash();
     if (isset(self::$cache[$cache_id])) {
         return self::$cache[$cache_id];
     }
     $result = new nc_search_extension_chain();
     foreach (self::get_all_extensions() as $rule) {
         if ($rule->get('extension_interface') == $interface && $context->conforms_to($rule)) {
             $extension_class = $rule->get('extension_class');
             $extension_instance = new $extension_class($context);
             if (!$extension_instance instanceof $interface) {
                 // WTF? Implement an interface!
                 throw new nc_search_exception("Extension '{$extension_class}' does not implement the interface '{$interface}'");
             }
             $result->add($extension_instance);
         }
     }
     self::$cache[$cache_id] = $result;
     return $result;
 }