Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.2] Smart Search: Add optimisations & garbage collection #36751

Merged
merged 20 commits into from May 27, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -11,8 +11,11 @@

\defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Controller\AdminController;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\Component\Finder\Administrator\Indexer\Indexer;

/**
* Index controller class for Finder.
Expand All @@ -37,6 +40,32 @@ public function getModel($name = 'Index', $prefix = 'Administrator', $config = a
return parent::getModel($name, $prefix, $config);
}

/**
* Method to optimise the index by removing orphaned entries.
*
* @return boolean True on success.
*
* @since __DEPLOY_VERSION__
*/
public function optimise()
{
$this->checkToken();

// Optimise the index by first running the garbage collection
$app = Factory::getApplication();
Hackwar marked this conversation as resolved.
Show resolved Hide resolved
PluginHelper::importPlugin('finder');
$app->triggerEvent('onFinderGarbageCollection');

// Now run the optimisation method from the indexer
$indexer = new Indexer;
$indexer->optimize();

$message = Text::_('COM_FINDER_INDEX_OPTIMISE_FINISHED');
$this->setRedirect('index.php?option=com_finder&view=index', $message);

return true;
}

/**
* Method to purge all indexed links from the database.
*
Expand Down
32 changes: 32 additions & 0 deletions administrator/components/com_finder/src/Indexer/Adapter.php
Expand Up @@ -262,6 +262,38 @@ public function onBuildIndex()
return true;
}

/**
* Method to remove outdated index entries
*
* @return integer
*
* @since __DEPLOY_VERSION__
*/
public function onFinderGarbageCollection()
{
$db = Factory::getDbo();
Hackwar marked this conversation as resolved.
Show resolved Hide resolved
$type_id = $this->getTypeId();

$query = $db->getQuery(true);
$subquery = $db->getQuery(true);
$subquery->select('CONCAT(' . $db->quote($this->getUrl('', $this->extension, $this->layout)) . ', id)')
->from($db->quoteName($this->table));
$query->select($db->quoteName('l.link_id'))
->from($db->quoteName('#__finder_links', 'l'))
->where($db->quoteName('l.type_id') . ' = ' . $type_id)
->where($db->quoteName('l.url') . ' LIKE ' . $db->quote($this->getUrl('%', $this->extension, $this->layout)))
->where($db->quoteName('l.url') . ' NOT IN (' . $subquery . ')');
$db->setQuery($query);
$items = $db->loadColumn();

foreach ($items as $item)
{
$this->indexer->remove($item);
}

return count($items);
}

/**
* Method to change the value of a content item's property in the links
* table. This is used to synchronize published and access states that
Expand Down
30 changes: 29 additions & 1 deletion administrator/components/com_finder/src/Indexer/Indexer.php
Expand Up @@ -713,7 +713,35 @@ public function optimize()
$db->setQuery($query);
$db->execute();

// Remove the orphaned taxonomy nodes.
// Delete all broken links. (Links missing the object)
$query = $db->getQuery(true)
->delete('#__finder_links')
->where($db->quoteName('object') . ' = ' . $db->quote(''));
$db->setQuery($query);
$db->execute();

// Delete all orphaned mappings of terms to links
$query2 = $db->getQuery(true)
->select($db->quoteName('link_id'))
->from($db->quoteName('#__finder_links'));
$query = $db->getQuery(true)
->delete($db->quoteName('#__finder_links_terms'))
->where($db->quoteName('link_id') . ' NOT IN (' . $query2 . ')');
$db->setQuery($query);
$db->execute();

// Delete all orphaned terms
$query2 = $db->getQuery(true)
->select($db->quoteName('term_id'))
->from($db->quoteName('#__finder_links_terms'));
$query = $db->getQuery(true)
->delete($db->quoteName('#__finder_terms'))
->where($db->quoteName('term_id') . ' NOT IN (' . $query2 . ')');
$db->setQuery($query);
$db->execute();

// Delete all orphaned taxonomies
Taxonomy::removeOrphanMaps();
Taxonomy::removeOrphanNodes();

// Optimize the tables.
Expand Down
27 changes: 26 additions & 1 deletion administrator/components/com_finder/src/Indexer/Taxonomy.php
Expand Up @@ -188,7 +188,7 @@ protected static function storeNode($node, $parentId)
* The database did not match the input. This could be because the
* state has changed or because the node does not exist. Let's figure
* out which case is true and deal with it.
* @todo: use factory?
* TODO: use factory?
Hackwar marked this conversation as resolved.
Show resolved Hide resolved
*/
$nodeTable = new MapTable($db);

Expand Down Expand Up @@ -385,6 +385,31 @@ public static function removeMaps($linkId)
return true;
}

/**
* Method to remove orphaned taxonomy maps
*
* @return integer The number of deleted rows.
*
* @since __DEPLOY_VERSION__
* @throws \RuntimeException on database error.
*/
public static function removeOrphanMaps()
{
// Delete all orphaned maps
$db = Factory::getDbo();
$query2 = $db->getQuery(true)
->select($db->quoteName('link_id'))
->from($db->quoteName('#__finder_links'));
$query = $db->getQuery(true)
->delete($db->quoteName('#__finder_taxonomy_map'))
->where($db->quoteName('link_id') . ' NOT IN (' . $query2 . ')');
$db->setQuery($query);
$db->execute();
$count = $db->getAffectedRows();

return $count;
}

/**
* Method to remove orphaned taxonomy nodes and branches.
*
Expand Down
20 changes: 17 additions & 3 deletions administrator/components/com_finder/src/View/Index/HtmlView.php
Expand Up @@ -202,13 +202,27 @@ protected function addToolbar()

if ($canDo->get('core.delete'))
{
ToolbarHelper::deleteList('', 'index.delete');
ToolbarHelper::divider();
$toolbar->confirmButton('', 'JTOOLBAR_DELETE', 'index.delete')
->icon('icon-delete')
->message('COM_FINDER_INDEX_CONFIRM_DELETE_PROMPT')
->listCheck(true);
$toolbar->divider();
}

if ($canDo->get('core.edit.state'))
{
ToolbarHelper::trash('index.purge', 'COM_FINDER_INDEX_TOOLBAR_PURGE', false);
$dropdown = $toolbar->dropdownButton('maintenance-group');
$dropdown->text('COM_FINDER_INDEX_TOOLBAR_MAINTENANCE')
->toggleSplit(false)
->icon('icon-wrench')
->buttonClass('btn btn-action');

$childBar = $dropdown->getChildToolbar();

$childBar->standardButton('cog', 'COM_FINDER_INDEX_TOOLBAR_OPTIMISE', 'index.optimise', false);
$childBar->confirmButton('index.purge', 'COM_FINDER_INDEX_TOOLBAR_PURGE', 'index.purge')
->icon('icon-trash')
->message('COM_FINDER_INDEX_CONFIRM_PURGE_PROMPT');
}
}

Expand Down
8 changes: 0 additions & 8 deletions administrator/components/com_finder/tmpl/index/default.php
Expand Up @@ -22,14 +22,6 @@
$listOrder = $this->escape($this->state->get('list.ordering'));
$listDirn = $this->escape($this->state->get('list.direction'));
$lang = Factory::getLanguage();

Text::script('COM_FINDER_INDEX_CONFIRM_PURGE_PROMPT');
Text::script('COM_FINDER_INDEX_CONFIRM_DELETE_PROMPT');

/** @var Joomla\CMS\WebAsset\WebAssetManager $wa */
$wa = $this->document->getWebAssetManager();
$wa->useScript('com_finder.index');

?>
<form action="<?php echo Route::_('index.php?option=com_finder&view=index'); ?>" method="post" name="adminForm" id="adminForm">
<div class="row">
Expand Down
3 changes: 3 additions & 0 deletions administrator/language/en-GB/com_finder.ini
Expand Up @@ -136,6 +136,7 @@ COM_FINDER_INDEX_HEADING_LINK_URL_ASC="Raw URL ascending"
COM_FINDER_INDEX_HEADING_LINK_URL_DESC="Raw URL descending"
COM_FINDER_INDEX_NO_CONTENT="No content matches your search criteria."
COM_FINDER_INDEX_NO_DATA="No content has been indexed."
COM_FINDER_INDEX_OPTIMISE_FINISHED="Optimisation finished."
COM_FINDER_INDEX_PLUGIN_CONTENT_NOT_ENABLED="The Smart Search Content Plugin is disabled. Changes to content will not update the Smart Search index until the Plugin is enabled."
COM_FINDER_INDEX_PLUGIN_CONTENT_NOT_ENABLED_LINK="The %s is disabled. Changes to content will not update the Smart Search index if you do not enable this plugin."
COM_FINDER_INDEX_PURGE_FAILED="Failed to delete selected items."
Expand All @@ -144,6 +145,8 @@ COM_FINDER_INDEX_SEARCH_DESC="Search in title, URL and last updated date."
COM_FINDER_INDEX_SEARCH_LABEL="Search Indexed Content"
COM_FINDER_INDEX_TABLE_CAPTION="Table of Indexed Content"
COM_FINDER_INDEX_TIP="Start the indexer by pressing the button below, or in the toolbar."
COM_FINDER_INDEX_TOOLBAR_MAINTENANCE="Maintenance"
COM_FINDER_INDEX_TOOLBAR_OPTIMISE="Optimise"
COM_FINDER_INDEX_TOOLBAR_PURGE="Clear Index"
COM_FINDER_INDEX_TOOLBAR_TITLE="Smart Search: Indexed Content"
COM_FINDER_INDEX_TYPE_FILTER="Any Type of Content"
Expand Down
23 changes: 0 additions & 23 deletions build/media_source/com_finder/joomla.asset.json
Expand Up @@ -84,29 +84,6 @@
"type": "module"
}
},
{
"name": "com_finder.index.es5",
"type": "script",
"uri": "com_finder/index-es5.min.js",
"dependencies": [
"core"
],
"attributes": {
"nomodule": true,
"defer": true
}
},
{
"name": "com_finder.index",
"type": "script",
"uri": "com_finder/index.min.js",
"dependencies": [
"com_finder.index.es5"
],
"attributes": {
"type": "module"
}
},
{
"name": "com_finder.indexer",
"type": "style",
Expand Down
25 changes: 0 additions & 25 deletions build/media_source/com_finder/js/index.es6.js

This file was deleted.