Skip to content

Commit

Permalink
Немного переделан омдуль поиска, теперь для поиска данных используетс…
Browse files Browse the repository at this point in the history
…я отдельный запрос и возвращаются ID записей
  • Loading branch information
butschster committed Dec 1, 2014
1 parent 9a03580 commit 01087df
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 63 deletions.
24 changes: 17 additions & 7 deletions cms/modules/datasource/classes/datasource/section/headline.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,28 +231,38 @@ abstract public function count_total( array $ids = NULL );
* @param Database_Query $query
* @return Database_Query
*/
public function search_by_keyword( Database_Query $query )
public function search_by_keyword(Database_Query $query)
{
$keyword = Request::initial()->query('keyword');

if(empty($keyword))
if (empty($keyword))
{
return $query;
}

if($this->_section->is_indexable())
if ($this->_section->is_indexable())
{
$query = Search::instance()->get_module_query($query, $keyword, 'ds_' . $this->_section->id());
$ids = Search::instance()->find_by_keyword($keyword, FALSE, 'ds_' . $this->_section->id(), NULL);
$ids = Arr::get($ids, 'ds_' . $this->_section->id());

if(!empty($ids))
{
$query->where('d.id', 'in', array_keys($ids));
}
else
{
$query->where('d.id', '=', 0);
}
}
else
{
$query
->where_open()
->where('d.id', 'like', '%'.$keyword.'%')
->or_where('d.header', 'like', '%'.$keyword.'%')
->where('d.id', 'like', '%' . $keyword . '%')
->or_where('d.header', 'like', '%' . $keyword . '%')
->where_close();
}

return $query;
}

Expand Down
39 changes: 31 additions & 8 deletions cms/modules/search/classes/kodicms/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,17 @@ public static function instance($group = NULL)

$config = Kohana::$config->load('search');

if ( ! $config->offsetExists($group))
if (!$config->offsetExists($group))
{
throw new Kohana_Exception(
'Failed to load Kohana Search group: :group',
array(':group' => $group)
);
'Failed to load Kohana Search group: :group', array(
':group' => $group));
}

$config = $config->get($group);

// Create a new search type instance
$search_class = 'Search_'.ucfirst($config['driver']);
$search_class = 'Search_' . ucfirst($config['driver']);
Search::$instances[$group] = new $search_class($config);

// Return the instance
Expand Down Expand Up @@ -87,7 +86,9 @@ protected function __construct(array $config)
public function config($key = NULL, $value = NULL)
{
if ($key === NULL)
{
return $this->_config;
}

if (is_array($key))
{
Expand All @@ -96,7 +97,9 @@ public function config($key = NULL, $value = NULL)
else
{
if ($value === NULL)
{
return Arr::get($this->_config, $key);
}

$this->_config[$key] = $value;
}
Expand Down Expand Up @@ -124,13 +127,33 @@ final public function __clone()
* @param array $params
* @return bool
*/
abstract public function add_to_index( $module, $id, $title, $content = '', $annotation, $params = array() );
abstract public function add_to_index($module, $id, $title, $content = '', $annotation, $params = array());

/**
*
* @param string $module
* @param integer $id
* @return bool
*/
abstract public function remove_from_index( $module, $id = NULL );
abstract public function remove_from_index($module, $id = NULL);

/**
*
* @param string $keyword
* @param boolean $only_title
* @param string|array $modules
* @param integer $limit
* @param integer $offset
* @return array
*/
abstract public function find_by_keyword($keyword, $only_title = FALSE, $modules = NULL, $limit = 50, $offset = 0);

/**
*
* @param string $keyword
* @param boolean $only_title
* @param string $modules
* @return integer
*/
abstract public function count_by_keyword($keyword, $only_title = FALSE, $modules = NULL);
}
92 changes: 50 additions & 42 deletions cms/modules/search/classes/kodicms/search/mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,26 @@ public function stem_query($keyword)
$result = '';

$text = UTF8::strtolower($keyword);
$text = trim( $text );
$text = strip_tags( $text );
$text = trim($text);
$text = strip_tags($text);

$stop_words = Model_Search_Stopwords::get();

// Parse original text and stem all words that are not tags
$tkn = new Model_Search_Tokenizer();
$tkn->set_text( $text );
$tkn->set_text($text);
$tkn->stopwords = $stop_words;

$stemmer = new Model_Search_Stemmer($tkn);

while ( $cur = $tkn->next() )
while ($cur = $tkn->next())
{
$result .= $stemmer->stem($cur);
}

return $result;
}

/**
*
* @param string $keyword
Expand All @@ -49,7 +49,7 @@ public function stem_query($keyword)
* @param integer $offset
* @return array
*/
public function find_by_keyword( $keyword, $only_title = FALSE, $modules = NULL, $limit = 50, $offset = 0)
public function find_by_keyword($keyword, $only_title = FALSE, $modules = NULL, $limit = 50, $offset = 0)
{
if (Kohana::$profiling === TRUE)
{
Expand All @@ -68,7 +68,7 @@ public function find_by_keyword( $keyword, $only_title = FALSE, $modules = NULL,
foreach($result as $row)
{
$row['params'] = Kohana::unserialize($row['params']);
$ids[$row['module']][] = $row;
$ids[$row['module']][$row['id']] = $row;
}

if (isset($benchmark))
Expand All @@ -86,12 +86,12 @@ public function find_by_keyword( $keyword, $only_title = FALSE, $modules = NULL,
* @param string $modules
* @return integer
*/
public function count_by_keyword( $keyword, $only_title = FALSE, $modules = NULL )
public function count_by_keyword($keyword, $only_title = FALSE, $modules = NULL)
{
$query = DB::select(array(DB::expr('COUNT(*)'), 'total'))
->from('search_index');
return $this->_get_query( $query, $keyword, $only_title, $modules, NULL )

return $this->_get_query($query, $keyword, $only_title, $modules, NULL)
->execute()
->get('total');
}
Expand All @@ -105,35 +105,35 @@ public function match_against_query($keyword)
{
$keyword = trim(strtolower($keyword));
$result = '>"' . $keyword . '"<';

$words = explode(' ', $keyword);

if(count($words) > 1 )
if (count($words) > 1)
{
$result .= '(';
foreach ($words as $word )
foreach ($words as $word)
{
$result .= '+' . $word;
}

$result .= ') ';
}

return $result;
}

/**
*
* @param Database_Query $query
* @param string $keyword
* @param string $module
* @param bool $only_title
*/
public function get_module_query( Database_Query $query, $keyword, $module, $only_title = FALSE )
public function get_module_query(Database_Query $query, $keyword, $module, $only_title = FALSE)
{
return $this->_get_query($query, $keyword, $only_title, $module, NULL)
->join('search_index', 'left')
->on('search_index.id', '=', 'd.id');
->on('search_index.id', '=', 'd.id');
}

/**
Expand All @@ -150,51 +150,58 @@ protected function _get_query(Database_Query $query, $keyword, $only_title = FAL
{
$keyword = $this->stem_query($keyword);

if($limit !== NULL)
if ($limit !== NULL)
{
$query
->limit((int) $limit)
->offset($offset);
}
if(is_array($modules))

if (is_array($modules))
{
$query->where('search_index.module', 'in', $modules);
}
elseif ($modules !== NULL)
elseif ($modules !== NULL)
{
$query->where('search_index.module', '=', $modules);
}
if($this->config('full_text_search') === TRUE)

if ($this->config('full_text_search') === TRUE)
{
$query->where(DB::expr('MATCH(`search_index`.`header`, `search_index`.`content`)'), 'AGAINST', DB::expr("('".self::match_against_query($keyword)."' IN BOOLEAN MODE)"));
$query->where(DB::expr('MATCH(`search_index`.`header`, `search_index`.`content`)'), 'AGAINST', DB::expr("('" . self::match_against_query($keyword) . "' IN BOOLEAN MODE)"));
}
else
{
$words = explode(' ', $keyword);

$query->where_open();
$query->where_open();
$query->where_open();

foreach ($words as $word )
foreach ($words as $word)
{
if (!empty($word))
{
if(!empty($word))
$query->where('search_index.header', 'like', '%'.$word.'%');
$query->where('search_index.header', 'like', '%' . $word . '%');
}
}

$query->where_close();
$query->where_close();

if($only_title === FALSE)
if ($only_title === FALSE)
{
$query->or_where_open();

foreach ($words as $word)
{
$query->or_where_open();
foreach ($words as $word )
if (!empty($word))
{
if(!empty($word))
$query->where('search_index.content', 'like', '%'.$word.'%');
$query->where('search_index.content', 'like', '%' . $word . '%');
}
$query->where_close();
}

$query->where_close();
}

$query->where_close();
}

Expand All @@ -210,21 +217,22 @@ protected function _get_query(Database_Query $query, $keyword, $only_title = FAL
* @param array $params
* @return bool
*/
public function add_to_index( $module, $id, $title, $content = '', $annotation, $params = array() )
public function add_to_index($module, $id, $title, $content = '', $annotation, $params = array())
{
$indexer = new Model_Search_Indexer;
return $indexer->add($module, $id, $title, $content, $annotation, $params);
}

/**
*
* @param string $module
* @param integer $id
* @return bool
*/
public function remove_from_index( $module, $id = NULL )
public function remove_from_index($module, $id = NULL)
{
$indexer = new Model_Search_Indexer;
return $indexer->remove( $module, $id );
return $indexer->remove($module, $id);
}

}
7 changes: 5 additions & 2 deletions cms/modules/search/classes/model/widget/page/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@ public function fetch_data()

$ids = Search::instance()->find_by_keyword($keyword, FALSE, 'pages', $this->list_size, $this->list_offset);

if(empty($ids['pages'])) return $return;

if(empty($ids['pages']))
{
return $return;
}

$pages = array();
foreach ($ids['pages'] as $item)
{
Expand Down
22 changes: 18 additions & 4 deletions cms/plugins/hybrid/classes/model/widget/hybrid/headline.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,15 +276,29 @@ protected function _parse_doc_id()
*
* @param Database_Query $query
*/
protected function _search_by_keyword( Database_Query $query )
protected function _search_by_keyword(Database_Query $query, $only_title = FALSE)
{
if($this->search_key === NULL OR trim($this->search_key) == '') return $query;
if ($this->search_key === NULL OR trim($this->search_key) == '')
{
return $query;
}

$keyword = $this->_ctx->get($this->search_key);

if (empty($keyword))
{
return $query;
}

if( empty($keyword) )return $query;
$ids = Search::instance()->find_by_keyword($keyword, $only_title, 'ds_' . $this->ds_id, NULL);

$ids = Arr::get($ids, 'ds_' . $this->ds_id);
if (!empty($ids))
{
return $query->where('id', 'in', array_keys($ids));
}

return Search::instance()->get_module_query($query, $keyword, 'ds_' . $this->ds_id);
return $query;
}

/**
Expand Down

0 comments on commit 01087df

Please sign in to comment.