Skip to content

Commit

Permalink
refactor: more like this
Browse files Browse the repository at this point in the history
  • Loading branch information
sitepark-veltrup committed Oct 2, 2024
1 parent f1b77a9 commit 003b247
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 25 deletions.
25 changes: 11 additions & 14 deletions src/Console/Command/MoreLikeThis.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Atoolo\Resource\ResourceChannel;
use Atoolo\Resource\ResourceLanguage;
use Atoolo\Resource\ResourceLocation;
use Atoolo\Search\Console\Command\Io\TypifiedInput;
use Atoolo\Search\Dto\Search\Query\MoreLikeThisQuery;
use Atoolo\Search\Dto\Search\Result\SearchResult;
Expand Down Expand Up @@ -39,9 +38,9 @@ protected function configure(): void
$this
->setHelp('Command to perform a more-like-this search')
->addArgument(
'location',
'id',
InputArgument::REQUIRED,
'Location of the resource to which the MoreLikeThis ' .
'Id of the resource to which the MoreLikeThis ' .
'search is to be applied.',
)
->addOption(
Expand All @@ -62,30 +61,28 @@ protected function execute(
$this->input = new TypifiedInput($input);
$this->io = new SymfonyStyle($input, $output);

$location = ResourceLocation::of(
$this->input->getStringArgument('location'),
ResourceLanguage::of(
$this->input->getStringOption('lang'),
),
$id = $this->input->getStringArgument('id');
$lang = ResourceLanguage::of(
$this->input->getStringOption('lang'),
);

$this->io->title('Channel: ' . $this->channel->name);

$query = $this->buildQuery($location);
$query = $this->buildQuery($id, $lang);
$result = $this->searcher->moreLikeThis($query);
$this->outputResult($result);

return Command::SUCCESS;
}

protected function buildQuery(
ResourceLocation $location,
): MoreLikeThisQuery {
protected function buildQuery(string $id, ResourceLanguage $lang): MoreLikeThisQuery
{
$filterList = [];
return new MoreLikeThisQuery(
location: $location,
filter: $filterList,
id: $id,
lang: $lang,
limit: 5,
filter: $filterList,
fields: ['content'],
);
}
Expand Down
7 changes: 4 additions & 3 deletions src/Dto/Search/Query/MoreLikeThisQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Atoolo\Search\Dto\Search\Query;

use Atoolo\Resource\ResourceLocation;
use Atoolo\Resource\ResourceLanguage;
use Atoolo\Search\Dto\Search\Query\Filter\Filter;

/**
Expand All @@ -25,9 +25,10 @@ class MoreLikeThisQuery
* which entries are similar.
*/
public function __construct(
public readonly ResourceLocation $location,
public readonly array $filter = [],
public readonly string $id,
public readonly ResourceLanguage $lang,
public readonly int $limit = 5,
public readonly array $filter = [],
public readonly array $fields = ['description', 'content'],
) {}
}
8 changes: 5 additions & 3 deletions src/Service/Search/SolrMoreLikeThis.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ public function __construct(

public function moreLikeThis(MoreLikeThisQuery $query): SearchResult
{
$index = $this->index->name($query->location->lang);
$index = $this->index->name($query->lang);
$client = $this->clientFactory->create($index);
$solrQuery = $this->buildSolrQuery($client, $query);
/** @var SolrMoreLikeThisResult $result */
$result = $client->execute($solrQuery);
return $this->buildResult($result, $query->location->lang);
return $this->buildResult($result, $query->lang);
}

private function buildSolrQuery(
Expand All @@ -44,13 +44,15 @@ private function buildSolrQuery(

$solrQuery = $client->createMoreLikeThis();
$solrQuery->setOmitHeader(false);
$solrQuery->setQuery('url:"' . $query->location . '"');
$solrQuery->setQuery('id:' . $query->id);
$solrQuery->setMltFields($query->fields);
$solrQuery->setRows($query->limit);
$solrQuery->setMinimumTermFrequency(2);
$solrQuery->setMatchInclude(true);

// Filter
$filterQuery = $solrQuery->createFilterQuery('self');
$filterQuery->setQuery('-id:' . $query->id);
$this->addFilterQueriesToSolrQuery($solrQuery, $query->filter);

return $solrQuery;
Expand Down
4 changes: 2 additions & 2 deletions test/Console/Command/MoreLikeThisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function testExecute(): void
->willReturn($result);

$this->commandTester->execute([
'location' => '/test.php',
'id' => '123',
]);

$this->commandTester->assertCommandIsSuccessful();
Expand Down Expand Up @@ -134,7 +134,7 @@ public function testExecuteNoResult(): void
->willReturn($result);

$this->commandTester->execute([
'location' => '/test.php',
'id' => '123',
]);

$this->commandTester->assertCommandIsSuccessful();
Expand Down
8 changes: 5 additions & 3 deletions test/Service/Search/SolrMoreLikeThisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Atoolo\Search\Test\Service\Search;

use Atoolo\Resource\Resource;
use Atoolo\Resource\ResourceLocation;
use Atoolo\Resource\ResourceLanguage;
use Atoolo\Search\Dto\Search\Query\Filter\ObjectTypeFilter;
use Atoolo\Search\Dto\Search\Query\MoreLikeThisQuery;
use Atoolo\Search\Service\IndexName;
Expand Down Expand Up @@ -72,8 +72,10 @@ public function testMoreLikeThis(): void
$filter = new ObjectTypeFilter(['test']);

$query = new MoreLikeThisQuery(
ResourceLocation::of('/test.php'),
[$filter],
id: '123',
lang: ResourceLanguage::default(),
limit: 5,
filter: [$filter],
);

$searchResult = $this->searcher->moreLikeThis($query);
Expand Down

0 comments on commit 003b247

Please sign in to comment.