Skip to content

Commit

Permalink
feat: add background indexer
Browse files Browse the repository at this point in the history
  • Loading branch information
sitepark-veltrup committed Dec 7, 2023
1 parent 124de81 commit 68352eb
Show file tree
Hide file tree
Showing 13 changed files with 368 additions and 52 deletions.
29 changes: 22 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@
"prefer-stable": true,
"require": {
"php": ">=8.1 <8.4.0",
"atoolo/resource": "dev-feature/hierarchy-loader",
"atoolo/resource": "dev-feature/resource-base-locator",
"solarium/solarium": "^6.3",
"symfony/config": "^6.3",
"symfony/console": "^6.3",
"symfony/dependency-injection": "^6.3",
"symfony/event-dispatcher": "^6.3",
"symfony/finder": "^6.3",
"symfony/yaml": "^6.3"
"symfony/config": "^6.3 | ^7.0",
"symfony/console": "^6.3 | ^7.0",
"symfony/dependency-injection": "^6.3 | ^7.0",
"symfony/event-dispatcher": "^6.3 | ^7.0",
"symfony/finder": "^6.3 | ^7.0",
"symfony/yaml": "^6.3 | ^7.0",
"symfony/lock": "^6.3 | ^7.0"
},
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "^1.0",
Expand Down Expand Up @@ -77,5 +78,19 @@
"*": "dist"
},
"sort-packages": true
},
"extra": {
"composer-link": {
"atoolo/resource": {
"dev": false,
"version": "dev-feature/hierarchy-loader"
}
}
},
"repositories": {
"atoolo/resource": {
"type": "path",
"url": "/home/veltrup/.cache/composer/link/atoolo/resource"
}
}
}
59 changes: 38 additions & 21 deletions src/Console/Command/Indexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Atoolo\Resource\Exception\InvalidResourceException;
use Atoolo\Resource\Loader\SiteKitLoader;
use Atoolo\Resource\Loader\SiteKitNavigationHierarchyLoader;
use Atoolo\Resource\Loader\StaticResourceBaseLocator;
use Atoolo\Search\Console\Command\Io\IndexerProgressProgressBar;
use Atoolo\Search\Dto\Indexer\IndexerParameter;
use Atoolo\Search\Service\Indexer\SiteKit\DefaultSchema21DocumentEnricher;
Expand All @@ -28,12 +29,19 @@ class Indexer extends Command
{
private IndexerProgressProgressBar $progressBar;
private SymfonyStyle $io;

private InputInterface $input;
private string $resourceDir;

protected function configure(): void
{
$this
->setHelp('Command to fill a search index')
->addArgument(
'solr-connection-url',
InputArgument::REQUIRED,
'Solr connection url.'
)
->addArgument(
'solr-core',
InputArgument::REQUIRED,
Expand Down Expand Up @@ -66,16 +74,14 @@ protected function execute(
OutputInterface $output
): int {

$this->input = $input;
$this->io = new SymfonyStyle($input, $output);
$this->progressBar = new IndexerProgressProgressBar($output);
$this->resourceDir = $this->getStringArgument(
$input,
'resource-dir'
);
$this->resourceDir = $this->getStringArgument('resource-dir');
$directories = (array)$input->getArgument('directories');

$cleanupThreshold = empty($directories)
? $this->getIntArgument($input, 'cleanup-threshold')
? $this->getIntArgument('cleanup-threshold', 0)
: 0;

if (empty($directories)) {
Expand All @@ -86,8 +92,7 @@ protected function execute(
}

$parameter = new IndexerParameter(
$this->getStringArgument($input, 'solr-core'),
$this->resourceDir,
$this->getStringArgument('solr-core'),
$cleanupThreshold,
$directories
);
Expand All @@ -100,25 +105,23 @@ protected function execute(
return Command::SUCCESS;
}

private function getStringArgument(
InputInterface $input,
string $name
): string {
$value = $input->getArgument($name);
private function getStringArgument(string $name): string
{
$value = $this->input->getArgument($name);
if (!is_string($value)) {
throw new InvalidArgumentException(
$name . ' must be a string'
);
}
return strval($value);
return (string)$value;
return $value;
}

private function getIntArgument(
InputInterface $input,
string $name
): int {
$value = $input->getArgument($name);
private function getIntArgument(string $name, int $default): int
{
if (!$this->input->hasArgument($name)) {
return $default;
}
$value = $this->input->getArgument($name);
if (!is_int($value)) {
throw new InvalidArgumentException(
$name . ' must be a integer'
Expand All @@ -143,18 +146,32 @@ protected function errorReport(): void

protected function createIndexer(): SolrIndexer
{
$resourceLoader = new SiteKitLoader($this->resourceDir);
$resourceBaseLocator = new StaticResourceBaseLocator(
$this->resourceDir
);
$resourceLoader = new SiteKitLoader($resourceBaseLocator);
$navigationLoader = new SiteKitNavigationHierarchyLoader(
$resourceLoader
);
$schema21 = new DefaultSchema21DocumentEnricher(
$navigationLoader
);

$clientFactory = new SolrParameterClientFactory();
$url = parse_url($this->getStringArgument('solr-connection-url'));

$clientFactory = new SolrParameterClientFactory(
$url['scheme'],
$url['host'],
$url['port'] ?? ($url['scheme'] === 'https' ? 443 : 8382),
$url['path'] ?? '',
null,
0
);

return new SolrIndexer(
[$schema21],
$this->progressBar,
$resourceBaseLocator,
$resourceLoader,
$clientFactory,
'internal'
Expand Down
23 changes: 21 additions & 2 deletions src/Console/Command/MoreLikeThis.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Atoolo\Search\Console\Command;

use Atoolo\Resource\Loader\SiteKitLoader;
use Atoolo\Resource\Loader\StaticResourceBaseLocator;
use Atoolo\Search\Dto\Search\Query\MoreLikeThisQuery;
use Atoolo\Search\Dto\Search\Result\ResourceSearchResult;
use Atoolo\Search\Service\Search\ExternalResourceFactory;
Expand All @@ -28,13 +29,19 @@
class MoreLikeThis extends Command
{
private SymfonyStyle $io;
private InputInterface $input;
private string $solrCore;
private string $resourceDir;

protected function configure(): void
{
$this
->setHelp('Command to performs a more-like-this search')
->addArgument(
'solr-connection-url',
InputArgument::REQUIRED,
'Solr connection url.'
)
->addArgument(
'solr-core',
InputArgument::REQUIRED,
Expand All @@ -58,6 +65,7 @@ protected function execute(
OutputInterface $output
): int {

$this->input = $input;
$this->io = new SymfonyStyle($input, $output);

$this->solrCore = $input->getArgument('solr-core');
Expand All @@ -74,8 +82,19 @@ protected function execute(

protected function createSearcher(): SolrMoreLikeThis
{
$resourceLoader = new SiteKitLoader($this->resourceDir);
$clientFactory = new SolrParameterClientFactory();
$resourceBaseLocator = new StaticResourceBaseLocator(
$this->resourceDir
);
$resourceLoader = new SiteKitLoader($resourceBaseLocator);
$url = parse_url($this->input->getArgument('solr-connection-url'));
$clientFactory = new SolrParameterClientFactory(
$url['scheme'],
$url['host'],
$url['port'] ?? ($url['scheme'] === 'https' ? 443 : 8983),
$url['path'] ?? '',
null,
0
);
$resourceFactoryList = [
new ExternalResourceFactory(),
new InternalResourceFactory($resourceLoader),
Expand Down
18 changes: 16 additions & 2 deletions src/Console/Command/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Atoolo\Search\Console\Command;

use Atoolo\Resource\Loader\SiteKitLoader;
use Atoolo\Resource\Loader\StaticResourceBaseLocator;
use Atoolo\Search\Dto\Search\Query\SelectQuery;
use Atoolo\Search\Dto\Search\Result\ResourceSearchResult;
use Atoolo\Search\Service\Search\ExternalResourceFactory;
Expand All @@ -28,6 +29,7 @@
class Search extends Command
{
private SymfonyStyle $io;
private InputInterface $input;
private string $index;
private string $resourceDir;

Expand Down Expand Up @@ -59,6 +61,7 @@ protected function execute(
OutputInterface $output
): int {

$this->input = $input;
$this->io = new SymfonyStyle($input, $output);
$this->resourceDir = $input->getArgument('resource-dir');
$this->index = $input->getArgument('index');
Expand All @@ -75,8 +78,19 @@ protected function execute(

protected function createSearch(): SolrSelect
{
$resourceLoader = new SiteKitLoader($this->resourceDir);
$clientFactory = new SolrParameterClientFactory();
$resourceBaseLocator = new StaticResourceBaseLocator(
$this->resourceDir
);
$resourceLoader = new SiteKitLoader($resourceBaseLocator);
$url = parse_url($this->input->getArgument('solr-connection-url'));
$clientFactory = new SolrParameterClientFactory(
$url['scheme'],
$url['host'],
$url['port'] ?? ($url['scheme'] === 'https' ? 443 : 8983),
$url['path'] ?? '',
null,
0
);
$defaultBoosting = new DefaultBoostModifier();

$resourceFactoryList = [
Expand Down
14 changes: 12 additions & 2 deletions src/Console/Command/Suggest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
)]
class Suggest extends Command
{
private InputInterface $input;
private SymfonyStyle $io;
private string $solrCore;

Expand All @@ -47,7 +48,7 @@ protected function execute(
InputInterface $input,
OutputInterface $output
): int {

$this->input = $input;
$this->io = new SymfonyStyle($input, $output);
$this->solrCore = $input->getArgument('solr-core');
$terms = $input->getArgument('terms');
Expand All @@ -65,6 +66,15 @@ protected function execute(
protected function createSearcher(): SolrSuggest
{
$clientFactory = new SolrParameterClientFactory();
$url = parse_url($this->input->getArgument('solr-connection-url'));
$clientFactory = new SolrParameterClientFactory(
$url['scheme'],
$url['host'],
$url['port'] ?? ($url['scheme'] === 'https' ? 443 : 8983),
$url['path'] ?? '',
null,
0
);
return new SolrSuggest($clientFactory);
}

Expand All @@ -74,7 +84,7 @@ protected function buildQuery(array $terms): SuggestQuery
$excludeMedia = $excludeMedia->exclude();
return new SuggestQuery(
$this->solrCore,
$terms,
implode(' ', $terms),
[
new ArchiveFilter(),
$excludeMedia
Expand Down
3 changes: 1 addition & 2 deletions src/Dto/Indexer/IndexerParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
class IndexerParameter
{
public function __construct(
public readonly string $coreId,
public readonly string $basePath,
public readonly string $index,
public readonly int $cleanupThreshold = 0,
public readonly array $directories = []
) {
Expand Down
5 changes: 4 additions & 1 deletion src/Indexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@
*/
interface Indexer
{
public function index(IndexerParameter $parameter): void;
/**
* @return string process id
*/
public function index(IndexerParameter $parameter): string;
}
Loading

0 comments on commit 68352eb

Please sign in to comment.