.

DataObjectSearchIndexSphinxBase Class Reference

Model base class for an application wide search index. More...

Inheritance diagram for DataObjectSearchIndexSphinxBase:
DataObjectSphinxBase ISearchIndex DataObjectBase ISearchAdapter IDataObject IActionSource ISearchAdapter IDBTable IDBWhereHolder DAOSearchindex

List of all members.

Public Member Functions

  count ()
  Count resulting items.
  exclude_models ($models)
  Exclude models from search.
  execute ()
  Execute search and return Entries DAO objects.
  get_sort_default_column ()
  Get the column to sort by default.
  get_sortable_columns ()
  Return array of sortable columns.
  limit_to_models ($models)
  Include only given models in search.
  set_matching ($matching)
  Set matching mode (MATCH_WIDE or MATCH_NARROW).
  set_search ($search)
  Set the search string.
  sort_by_relevance ()
  Sort by Relevance.

Public Attributes

  $creationdate
  $id
  $item_id
  $item_model
  $meta
  $modificationdate
  $teaser
  $text
  $title

Protected Member Functions

  compute_age_weight ($weight_base)
  Compute weighting regarding the age of things.
  compute_model_weight ($weight_base)
  Compute weighting of models in relation to each other, like defined in model rules.
  compute_relevance_w ()
  COmputed weighted relevance.
  configure_select_query ($query, $policy)
  Configure a select query.
  create_table_object ()
  Create Table features.
  extract_model_ids ($models)
  Return an array of model ids for given models.
  get_additional_field_definitions ()
  Too be overloaded.
  preprocess_query ($query)
  Preprocess query string.
  resolve_model ($id)
  Turn a model ID into a model name.
  set_field_weights ()
  Set weights for columns.

Protected Attributes

  $matching = self::MATCH_NARROW

Detailed Description

Model base class for an application wide search index.

Author:
Gerd Riesselmann

Definition at line 8 of file dataobject.searchindex.base.cls.php.


Member Function Documentation

DataObjectSearchIndexSphinxBase::compute_age_weight ( weight_base  )  [protected]

Compute weighting regarding the age of things.

Attention:
This gets added, so to vote down older stuff, return a negative expression
Returns:
string

Definition at line 273 of file dataobject.searchindex.base.cls.php.

00273                                                             {
00274                 $ret = '0';
00275                 if ($this->matching == self::MATCH_NARROW) {
00276                         $month = 30 * GyroDate::ONE_DAY;
00277                         $now = time();
00278                         $weight_base = String::number($weight_base, 2, true);
00279                         
00280                         $ret = "-$weight_base * ($now - modificationdate) / $month";
00281                 }
00282                 return $ret;
00283         }
DataObjectSearchIndexSphinxBase::compute_model_weight ( weight_base  )  [protected]

Compute weighting of models in relation to each other, like defined in model rules.

Parameters:
int  $weight_base Factor by which a model with a relevance of 2 is voted up
Returns:
strng

Definition at line 291 of file dataobject.searchindex.base.cls.php.

00291                                                               {
00292                 $weight_if_expression = $weight_base;
00293                 // Build model weighting expression
00294                 foreach(SearchIndexRepository::get_model_rules() as $rule) {
00295                         $model_id = $rule->model_id;
00296                         $model_weight = String::number($weight_base * $rule->weight, 2, true); 
00297                         $weight_if_expression = "IF(item_model = $model_id, $model_weight, $weight_if_expression)";
00298                 }
00299                 return $weight_if_expression;
00300         }
DataObjectSearchIndexSphinxBase::compute_relevance_w (  )  [protected]

COmputed weighted relevance.

Returns:
string An expression Sphinx can evaluate

Definition at line 260 of file dataobject.searchindex.base.cls.php.

00260                                                  {
00261                 $model_weight = $this->compute_model_weight(3000);
00262                 $age_weight = $this->compute_age_weight(100);
00263                 return "@weight + ($age_weight) + ($model_weight)";
00264         }
DataObjectSearchIndexSphinxBase::configure_select_query ( query,
policy  
) [protected]

Configure a select query.

Parameters:
DBQuerySelect  $query
int  $policy

Reimplemented from DataObjectSphinxBase.

Definition at line 217 of file dataobject.searchindex.base.cls.php.

00217                                                                    {
00218         $this->set_field_weights();
00219         $this->sphinx_all_fields = $this->preprocess_query($this->sphinx_all_fields);
00220                 switch($this->matching) {
00221                         case self::MATCH_WIDE:
00222                                 $this->set_sphinx_feature(DBDriverSphinx::FEATURE_MATCH_MODE, DBDriverSphinx::MATCH_OR);
00223                                 break;
00224                         default:
00225                                 $this->set_sphinx_feature(DBDriverSphinx::FEATURE_MATCH_MODE, DBDriverSphinx::MATCH_EX);
00226                                 break;
00227                 }       
00228                 parent::configure_select_query($query, $policy);
00229                 
00230                 $query->set_fields(array(
00231                         '*',
00232                         $this->compute_relevance_w() => 'relevance_w'
00233                 ));             
00234         }
DataObjectSearchIndexSphinxBase::count (  ) 

Count resulting items.

Make same Behaviour as count_pager()

Returns:
int

Reimplemented from DataObjectBase.

Definition at line 143 of file dataobject.searchindex.base.cls.php.

00143                                 {
00144                 try {
00145                         $count = parent::count();
00146                 }
00147                 catch (Exception $ex) {
00148                         $this->set_sphinx_feature(DBDriverSphinx::FEATURE_STRIP_OPERATORS, true);
00149                         $count = parent::count();
00150                 }               
00151                 return min($count, APP_SPHINX_MAX_MATCHES);             
00152         }
DataObjectSearchIndexSphinxBase::create_table_object (  )  [protected]

Create Table features.

Returns:
DBTable

Reimplemented from DataObjectBase.

Definition at line 27 of file dataobject.searchindex.base.cls.php.

00027                                                  {
00028                 $this->set_matching(self::MATCH_NARROW);
00029                 return new DBTable(
00030                         Config::get_value(ConfigSearchIndex::TABLE_NAME),
00031                         array_merge(
00032                                 array(
00033                                         new DBFieldInt('id', null, DBFieldInt::PRIMARY_KEY),
00034                                         new DBFieldInt('item_id', null, DBFieldInt::UNSIGNED | DBDriverSphinx::SPHINX_ATTRIBUTE),
00035                                         new DBFieldInt('item_model', null, DBFieldInt::UNSIGNED | DBDriverSphinx::SPHINX_ATTRIBUTE),
00036                                         new DBFieldText('title', 200, null, DBField::NOT_NULL),
00037                                         new DBFieldText('teaser', DBFieldText::BLOB_LENGTH_SMALL, null, DBField::NONE),
00038                                         new DBFieldTextHtml('text', DBFieldText::BLOB_LENGTH_LARGE, null, DBField::NONE),
00039                                         new DBFieldText('meta', DBFieldText::BLOB_LENGTH_SMALL, null, DBField::NONE),
00040                                         new DBFieldDateTime('creationdate', DBFieldDateTime::NOW, DBFieldDateTime::NOT_NULL  | DBDriverSphinx::SPHINX_ATTRIBUTE),
00041                                         new DBFieldDateTime('modificationdate', DBFieldDateTime::NOW, DBFieldDateTime::NOT_NULL  | DBDriverSphinx::SPHINX_ATTRIBUTE)                                                                                                    
00042                                 ),
00043                                 $this->get_additional_field_definitions()       
00044                         ),
00045                         'id',
00046                         array(),
00047                         array(),
00048                         DBDriverSphinx::DEFAULT_CONNECTION_NAME
00049                 );
00050         }
DataObjectSearchIndexSphinxBase::exclude_models ( models  ) 

Exclude models from search.

Parameters:
string|array  Name of model or array of names of models

Implements ISearchIndex.

Definition at line 79 of file dataobject.searchindex.base.cls.php.

00079                                                 {
00080                 $exclude = $this->extract_model_ids($models);
00081                 if (count($exclude)) {
00082                         $this->add_where('item_model', DBWhere::OP_NOT_IN, $exclude);
00083                 }
00084         }
DataObjectSearchIndexSphinxBase::execute (  ) 

Execute search and return Entries DAO objects.

Reimplemented from DataObjectBase.

Definition at line 173 of file dataobject.searchindex.base.cls.php.

00173                                   {
00174                 $ret = array();
00175                 $log_fail = Config::has_feature(Config::LOG_FAILED_QUERIES);
00176                 try {
00177                         // Disable logging failed queries, since this is allowed to fail
00178                         Config::set_feature(Config::LOG_FAILED_QUERIES, false);
00179                         $found = $this->find();
00180                         Config::set_feature(Config::LOG_FAILED_QUERIES, $log_fail);
00181                 }
00182                 catch (Exception $ex) {
00183                         Config::set_feature(Config::LOG_FAILED_QUERIES, $log_fail);
00184                         // This is a fallback, if combination of operators caus an error 
00185                         $this->set_sphinx_feature(DBDriverSphinx::FEATURE_STRIP_OPERATORS, true);
00186                         $found = $this->find();
00187                 }       
00188                 if ($found) {
00189                         while ($this->fetch()) {
00190                                 $model = $this->resolve_model($this->item_model);
00191                                 $p = false;
00192                                 if ($model) {
00193                                         $p = DB::get_item_by_pk($model, $this->item_id);
00194                                 }
00195                                 if ($p) {
00196                                         $p->relevance_w = $this->relevance_w;
00197                                         $ret[] = $p;
00198                                 }
00199                         }
00200                 }
00201                 return $ret;
00202         }
DataObjectSearchIndexSphinxBase::extract_model_ids ( models  )  [protected]

Return an array of model ids for given models.

Definition at line 107 of file dataobject.searchindex.base.cls.php.

00107                                                       {
00108                 $ret = array();
00109                 foreach(Arr::force($models, false) as $model) {
00110                         $ret[] = SearchIndexRepository::get_model_id($model);
00111                 }
00112                 // Remove empty ids
00113                 $ret = array_filter($ret);
00114                 return $ret;
00115         } 
DataObjectSearchIndexSphinxBase::get_additional_field_definitions (  )  [protected]

Too be overloaded.

Return addition table fields

Returns:
array Array of IDBField

Definition at line 57 of file dataobject.searchindex.base.cls.php.

00057                                                               {
00058                 return array();
00059         }
DataObjectSearchIndexSphinxBase::get_sort_default_column (  ) 

Get the column to sort by default.

Reimplemented from DataObjectBase.

Definition at line 166 of file dataobject.searchindex.base.cls.php.

00166                                                   {
00167                 return 'relevance';
00168         }
DataObjectSearchIndexSphinxBase::get_sortable_columns (  ) 

Return array of sortable columns.

Array has column name as key and some sort of sort-column-object or an array as values

Reimplemented from DataObjectBase.

Definition at line 157 of file dataobject.searchindex.base.cls.php.

00157                                                {
00158                 return array(
00159                         'relevance' => new DBSortColumn('relevance_w', 'Relevanz', DBSortColumn::TYPE_MATCH, DBSortColumn::ORDER_FORWARD, true)
00160                 );
00161         }       
DataObjectSearchIndexSphinxBase::limit_to_models ( models  ) 

Include only given models in search.

Parameters:
string|array  Name of model or array of names of models

Implements ISearchIndex.

Definition at line 91 of file dataobject.searchindex.base.cls.php.

00091                                                  {
00092                 $include = $this->extract_model_ids($models);
00093                 switch(count($include)) {
00094                         case 0:
00095                                 // Ensure nothing is found!
00096                                 $this->add_where('item_model', '=', 0);
00097                                 break;
00098                         default:
00099                                 $this->add_where('item_model', DBWhere::OP_IN, $include);
00100                                 break;
00101                 }
00102         }
DataObjectSearchIndexSphinxBase::preprocess_query ( query  )  [protected]

Preprocess query string.

Parameters:
string  $query
Returns:
string

Definition at line 242 of file dataobject.searchindex.base.cls.php.

00242                                                     {
00243                 // replace "a-b" by "a b", like in 'ad-hoc'
00244                 $query = String::preg_replace('@(\w)\-@', '$1 ', $query);
00245                 return $query;
00246         }
DataObjectSearchIndexSphinxBase::resolve_model ( id  )  [protected]

Turn a model ID into a model name.

Definition at line 207 of file dataobject.searchindex.base.cls.php.

00207                                               {
00208                 return SearchIndexRepository::get_model_for_id($id);
00209         }
DataObjectSearchIndexSphinxBase::set_field_weights (  )  [protected]

Set weights for columns.

Definition at line 251 of file dataobject.searchindex.base.cls.php.

00251                                                {
00252                 $this->set_sphinx_feature(DBDriverSphinx::FEATURE_WEIGHTS, array('title' => 5, 'teaser' => 3, 'text' => 1));
00253         }
DataObjectSearchIndexSphinxBase::set_matching ( matching  ) 

Set matching mode (MATCH_WIDE or MATCH_NARROW).

Implements ISearchIndex.

Definition at line 127 of file dataobject.searchindex.base.cls.php.

00127                                                 {
00128                 $this->matching = $matching;
00129         }       
DataObjectSearchIndexSphinxBase::set_search ( search  ) 

Set the search string.

Parameters:
string  $search The search string

Implements ISearchIndex.

Definition at line 70 of file dataobject.searchindex.base.cls.php.

00070                                             {
00071                 $this->sphinx_all_fields = $search;
00072         }
DataObjectSearchIndexSphinxBase::sort_by_relevance (  ) 

Sort by Relevance.

Implements ISearchIndex.

Definition at line 120 of file dataobject.searchindex.base.cls.php.

00120                                             {
00121                 $this->sort('relevance_w', self::DESC);
00122         }

Member Data Documentation

DataObjectSearchIndexSphinxBase::$creationdate

Definition at line 18 of file dataobject.searchindex.base.cls.php.

DataObjectSearchIndexSphinxBase::$id

Definition at line 9 of file dataobject.searchindex.base.cls.php.

DataObjectSearchIndexSphinxBase::$item_id

Definition at line 10 of file dataobject.searchindex.base.cls.php.

DataObjectSearchIndexSphinxBase::$item_model

Definition at line 11 of file dataobject.searchindex.base.cls.php.

DataObjectSearchIndexSphinxBase::$matching = self::MATCH_NARROW [protected]

Definition at line 20 of file dataobject.searchindex.base.cls.php.

DataObjectSearchIndexSphinxBase::$meta

Definition at line 16 of file dataobject.searchindex.base.cls.php.

DataObjectSearchIndexSphinxBase::$modificationdate

Definition at line 17 of file dataobject.searchindex.base.cls.php.

DataObjectSearchIndexSphinxBase::$teaser

Definition at line 14 of file dataobject.searchindex.base.cls.php.

DataObjectSearchIndexSphinxBase::$text

Definition at line 15 of file dataobject.searchindex.base.cls.php.

DataObjectSearchIndexSphinxBase::$title

Definition at line 13 of file dataobject.searchindex.base.cls.php.


The documentation for this class was generated from the following file: