1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSElasticaBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace FOS\ElasticaBundle\Command; |
13
|
|
|
|
14
|
|
|
use Elastica\Query; |
15
|
|
|
use Elastica\Result; |
16
|
|
|
use FOS\ElasticaBundle\Index\IndexManager; |
17
|
|
|
use Symfony\Component\Console\Command\Command; |
18
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
19
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
20
|
|
|
use Symfony\Component\Console\Input\InputOption; |
21
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Searches a type. |
25
|
|
|
*/ |
26
|
|
|
class SearchCommand extends Command |
27
|
|
|
{ |
28
|
|
|
protected static $defaultName = 'fos:elastica:search'; |
29
|
|
|
|
30
|
|
|
private $indexManager; |
31
|
|
|
|
32
|
|
|
public function __construct(IndexManager $indexManager) |
33
|
|
|
{ |
34
|
|
|
parent::__construct(); |
35
|
|
|
|
36
|
|
|
$this->indexManager = $indexManager; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
protected function configure() |
40
|
|
|
{ |
41
|
|
|
$this |
42
|
|
|
->setName('fos:elastica:search') |
43
|
|
|
->addArgument('type', InputArgument::REQUIRED, 'The type to search in') |
44
|
|
|
->addArgument('query', InputArgument::REQUIRED, 'The text to search') |
45
|
|
|
->addOption('index', null, InputOption::VALUE_REQUIRED, 'The index to search in') |
46
|
|
|
->addOption('limit', null, InputOption::VALUE_REQUIRED, 'The maximum number of documents to return', 20) |
47
|
|
|
->addOption('show-field', null, InputOption::VALUE_REQUIRED, 'Field to show, null uses the first field') |
48
|
|
|
->addOption('show-source', null, InputOption::VALUE_NONE, 'Show the documents sources') |
49
|
|
|
->addOption('show-id', null, InputOption::VALUE_NONE, 'Show the documents ids') |
50
|
|
|
->addOption('explain', null, InputOption::VALUE_NONE, 'Enables explanation for each hit on how its score was computed.') |
51
|
|
|
->setDescription('Searches documents in a given type and index') |
52
|
|
|
; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
56
|
|
|
{ |
57
|
|
|
$indexName = $input->getOption('index'); |
58
|
|
|
$index = $this->indexManager->getIndex($indexName ? $indexName : null); |
59
|
|
|
$type = $index->getType($input->getArgument('type')); |
60
|
|
|
$query = Query::create($input->getArgument('query')); |
61
|
|
|
$query->setSize($input->getOption('limit')); |
62
|
|
|
if ($input->getOption('explain')) { |
63
|
|
|
$query->setExplain(true); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$resultSet = $type->search($query); |
67
|
|
|
|
68
|
|
|
$output->writeLn(sprintf('Found %d results', $type->count($query))); |
69
|
|
|
foreach ($resultSet->getResults() as $result) { |
70
|
|
|
$output->writeLn($this->formatResult($result, $input->getOption('show-field'), $input->getOption('show-source'), $input->getOption('show-id'), $input->getOption('explain'))); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param Result $result |
76
|
|
|
* @param string $showField |
77
|
|
|
* @param string $showSource |
78
|
|
|
* @param string $showId |
79
|
|
|
* @param string $explain |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
protected function formatResult(Result $result, $showField, $showSource, $showId, $explain) |
84
|
|
|
{ |
85
|
|
|
$source = $result->getSource(); |
86
|
|
|
if ($showField) { |
87
|
|
|
$toString = isset($source[$showField]) ? $source[$showField] : '-'; |
88
|
|
|
} else { |
89
|
|
|
$toString = reset($source); |
90
|
|
|
} |
91
|
|
|
$string = sprintf('[%0.2f] %s', $result->getScore(), var_export($toString, true)); |
92
|
|
|
if ($showSource) { |
93
|
|
|
$string = sprintf('%s %s', $string, json_encode($source)); |
94
|
|
|
} |
95
|
|
|
if ($showId) { |
96
|
|
|
$string = sprintf('{%s} %s', $result->getId(), $string); |
97
|
|
|
} |
98
|
|
|
if ($explain) { |
99
|
|
|
$string = sprintf('%s %s', $string, json_encode($result->getExplanation())); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $string; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|