/** * {@inheritdoc} */ public function register(Application $app) { Analyzer::setDefault(new CaseInsensitive()); QueryParser::setDefaultEncoding('UTF-8'); $app['zendsearch.indices_path'] = array(); $app['zendsearch.indices.initializer'] = $app->protect(function () use($app) { static $initialized = false; if ($initialized) { return; } $initialized = true; $indices = array(); foreach ($app['zendsearch.indices_path'] as $name => $index) { $indices[$name] = file_exists($index) ? Lucene::open($index) : Lucene::create($index); } $app['zendsearch.indices_collection'] = $indices; }); $app['zendsearch.indices'] = $app->share(function ($app) { $app['zendsearch.indices.initializer'](); return $app['zendsearch.indices_collection']; }); $app['zendsearch.multisearcher'] = $app->share(function ($app) { $app['zendsearch.indices.initializer'](); $multi = new MultiSearcher(); foreach ($app['zendsearch.indices'] as $index) { $multi->addIndex($index); } return $multi; }); $app['zendsearch'] = $app->share(function ($app) { return $app['zendsearch.multisearcher']; }); }
public function search($query, $contexts = array()) { $searcher = new Lucene\MultiSearcher(); foreach ($contexts as $indexName) { $searcher->addIndex($this->getLuceneIndex($indexName)); } $query = Lucene\Search\QueryParser::parse($query); try { $luceneHits = $searcher->find($query); } catch (\RuntimeException $e) { if (!preg_match('&non-wildcard characters&', $e->getMessage())) { throw $e; } $luceneHits = array(); } $hits = array(); foreach ($luceneHits as $luceneHit) { /* @var Lucene\Search\QueryHit $luceneHit */ $luceneDocument = $luceneHit->getDocument(); $hit = new Hit(); $hit->setScore($luceneHit->score); $hit->setHash($luceneDocument->getFieldValue(self::HASH_FIELDNAME)); foreach ($luceneDocument->getFieldNames() as $fieldName) { $hit->addMetadata($fieldName, $luceneDocument->getFieldValue($fieldName)); } $hits[] = $hit; } // The MultiSearcher does not support sorting, so we do it here. usort($hits, function (HitInterface $documentA, HitInterface $documentB) { if ($documentA->getScore() < $documentB->getScore()) { return true; } return false; }); return $hits; }