From a2eb6242c0617696f06ade2772cb54fe23a9ed5c Mon Sep 17 00:00:00 2001 From: ornicar Date: Tue, 12 Apr 2011 15:18:18 -0700 Subject: [PATCH] Add a search command --- Command/SearchCommand.php | 49 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 Command/SearchCommand.php diff --git a/Command/SearchCommand.php b/Command/SearchCommand.php new file mode 100644 index 0000000..a39bfeb --- /dev/null +++ b/Command/SearchCommand.php @@ -0,0 +1,49 @@ +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') + ->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 = $input->getArgument('query'); + + $resultSet = $type->search($query); + + $output->writeLn(sprintf('Found %d results', $resultSet->count())); + foreach ($resultSet->getResults() as $result) { + $source = $result->getSource(); + $output->writeLn(sprintf('[%0.2f] %s', $result->getScore(), var_export(reset($source), true))); + } + } +}