Skip to content

Commit

Permalink
Set default language for TNTSearch
Browse files Browse the repository at this point in the history
Signed-off-by: Konstantin Myakshin <[email protected]>
  • Loading branch information
Koc authored and mejo- committed Mar 6, 2024
1 parent 1ceb6d1 commit ce2f2b1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
24 changes: 23 additions & 1 deletion lib/Search/FileSearch/FileSearcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,31 @@ class FileSearcher extends TNTSearch {
'storage' => ''
];

public const SUPPORTED_LANGUAGES = [
'ar' => 'Arabic',
'cr' => 'Croatian',
'fr' => 'French',
'de' => 'German',
'en' => 'Porter',
'it' => 'Italian',
'lv' => 'Latvian',
'pl' => 'Polish',
'pt' => 'Portuguese',
'ru' => 'Russian',
'uk' => 'Ukrainian',
];

private const UNSUPPORTED_LANGUAGE = 'No';

protected FileIndexer $indexer;
private ?string $language;

public function __construct() {
public function __construct(?string $language = null) {
parent::__construct();
$this->loadConfig();
$this->asYouType(true);
$this->fuzziness(true);
$this->language = $language;
}

public function loadConfig(array $config = self::DEFAULT_CONFIG): void {
Expand Down Expand Up @@ -75,6 +93,8 @@ public function selectIndex($indexName): FileIndexer {
$this->index->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$this->indexer->setIndex($this->index);
$this->indexer->setLanguage($this->language ?? self::UNSUPPORTED_LANGUAGE);

return $this->indexer;
}

Expand All @@ -84,6 +104,8 @@ public function selectIndex($indexName): FileIndexer {
*/
public function createIndex($indexName = '', $disableOutput = false): FileIndexer {
$this->indexer->createIndex($indexName);
$this->indexer->setLanguage($this->language ?? self::UNSUPPORTED_LANGUAGE);

$this->index = $this->indexer->getIndex();
return $this->indexer;
}
Expand Down
18 changes: 13 additions & 5 deletions lib/Service/SearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use OCP\Files\InvalidPathException;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IConfig;
use OCP\ITempManager;
use OCP\Lock\LockedException;
use PDO;
Expand All @@ -22,9 +23,12 @@
class SearchService {
private const INDICES_DIR_NAME = 'indices';

public function __construct(private CollectiveFolderManager $collectiveFolderManager,
public function __construct(
private CollectiveFolderManager $collectiveFolderManager,
private ITempManager $tempManager,
private LoggerInterface $logger) {
private LoggerInterface $logger,
private IConfig $config,
) {
}

/**
Expand All @@ -39,8 +43,7 @@ public function indexCollective(Collective $collective): void {
throw new FileSearchException('Collectives search service could not find folder for collective.', 0, $e);
}

$searcher = new FileSearcher();
$indexer = $searcher->createIndex($indexPath);
$indexer = $this->createFileSearcher()->createIndex($indexPath);
$indexer->runOnDirectory($collectiveFolder);

$this->saveIndex($collective, $indexPath);
Expand All @@ -56,7 +59,7 @@ public function searchCollective(Collective $collective, string $term, int $maxR
return [];
}

$searcher = new FileSearcher();
$searcher = $this->createFileSearcher();
$file = $this->getIndexForCollective($collective);
if ($file === null) {
$this->logger->warning('Collectives search failed to find search index for collective with ID ' . $collective->getId());
Expand Down Expand Up @@ -114,6 +117,11 @@ private function getOrCreateIndexForCollective(Collective $collective): ?File {
return $file instanceof File ? $file : null;
}

private function createFileSearcher(): FileSearcher {
$defaultLanguage = $this->config->getSystemValue('default_language', 'en');
return new FileSearcher(FileSearcher::SUPPORTED_LANGUAGES[$defaultLanguage] ?? null);
}

/**
* @throws FileSearchException
*/
Expand Down

0 comments on commit ce2f2b1

Please sign in to comment.