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

Set default language for TNTSearch #1065

Merged
merged 1 commit into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be some string that is less likely to be similar to no (for "Norwegian")?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I asked earlier and this is just how tntsearch expects it https://github.com/teamtnt/tntsearch/blob/13096686f81856addcd2eabc5eb73a470be36fb4/src/Engines/EngineTrait.php#L167-L171

I think the constant is the best we can get for that ;)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, thanks for the pointer. Then the PR is good to go 😊 Thanks for the contribution @Koc!


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
Loading