From 003b2477b81e77e241dea6a8fc9b4d3c93953fa1 Mon Sep 17 00:00:00 2001 From: Holger Veltrup Date: Wed, 2 Oct 2024 17:14:22 +0200 Subject: [PATCH] refactor: more like this --- src/Console/Command/MoreLikeThis.php | 25 +++++++++----------- src/Dto/Search/Query/MoreLikeThisQuery.php | 7 +++--- src/Service/Search/SolrMoreLikeThis.php | 8 ++++--- test/Console/Command/MoreLikeThisTest.php | 4 ++-- test/Service/Search/SolrMoreLikeThisTest.php | 8 ++++--- 5 files changed, 27 insertions(+), 25 deletions(-) diff --git a/src/Console/Command/MoreLikeThis.php b/src/Console/Command/MoreLikeThis.php index 226ccd1..9d81d79 100644 --- a/src/Console/Command/MoreLikeThis.php +++ b/src/Console/Command/MoreLikeThis.php @@ -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; @@ -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( @@ -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'], ); } diff --git a/src/Dto/Search/Query/MoreLikeThisQuery.php b/src/Dto/Search/Query/MoreLikeThisQuery.php index d80d728..aa04368 100644 --- a/src/Dto/Search/Query/MoreLikeThisQuery.php +++ b/src/Dto/Search/Query/MoreLikeThisQuery.php @@ -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; /** @@ -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'], ) {} } diff --git a/src/Service/Search/SolrMoreLikeThis.php b/src/Service/Search/SolrMoreLikeThis.php index c926ee1..267a417 100644 --- a/src/Service/Search/SolrMoreLikeThis.php +++ b/src/Service/Search/SolrMoreLikeThis.php @@ -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( @@ -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; diff --git a/test/Console/Command/MoreLikeThisTest.php b/test/Console/Command/MoreLikeThisTest.php index 3676e01..e7984da 100644 --- a/test/Console/Command/MoreLikeThisTest.php +++ b/test/Console/Command/MoreLikeThisTest.php @@ -97,7 +97,7 @@ public function testExecute(): void ->willReturn($result); $this->commandTester->execute([ - 'location' => '/test.php', + 'id' => '123', ]); $this->commandTester->assertCommandIsSuccessful(); @@ -134,7 +134,7 @@ public function testExecuteNoResult(): void ->willReturn($result); $this->commandTester->execute([ - 'location' => '/test.php', + 'id' => '123', ]); $this->commandTester->assertCommandIsSuccessful(); diff --git a/test/Service/Search/SolrMoreLikeThisTest.php b/test/Service/Search/SolrMoreLikeThisTest.php index ddcd87b..15c894b 100644 --- a/test/Service/Search/SolrMoreLikeThisTest.php +++ b/test/Service/Search/SolrMoreLikeThisTest.php @@ -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; @@ -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);