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: Allow fuzzy word matching #36752

Merged
merged 23 commits into from
Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
c0c7655
Finder: Allow fuzzy word matching
Hackwar Jan 19, 2022
4c7ccd4
Update administrator/components/com_finder/config.xml
Hackwar Jan 20, 2022
c5f23d9
Update administrator/components/com_finder/src/Indexer/Query.php
Hackwar Jan 20, 2022
79d0d38
Reverting accidental change of todo
Hackwar Jan 20, 2022
6ab9cea
Reverting accidental deletion
Hackwar Jan 20, 2022
b7317b0
Merge branch '4.1-dev' into 4.2-finder-wordmatch
Hackwar Jan 20, 2022
25d4cee
Update administrator/components/com_finder/src/Indexer/Query.php
Hackwar Jan 24, 2022
13a25db
Merge branch '4.2-dev' into 4.2-finder-wordmatch
Hackwar Jan 24, 2022
cfbf6ef
Using strings to distinguish word matching modes
Hackwar Jan 26, 2022
09d5e1f
Update administrator/components/com_finder/config.xml
Hackwar Jan 26, 2022
c3f8e17
Update administrator/language/en-GB/com_finder.ini
Hackwar Jan 27, 2022
03e46d6
Update administrator/language/en-GB/com_finder.ini
Hackwar Jan 27, 2022
7e166c7
Update Query.php
Hackwar Feb 3, 2022
69f9152
Merge branch '4.2-dev' into 4.2-finder-wordmatch
Hackwar Feb 3, 2022
a51a36a
Update administrator/language/en-GB/com_finder.ini
Hackwar Feb 6, 2022
3dfada0
Update administrator/language/en-GB/com_finder.ini
Hackwar Feb 6, 2022
59f251b
Merge branch '4.2-dev' into 4.2-finder-wordmatch
richard67 Mar 10, 2022
7e56d0b
Merge branch '4.2-dev' into 4.2-finder-wordmatch
Hackwar Apr 2, 2022
f96ccc4
Merge branch '4.2-dev' of https://github.com/joomla/joomla-cms into 4…
Hackwar May 17, 2022
a1d1b4f
Fixing SQL
Hackwar May 17, 2022
f3a3c8f
Using prepared statements
Hackwar May 20, 2022
c809c80
Merge branch '4.2-dev' into 4.2-finder-wordmatch
Hackwar May 20, 2022
e0c7bd8
Merge branch '4.2-dev' into 4.2-finder-wordmatch
roland-d May 20, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion administrator/components/com_finder/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,18 @@
<fieldset
name="search"
label="COM_FINDER_FIELDSET_SEARCH_OPTIONS_LABEL"
description="COM_FINDER_FIELDSET_SEARCH_OPTIONS_DESCRIPTION"
>
<field
name="word_match"
type="list"
label="COM_FINDER_CONFIG_WORD_MATCH_LABEL"
description="COM_FINDER_CONFIG_WORD_MATCH_DESC"
default="0"
>
<option value="exact">COM_FINDER_CONFIG_WORD_MATCH_OPTION_EXACT</option>
<option value="begin">COM_FINDER_CONFIG_WORD_MATCH_OPTION_BEGIN</option>
<option value="fuzzy">COM_FINDER_CONFIG_WORD_MATCH_OPTION_FUZZY</option>
</field>

<field
name="show_taxonomy"
Expand Down
30 changes: 28 additions & 2 deletions administrator/components/com_finder/src/Indexer/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,14 @@ class Query
*/
public $when2;

/**
* Match search terms exactly or with a LIKE scheme
*
* @var string
* @since __DEPLOY_VERSION__
*/
public $wordmode;

/**
* Method to instantiate the query object.
*
Expand All @@ -196,6 +204,9 @@ public function __construct($options)
// Get the matching mode.
$this->mode = 'AND';

// Set the word matching mode
$this->wordmode = !empty($options['word_match']) ? $options['word_match'] : 'exact';

// Initialize the temporary date storage.
$this->dates = new Registry;

Expand Down Expand Up @@ -1343,8 +1354,23 @@ protected function getTokenData($token)
else
{
// Add the term to the query.
$query->where('(t.term = ' . $db->quote($token->term) . ' OR t.stem = ' . $db->quote($token->stem) . ')')
->where('t.phrase = 0')

$searchTerm = $token->term;
$searchStem = $token->stem;

if ($this->wordmode == 'begin')
{
$searchTerm .= '%';
$searchStem .= '%';
}
elseif ($this->wordmode == 'fuzzy')
{
$searchTerm = '%' . $searchTerm . '%';
$searchStem = '%' . $searchStem . '%';
}

$query->where('(t.term = ' . $db->quote($searchTerm) . ' OR t.stem = ' . $db->quote($searchStem) . ')');
$query->where('t.phrase = 0')
->where('t.language IN (\'*\',' . $db->quote($token->language) . ')');
}

Expand Down
8 changes: 6 additions & 2 deletions administrator/language/en-GB/com_finder.ini
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ COM_FINDER_CONFIG_TITLE_MULTIPLIER_LABEL="Title Text Weight Multiplier"
COM_FINDER_CONFIG_TUPLECOUNT_LABEL="Search for Phrases"
COM_FINDER_CONFIG_TUPLECOUNT_PHRASE_DISABLED="Disabled (Improved performance)"
COM_FINDER_CONFIG_TUPLECOUNT_PHRASE_ENABLED="Enabled (Improved search results)"
COM_FINDER_CONFIG_WORD_MATCH_DESC="This option allows to set how search terms are matched in the index. By default a word is matched exactly, but when a language supports compound words, this allows to match the search term to the beginning or in a random place inside of words in the index."
COM_FINDER_CONFIG_WORD_MATCH_LABEL="Word Match"
COM_FINDER_CONFIG_WORD_MATCH_OPTION_EXACT="Match exactly"
COM_FINDER_CONFIG_WORD_MATCH_OPTION_BEGIN="Also match words beginning with the search term"
COM_FINDER_CONFIG_WORD_MATCH_OPTION_FUZZY="Match words containing the search term anywhere"
COM_FINDER_CONFIGURATION="Smart Search: Options"
COM_FINDER_CONTENT_PLUGIN="Smart Search Content Plugin"
COM_FINDER_CREATE_FILTER="Create a filter."
Expand All @@ -65,9 +70,8 @@ COM_FINDER_EMPTYSTATE_CONTENT="No content has been indexed or you have deleted a
COM_FINDER_EMPTYSTATE_SEARCHES_CONTENT="There are no phrases used for site searching to view yet."
COM_FINDER_FIELD_CREATED_BY_ALIAS_LABEL="Alias"
COM_FINDER_FIELD_CREATED_BY_LABEL="Created By"
COM_FINDER_FIELDSET_INDEX_OPTIONS_DESCRIPTION="Indexing options"
COM_FINDER_FIELDSET_INDEX_OPTIONS_DESCRIPTION="These options influence how the content is indexed. After changing settings here, the index needs to be rebuild."
COM_FINDER_FIELDSET_INDEX_OPTIONS_LABEL="Index"
COM_FINDER_FIELDSET_SEARCH_OPTIONS_DESCRIPTION="Smart Search options"
COM_FINDER_FIELDSET_SEARCH_OPTIONS_LABEL="Smart Search"
COM_FINDER_FILTER_BRANCH_LABEL="Search by %s"
COM_FINDER_FILTER_EDIT_TOOLBAR_TITLE="Smart Search: Edit Filter"
Expand Down
64 changes: 3 additions & 61 deletions components/com_finder/src/Model/SearchModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,9 @@ protected function populateState($ordering = null, $direction = null)
// Get the query language.
$options['language'] = $request->getCmd('l', $params->get('l', $language->getTag()));

// Set the word match mode
$options['word_match'] = $params->get('word_match', 'exact');

// Get the start date and start date modifier filters.
$options['date1'] = $request->getString('d1', $params->get('d1', ''));
$options['when1'] = $request->getString('w1', $params->get('w1', ''));
Expand Down Expand Up @@ -487,65 +490,4 @@ protected function populateState($ordering = null, $direction = null)
$this->setState('user.id', (int) $user->get('id'));
$this->setState('user.groups', $user->getAuthorisedViewLevels());
}

/**
* Method to retrieve data from cache.
*
* @param string $id The cache store id.
* @param boolean $persistent Flag to enable the use of external cache. [optional]
*
* @return mixed The cached data if found, null otherwise.
*
* @since 2.5
*/
protected function retrieve($id, $persistent = true)
{
$data = null;

// Use the internal cache if possible.
if (isset($this->cache[$id]))
{
return $this->cache[$id];
}

// Use the external cache if data is persistent.
if ($persistent)
{
$data = Factory::getCache($this->context, 'output')->get($id);
$data = $data ? unserialize($data) : null;
}

// Store the data in internal cache.
if ($data)
{
$this->cache[$id] = $data;
}

return $data;
}

/**
* Method to store data in cache.
*
* @param string $id The cache store id.
* @param mixed $data The data to cache.
* @param boolean $persistent Flag to enable the use of external cache. [optional]
*
* @return boolean True on success, false on failure.
*
* @since 2.5
*/
protected function store($id, $data, $persistent = true)
{
// Store the data in internal cache.
$this->cache[$id] = $data;

// Store the data in external cache if data is persistent.
if ($persistent)
{
return Factory::getCache($this->context, 'output')->store(serialize($data), $id);
}

return true;
}
}