-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #193 from tpwd/feature/issue-19-word-delimiter
[FEATURE] Additional word characters
- Loading branch information
Showing
9 changed files
with
139 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tpwd\KeSearch\Utility; | ||
|
||
use Tpwd\KeSearch\Lib\SearchHelper; | ||
|
||
class AdditionalWordCharactersUtility | ||
{ | ||
public static function getAdditionalWordCharacters(): array | ||
{ | ||
$extConf = SearchHelper::getExtConf(); | ||
$additionalWordCharacters = []; | ||
if (!empty($extConf['additionalWordCharacters'] ?? '')) { | ||
foreach (str_split($extConf['additionalWordCharacters']) as $char) { | ||
$additionalWordCharacters[] = $char; | ||
} | ||
} | ||
return $additionalWordCharacters; | ||
} | ||
|
||
public static function getAdditionalContent(string $content): string | ||
{ | ||
$additionalWordCharacters = self::getAdditionalWordCharacters(); | ||
if (empty($additionalWordCharacters)) { | ||
return ''; | ||
} | ||
$additionalContent = ''; | ||
foreach ($additionalWordCharacters as $additionalWordCharacter) { | ||
$matches = []; | ||
$pattern = '/(?=(?:[^\s]*[' . $additionalWordCharacter . ']){1,})\S+/'; | ||
preg_match($pattern, $content, $matches); | ||
if ($matches) { | ||
if (!empty($additionalContent)) { | ||
$additionalContent .= ' '; | ||
} | ||
$additionalContent .= str_replace( | ||
$additionalWordCharacter, | ||
self::getReplacementForAdditionalWordCharacter($additionalWordCharacter), | ||
implode(' ', $matches) | ||
); | ||
} | ||
} | ||
return $additionalContent; | ||
} | ||
|
||
public static function replaceAdditionalWordCharacters(string $content): string | ||
{ | ||
$additionalWordCharacters = self::getAdditionalWordCharacters(); | ||
if (empty($additionalWordCharacters)) { | ||
return ''; | ||
} | ||
foreach ($additionalWordCharacters as $additionalWordCharacter) { | ||
$content = str_replace( | ||
$additionalWordCharacter, | ||
self::getReplacementForAdditionalWordCharacter($additionalWordCharacter), | ||
$content | ||
); | ||
} | ||
return $content; | ||
} | ||
|
||
public static function getReplacementForAdditionalWordCharacter(string $character): string | ||
{ | ||
return '___' . ord($character) . '___'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
.. include:: /Includes.rst.txt | ||
|
||
.. _configuration-additional-word-characters: | ||
|
||
========================== | ||
Additional word characters | ||
========================== | ||
|
||
By default MySQL treats certain characters as word delimiters, e.g. dot (".") | ||
and hyphen ("-"). That means words which contain one of these characters will be | ||
treated as two words and it is not possible to search for such a word. | ||
|
||
But in some cases it would be helpful to be able to search for such words, e.g. | ||
serial numbers which contain a hyphen, e.g. "AB-123". | ||
|
||
Since version 5.1.0 it is possible to make this words searchable. Go to the | ||
extension settings and add the desired characters in "Additional word | ||
characters". You can add multiple characters there. | ||
|
||
.. figure:: /Images/Configuration/additional-word-characters-configuration.png | ||
:alt: Configure additional word characters | ||
:class: with-border | ||
|
||
After that you will have to start the indexer. | ||
|
||
Words containing the configured characters can then be searched. | ||
|
||
.. figure:: /Images/Configuration/additional-word-characters-result.png | ||
:alt: Search result with additional word characters | ||
:class: with-border |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+18.3 KB
Documentation/Images/Configuration/additional-word-characters-configuration.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+14.7 KB
Documentation/Images/Configuration/additional-word-characters-result.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters