Completed
Pull Request — master (#1622)
by Rimas
10:50
created

SearchCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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
        return 0;
74
    }
75
76
    /**
77
     * @param Result $result
78
     * @param string $showField
79
     * @param string $showSource
80
     * @param string $showId
81
     * @param string $explain
82
     *
83
     * @return string
84
     */
85
    protected function formatResult(Result $result, $showField, $showSource, $showId, $explain)
86
    {
87
        $source = $result->getSource();
88
        if ($showField) {
89
            $toString = isset($source[$showField]) ? $source[$showField] : '-';
90
        } else {
91
            $toString = reset($source);
92
        }
93
        $string = sprintf('[%0.2f] %s', $result->getScore(), var_export($toString, true));
94
        if ($showSource) {
95
            $string = sprintf('%s %s', $string, json_encode($source));
96
        }
97
        if ($showId) {
98
            $string = sprintf('{%s} %s', $result->getId(), $string);
99
        }
100
        if ($explain) {
101
            $string = sprintf('%s %s', $string, json_encode($result->getExplanation()));
102
        }
103
104
        return $string;
105
    }
106
}
107