SearchCommand::formatResult()   A
last analyzed

Complexity

Conditions 5
Paths 16

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 13
cp 0
rs 9.2728
c 0
b 0
f 0
cc 5
nc 16
nop 5
crap 30
1
<?php
2
3
/*
4
 * This file is part of the FOSElasticaBundle package.
5
 *
6
 * (c) FriendsOfSymfony <https://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 4
    public function __construct(IndexManager $indexManager)
33
    {
34 4
        parent::__construct();
35
36 4
        $this->indexManager = $indexManager;
37 4
    }
38
39 4
    protected function configure()
40
    {
41
        $this
42 4
            ->setName('fos:elastica:search')
43 4
            ->addArgument('query', InputArgument::REQUIRED, 'The text to search')
44 4
            ->addOption('index', null, InputOption::VALUE_REQUIRED, 'The index to search in')
45 4
            ->addOption('limit', null, InputOption::VALUE_REQUIRED, 'The maximum number of documents to return', 20)
46 4
            ->addOption('show-field', null, InputOption::VALUE_REQUIRED, 'Field to show, null uses the first field')
47 4
            ->addOption('show-source', null, InputOption::VALUE_NONE, 'Show the documents sources')
48 4
            ->addOption('show-id', null, InputOption::VALUE_NONE, 'Show the documents ids')
49 4
            ->addOption('explain', null, InputOption::VALUE_NONE, 'Enables explanation for each hit on how its score was computed.')
50 4
            ->setDescription('Searches documents in a given type and index')
51
        ;
52 4
    }
53
54
    protected function execute(InputInterface $input, OutputInterface $output)
55
    {
56
        $indexName = $input->getOption('index');
57
        $index = $this->indexManager->getIndex($indexName ? $indexName : null);
58
        $query = Query::create($input->getArgument('query'));
59
        $query->setSize($input->getOption('limit'));
60
        if ($input->getOption('explain')) {
61
            $query->setExplain(true);
62
        }
63
64
        $resultSet = $index->search($query);
65
66
        $output->writeLn(\sprintf('Found %d results', $index->count($query)));
67
        foreach ($resultSet->getResults() as $result) {
68
            $output->writeLn($this->formatResult($result, $input->getOption('show-field'), $input->getOption('show-source'), $input->getOption('show-id'), $input->getOption('explain')));
69
        }
70
71
        return 0;
72
    }
73
74
    /**
75
     * @param string $showField
76
     * @param string $showSource
77
     * @param string $showId
78
     * @param string $explain
79
     *
80
     * @return string
81
     */
82
    protected function formatResult(Result $result, $showField, $showSource, $showId, $explain)
83
    {
84
        $source = $result->getSource();
85
        if ($showField) {
86
            $toString = $source[$showField] ?? '-';
87
        } else {
88
            $toString = \reset($source);
89
        }
90
        $string = \sprintf('[%0.2f] %s', $result->getScore(), \var_export($toString, true));
91
        if ($showSource) {
92
            $string = \sprintf('%s %s', $string, \json_encode($source));
93
        }
94
        if ($showId) {
95
            $string = \sprintf('{%s} %s', $result->getId(), $string);
96
        }
97
        if ($explain) {
98
            $string = \sprintf('%s %s', $string, \json_encode($result->getExplanation()));
99
        }
100
101
        return $string;
102
    }
103
}
104