From 2827bd0e9680bfa1833566bb633952ca6340279b Mon Sep 17 00:00:00 2001 From: Adrien DUPUIS Date: Thu, 4 Feb 2021 20:47:30 +0100 Subject: [PATCH 1/6] Fulltext search command --- src/bundle/Command/FulltextSearchCommand.php | 64 ++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/bundle/Command/FulltextSearchCommand.php diff --git a/src/bundle/Command/FulltextSearchCommand.php b/src/bundle/Command/FulltextSearchCommand.php new file mode 100644 index 0000000..6b62213 --- /dev/null +++ b/src/bundle/Command/FulltextSearchCommand.php @@ -0,0 +1,64 @@ +searchService = $searchService; + } + + function configure() + { + $this + ->setDescription('Search for content using fulltext') + //->addOption('offset', 'o',InputOption::VALUE_REQUIRED, 'Returned content offset', 0) + ->addOption('limit', 'c', InputOption::VALUE_REQUIRED, 'Returned content count', 5) + //->addOption('language', 'l', InputOption::VALUE_REQUIRED, 'Language to search in') + ->addArgument('phrase', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'Searched text'); + } + + function execute(InputInterface $input, OutputInterface $output) + { + $phrase = implode(' ', $input->getArgument('phrase')); + $limit = $input->getOption('limit'); + $plural = 1 < $limit ? 's' : ''; + $output->writeln("Searching {$limit} content{$plural} for “{$phrase}”…"); + + try { + $searchResult = $this->searchService->findContent(new Query([ + 'filter' => new Query\Criterion\FullText($phrase), + 'limit' => $limit, + ])); + } catch (\Exception $e) { + $output->writeln("{$e->getMessage()}"); + + return 1; + } + + $totalCount = $searchResult->totalCount; + $plural = 1 < $totalCount ? 's' : ''; + $output->writeln("{$totalCount} result{$plural}"); + + foreach ($searchResult->searchHits as $searchHit) { + /** @var Content $content */ + $content = $searchHit->valueObject; + $output->writeln("- [{$content->id}] “{$content->getName()}” (main location: {$content->contentInfo->mainLocationId})"); + } + + return 0; + } +} \ No newline at end of file From 3ed043b39b4f44e287ebbbc2716cef28f6134237 Mon Sep 17 00:00:00 2001 From: Adrien DUPUIS Date: Thu, 4 Feb 2021 20:59:27 +0100 Subject: [PATCH 2/6] Fulltext search command: Enhance output --- src/bundle/Command/FulltextSearchCommand.php | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/bundle/Command/FulltextSearchCommand.php b/src/bundle/Command/FulltextSearchCommand.php index 6b62213..7892dcf 100644 --- a/src/bundle/Command/FulltextSearchCommand.php +++ b/src/bundle/Command/FulltextSearchCommand.php @@ -15,13 +15,13 @@ class FulltextSearchCommand extends Command { protected static $defaultName = 'search:fulltext'; - function __construct(SearchService $searchService) + public function __construct(SearchService $searchService) { parent::__construct(self::$defaultName); $this->searchService = $searchService; } - function configure() + public function configure() { $this ->setDescription('Search for content using fulltext') @@ -31,12 +31,12 @@ function configure() ->addArgument('phrase', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'Searched text'); } - function execute(InputInterface $input, OutputInterface $output) + public function execute(InputInterface $input, OutputInterface $output) { $phrase = implode(' ', $input->getArgument('phrase')); $limit = $input->getOption('limit'); $plural = 1 < $limit ? 's' : ''; - $output->writeln("Searching {$limit} content{$plural} for “{$phrase}”…"); + $output->writeln("Searching {$limit} first content{$plural} for “{$phrase}”…"); try { $searchResult = $this->searchService->findContent(new Query([ @@ -50,15 +50,18 @@ function execute(InputInterface $input, OutputInterface $output) } $totalCount = $searchResult->totalCount; + $sliceCount = count($searchResult->searchHits); $plural = 1 < $totalCount ? 's' : ''; - $output->writeln("{$totalCount} result{$plural}"); + $time = $searchResult->time; + $time = $time < 1 ? (1000 * $time).'ms' : "{$time}s"; + $output->writeln("{$sliceCount}/{$totalCount} result{$plural} in $time"); foreach ($searchResult->searchHits as $searchHit) { /** @var Content $content */ $content = $searchHit->valueObject; - $output->writeln("- [{$content->id}] “{$content->getName()}” (main location: {$content->contentInfo->mainLocationId})"); + $output->writeln("- [{$content->id}@{$content->contentInfo->mainLocationId}] “{$content->getName()}”"); } return 0; } -} \ No newline at end of file +} From 773cca7c92223d7600363209b5dc4fd8e3d1bdfb Mon Sep 17 00:00:00 2001 From: Adrien DUPUIS Date: Thu, 4 Feb 2021 21:18:26 +0100 Subject: [PATCH 3/6] Fulltext search command: Enhance output --- src/bundle/Command/FulltextSearchCommand.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/bundle/Command/FulltextSearchCommand.php b/src/bundle/Command/FulltextSearchCommand.php index 7892dcf..9c5542f 100644 --- a/src/bundle/Command/FulltextSearchCommand.php +++ b/src/bundle/Command/FulltextSearchCommand.php @@ -56,10 +56,13 @@ public function execute(InputInterface $input, OutputInterface $output) $time = $time < 1 ? (1000 * $time).'ms' : "{$time}s"; $output->writeln("{$sliceCount}/{$totalCount} result{$plural} in $time"); + $canScore = !empty($searchResult->maxScore); + foreach ($searchResult->searchHits as $searchHit) { /** @var Content $content */ $content = $searchHit->valueObject; - $output->writeln("- [{$content->id}@{$content->contentInfo->mainLocationId}] “{$content->getName()}”"); + $score = $canScore ? (100*$searchHit->score/$searchResult->maxScore).'% ' : ''; + $output->writeln("- [{$content->id}@{$content->contentInfo->mainLocationId}] {$score}“{$content->getName()}”"); } return 0; From 08709a0e91b71ae67a1d134238b43f6ff0fb3277 Mon Sep 17 00:00:00 2001 From: Adrien DUPUIS Date: Mon, 8 Feb 2021 16:44:39 +0100 Subject: [PATCH 4/6] Fulltext search command: Enhance output --- src/bundle/Command/FulltextSearchCommand.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/bundle/Command/FulltextSearchCommand.php b/src/bundle/Command/FulltextSearchCommand.php index 9c5542f..352999b 100644 --- a/src/bundle/Command/FulltextSearchCommand.php +++ b/src/bundle/Command/FulltextSearchCommand.php @@ -42,6 +42,7 @@ public function execute(InputInterface $input, OutputInterface $output) $searchResult = $this->searchService->findContent(new Query([ 'filter' => new Query\Criterion\FullText($phrase), 'limit' => $limit, + //'sortClauses' => [new Query\SortClause\Score()], ])); } catch (\Exception $e) { $output->writeln("{$e->getMessage()}"); @@ -53,7 +54,7 @@ public function execute(InputInterface $input, OutputInterface $output) $sliceCount = count($searchResult->searchHits); $plural = 1 < $totalCount ? 's' : ''; $time = $searchResult->time; - $time = $time < 1 ? (1000 * $time).'ms' : "{$time}s"; + $time = $time < 1 ? number_format(1000 * $time, 0, '.', '').'ms' : number_format($time, 3, '.', '').'s'; $output->writeln("{$sliceCount}/{$totalCount} result{$plural} in $time"); $canScore = !empty($searchResult->maxScore); From b4b115cf88156c277e4e246e3c0f359d83a7116e Mon Sep 17 00:00:00 2001 From: Adrien DUPUIS Date: Tue, 2 Mar 2021 18:35:06 +0100 Subject: [PATCH 5/6] Add language option to search:fulltext --- src/bundle/Command/FulltextSearchCommand.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/bundle/Command/FulltextSearchCommand.php b/src/bundle/Command/FulltextSearchCommand.php index 352999b..170c065 100644 --- a/src/bundle/Command/FulltextSearchCommand.php +++ b/src/bundle/Command/FulltextSearchCommand.php @@ -27,13 +27,14 @@ public function configure() ->setDescription('Search for content using fulltext') //->addOption('offset', 'o',InputOption::VALUE_REQUIRED, 'Returned content offset', 0) ->addOption('limit', 'c', InputOption::VALUE_REQUIRED, 'Returned content count', 5) - //->addOption('language', 'l', InputOption::VALUE_REQUIRED, 'Language to search in') + ->addOption('language', 'l', InputOption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY, 'Language to search in') ->addArgument('phrase', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'Searched text'); } public function execute(InputInterface $input, OutputInterface $output) { $phrase = implode(' ', $input->getArgument('phrase')); + $languageFilter = ['languages' => $input->getOption('language')]; $limit = $input->getOption('limit'); $plural = 1 < $limit ? 's' : ''; $output->writeln("Searching {$limit} first content{$plural} for “{$phrase}”…"); @@ -43,7 +44,7 @@ public function execute(InputInterface $input, OutputInterface $output) 'filter' => new Query\Criterion\FullText($phrase), 'limit' => $limit, //'sortClauses' => [new Query\SortClause\Score()], - ])); + ]), $languageFilter); } catch (\Exception $e) { $output->writeln("{$e->getMessage()}"); From 1a32d5dfeff623a2f6902ea391e2a5a99be22fe0 Mon Sep 17 00:00:00 2001 From: Adrien DUPUIS Date: Tue, 2 Mar 2021 18:40:27 +0100 Subject: [PATCH 6/6] Format FulltextSearchCommand --- src/bundle/Command/FulltextSearchCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/bundle/Command/FulltextSearchCommand.php b/src/bundle/Command/FulltextSearchCommand.php index 170c065..5391d2d 100644 --- a/src/bundle/Command/FulltextSearchCommand.php +++ b/src/bundle/Command/FulltextSearchCommand.php @@ -27,7 +27,7 @@ public function configure() ->setDescription('Search for content using fulltext') //->addOption('offset', 'o',InputOption::VALUE_REQUIRED, 'Returned content offset', 0) ->addOption('limit', 'c', InputOption::VALUE_REQUIRED, 'Returned content count', 5) - ->addOption('language', 'l', InputOption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY, 'Language to search in') + ->addOption('language', 'l', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Language to search in') ->addArgument('phrase', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'Searched text'); } @@ -63,7 +63,7 @@ public function execute(InputInterface $input, OutputInterface $output) foreach ($searchResult->searchHits as $searchHit) { /** @var Content $content */ $content = $searchHit->valueObject; - $score = $canScore ? (100*$searchHit->score/$searchResult->maxScore).'% ' : ''; + $score = $canScore ? (100 * $searchHit->score / $searchResult->maxScore).'% ' : ''; $output->writeln("- [{$content->id}@{$content->contentInfo->mainLocationId}] {$score}“{$content->getName()}”"); }