/**
  * {@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'];
     });
 }
示例#2
0
 /**
  * @covers ZendSearch\Lucene\MultiSearcher::find
  * @covers ZendSearch\Lucene\Search\QueryHit::getDocument
  */
 public function testFind()
 {
     $index = new Lucene\MultiSearcher(array(Lucene\Lucene::open(__DIR__ . '/_indexSample/_files'), Lucene\Lucene::open(__DIR__ . '/_indexSample/_files')));
     $hits = $index->find('submitting');
     $this->assertEquals(count($hits), 2 * 3);
     foreach ($hits as $hit) {
         $document = $hit->getDocument();
         $this->assertTrue($document instanceof Lucene\Document);
     }
 }
示例#3
0
<?php

require 'vendor/autoload.php';
use ZendSearch\Lucene\Lucene;
use ZendSearch\Lucene\MultiSearcher;
use ZendSearch\Lucene\Search\QueryParser;
$stem = function ($e) {
    return \Porter::Stem($e);
};
$q = isset($_GET['q']) ? $_GET['q'] : null;
$q = htmlentities($q);
$q = implode('+', array_map($stem, explode(' ', $q)));
header('Content-Type: application/json');
$output = array();
if ($q) {
    $indexer = Lucene::open('../_index');
    $search = new MultiSearcher(array($indexer));
    $query = QueryParser::parse($q);
    $result = $search->find($query);
    foreach ($result as $hit) {
        $title = strtolower(str_replace('-', ' ', $hit->name));
        $resultUrl = '../' . $hit->fileName;
        $output[] = array('href' => $resultUrl, 'name' => ucfirst($title), 'preview' => $query->htmlFragmentHighlightMatches(substr(preg_replace("/\\s+|{$title}/i", " ", $hit->body), 0, 300) . '...'));
    }
}
echo json_encode($output);
 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;
 }