-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathQueryCommand.php
More file actions
108 lines (93 loc) · 3.3 KB
/
Copy pathQueryCommand.php
File metadata and controls
108 lines (93 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
/*
* This file is part of the MassiveSearchBundle
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Massive\Bundle\SearchBundle\Command;
use Massive\Bundle\SearchBundle\Search\SearchManagerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Command to execute a query on the configured search engine.
*
* @final
*/
class QueryCommand extends Command
{
protected static $defaultName = 'massive:search:query';
/**
* @var SearchManagerInterface
*/
private $searchManager;
public function __construct(SearchManagerInterface $searchManager)
{
parent::__construct(self::$defaultName);
$this->searchManager = $searchManager;
}
public function configure()
{
$this->addArgument('query', InputArgument::REQUIRED, 'Search query');
$this->addOption('index', 'I', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Index to search');
$this->addOption('locale', 'l', InputOption::VALUE_REQUIRED, 'Index to search');
$this->setDescription('Search the using a given query');
$this->setHelp(
<<<'EOT'
The %command.name_full% command will search the configured repository and present
the results.
%command.name_full% "This is a query string" --index=content
EOT
);
}
public function execute(InputInterface $input, OutputInterface $output): int
{
$query = $input->getArgument('query');
$indexes = $input->getOption('index');
$locale = $input->getOption('locale');
$start = \microtime(true);
$hits = $this->searchManager->createSearch($query)->indexes($indexes)->locale($locale)->execute();
$timeElapsed = \microtime(true) - $start;
$table = new Table($output);
$table->setHeaders(['Score', 'ID', 'Title', 'Description', 'Url', 'Image', 'Class']);
foreach ($hits as $hit) {
$document = $hit->getDocument();
$table->addRow(
[
$hit->getScore(),
$document->getId(),
$document->getTitle(),
$this->truncate($document->getDescription(), 50),
$document->getUrl(),
$document->getImageUrl(),
$document->getClass(),
]
);
}
$table->render();
$output->writeln(\sprintf('%s result(s) in %fs', \count($hits), $timeElapsed));
return 0;
}
/**
* Truncate the given string.
*
* See: https://github.com/symfony/symfony/issues/11977
*
* @param string $text Text to truncate
* @param int $length Length
* @param string $suffix Suffix to append
*
* @return string
*/
private function truncate($text, $length, $suffix = '...')
{
$computedLength = $length - \strlen($suffix);
return \strlen($text) > $computedLength ? \substr($text, 0, $computedLength) . $suffix : $text;
}
}