FOSElasticaBundle/Command/SearchCommand.php

82 lines
3.2 KiB
PHP
Raw Normal View History

2011-04-13 00:18:18 +02:00
<?php
namespace FOQ\ElasticaBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\Output;
use Elastica_Query;
2011-04-21 21:18:48 +02:00
use Elastica_Result;
2011-04-13 00:18:18 +02:00
/**
* Searches a type
*/
class SearchCommand extends Command
{
/**
* @see Command
*/
protected function configure()
{
$this
->setDefinition(array(
new InputArgument('type', InputArgument::REQUIRED, 'The type to search in'),
new InputArgument('query', InputArgument::REQUIRED, 'The text to search'),
))
->addOption('index', null, InputOption::VALUE_NONE, 'The index to search in')
->addOption('limit', null, InputOption::VALUE_REQUIRED, 'The maximum number of documents to return', 20)
->addOption('show-field', null, InputOption::VALUE_REQUIRED, 'Field to show, null uses the first field')
2011-04-21 21:18:48 +02:00
->addOption('show-source', null, InputOption::VALUE_NONE, 'Show the documents sources')
->addOption('show-id', null, InputOption::VALUE_NONE, 'Show the documents ids')
->addOption('explain', null, InputOption::VALUE_NONE, 'Enables explanation for each hit on how its score was computed.')
2011-04-13 00:18:18 +02:00
->setName('foq:elastica:search')
->setDescription('Searches documents in a given type and index');
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$index = $this->container->get('foq_elastica.index_manager')->getIndex($input->getOption('index'));
$type = $index->getType($input->getArgument('type'));
$query = Elastica_Query::create($input->getArgument('query'));
$query->setLimit($input->getOption('limit'));
if ($input->getOption('explain')) {
$query->setExplain(true);
}
2011-04-13 00:18:18 +02:00
$resultSet = $type->search($query);
$output->writeLn(sprintf('Found %d results', $type->count($query)));
2011-04-13 00:18:18 +02:00
foreach ($resultSet->getResults() as $result) {
$output->writeLn($this->formatResult($result, $input->getOption('show-field'), $input->getOption('show-source'), $input->getOption('show-id'), $input->getOption('explain')));
2011-04-13 00:18:18 +02:00
}
}
2011-04-21 21:18:48 +02:00
protected function formatResult(Elastica_Result $result, $showField, $showSource, $showId, $explain)
2011-04-21 21:18:48 +02:00
{
$source = $result->getSource();
if ($showField) {
$toString = isset($source[$showField]) ? $source[$showField] : '-';
} else {
$toString = reset($source);
}
$string = sprintf('[%0.2f] %s', $result->getScore(), var_export($toString, true));
2011-04-21 21:18:48 +02:00
if ($showSource) {
$string = sprintf('%s %s', $string, json_encode($source));
}
if ($showId) {
$string = sprintf('{%s} %s', $result->getId(), $string);
}
if ($explain) {
$string = sprintf('%s %s', $string, json_encode($result->getExplanation()));
2011-04-21 21:18:48 +02:00
}
return $string;
}
2011-04-13 00:18:18 +02:00
}