Esempio n. 1
0
 /**
  * Get index
  * @return \ZendSearch\Lucene\Index
  */
 private function index()
 {
     if (!isset(self::$index)) {
         $analyzer = new CaseInsensitive();
         if ($this->config()->exists('zend_search', 'stop_words')) {
             $stop_word_filter = new StopWords();
             $words = $this->getRealPath($this->config()->get('zend_search', 'stop_words'));
             if ($words !== false) {
                 $stop_word_filter->loadFromFile($words);
             } else {
                 throw new \InvalidArgumentException('Path not found');
             }
             $analyzer->addFilter($stop_word_filter);
         }
         if ($this->config()->exists('zend_search', 'morphy_dicts')) {
             $morphy_dicts = $this->getRealPath($this->config()->get('zend_search', 'morphy_dicts'));
             if ($morphy_dicts !== false) {
                 $analyzer->addFilter(new Morphy($morphy_dicts, $this->config()->getCharset()));
             } else {
                 throw new \InvalidArgumentException('Path not found');
             }
         }
         Analyzer::setDefault($analyzer);
         Lucene::setResultSetLimit($this->limit);
         QueryParser::setDefaultEncoding($this->config()->getCharset());
         $index = $this->config() - get('zend_search', 'index');
         $path = $this->getRealPath($index);
         self::$index = $path ? Lucene::open($path) : Lucene::create($index);
     }
     return self::$index;
 }
 /**
  * {@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'];
     });
 }
 /**
  * @param string $directory
  * @return \ZendSearch\Lucene\SearchIndexInterface
  */
 protected function getLuceneIndex($directory)
 {
     if (file_exists($directory . DIRECTORY_SEPARATOR . 'segments.gen')) {
         return Lucene::open($directory);
     } else {
         return Lucene::create($directory);
     }
 }
Esempio n. 4
0
 /**
  * Opens a new zend search index. If it does not exist it will be created.
  * 
  * @param string $indexPath Path to the index
  *
  * @return SearchIndexInterface
  */
 public static function openOrCreate($indexPath)
 {
     try {
         return Lucene::open($indexPath);
     } catch (\Exception $e) {
         return Lucene::create($indexPath);
     }
 }
 /**
  * 
  * @return \ZendSearch\Lucene\SearchIndexInterface
  */
 public function getIndex()
 {
     if (is_null($this->index)) {
         $this->fileSystem = tao_models_classes_FileSourceService::singleton()->getFileSource($this->getOption('fileSystem'));
         $this->index = Lucene::open($this->fileSystem->getPath());
     }
     return $this->index;
 }
Esempio n. 6
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);
     }
 }
Esempio n. 7
0
 /**
  *
  * Create connection to index
  *
  * @param $path
  * @param AnalyzerConfig $config
  * @throws \Exception
  */
 public function __construct($path)
 {
     $this->indexPath = $path;
     try {
         $this->index = Lucene::open($path);
     } catch (ExceptionInterface $e) {
         $this->index = Lucene::create($path);
     } catch (\Exception $e) {
         if (!file_exists($path)) {
             throw new \Exception("Couldn't connect to index of Zend Lucene. Directory '{$path}' doesn't exist.'");
         }
         throw $e;
     }
 }
 public function indexAction()
 {
     $request = $this->getRequest();
     if ($request->isPost()) {
         $queryText = $request->getPost()->get('query');
         $searchIndexLocation = $this->getIndexLocation();
         $index = Lucene\Lucene::open($searchIndexLocation);
         $this->searchResults = $index->find($queryText);
     }
     // prepare search form
     $form = new \Zend\Form\Form();
     $form->add(array('name' => 'query', 'attributes' => array('type' => 'text', 'id' => 'queryText', 'required' => 'required'), 'options' => array('label' => 'Search String')));
     $form->add(array('name' => 'submit', 'attributes' => array('type' => 'submit', 'value' => 'Search', 'style' => "margin-bottom: 8px; height: 27px;")));
     $viewModel = new ViewModel(array('form' => $form, 'searchResults' => $this->searchResults));
     return $viewModel;
 }
Esempio n. 9
0
 /**
  * Get the ZendSearch lucene index instance associated with this instance.
  *
  * @return \ZendSearch\Lucene\Index
  */
 protected function getIndex()
 {
     if (!$this->index) {
         $path = rtrim(Config::get('search.connections.zend.path'), '/') . '/' . $this->name;
         try {
             $this->index = \ZendSearch\Lucene\Lucene::open($path);
         } catch (\ZendSearch\Exception\ExceptionInterface $e) {
             $this->index = \ZendSearch\Lucene\Lucene::create($path);
         } catch (\ErrorException $e) {
             if (!file_exists($path)) {
                 throw new \Exception("'path' directory does not exist for the 'zend' search driver: '" . rtrim(Config::get('search.connections.zend.path'), '/') . "'");
             }
             throw $e;
         }
         \ZendSearch\Lucene\Analysis\Analyzer\Analyzer::setDefault(new \ZendSearch\Lucene\Analysis\Analyzer\Common\Utf8Num\CaseInsensitive());
     }
     return $this->index;
 }
Esempio n. 10
0
 /**
  * opens or creates the given lucene index
  *
  * @throws SetUpException
  */
 public function openOrCreate()
 {
     $indexFolder = $this->files->setUpIndexFolder();
     $storage = $indexFolder->getStorage();
     $localPath = $storage->getLocalFolder($indexFolder->getInternalPath());
     //let lucene search for numbers as well as words
     Analyzer::setDefault(new CaseInsensitive());
     // can we use the index?
     if ($indexFolder->nodeExists('v0.6.0')) {
         // correct index present
         $this->index = Lucene::open($localPath);
     } else {
         $this->logger->info('recreating outdated lucene index');
         $indexFolder->delete();
         $this->index = Lucene::create($localPath);
         $indexFolder->newFile('v0.6.0');
     }
 }
 public function optimizeSearchIndex()
 {
     $startTime = microtime(true);
     $indexRootPath = $this->cmsController->getCore()->getSiteRoot() . 'index' . DIRECTORY_SEPARATOR;
     foreach (scandir($indexRootPath) as $d) {
         if (is_dir($indexRootPath . $d) === false || in_array($d, array('.', '..'))) {
             continue;
         }
         try {
             $searchIndex = Lucene::open($indexRootPath . $d);
             $searchIndex->optimize();
             $searchIndex->commit();
         } catch (\Exception $e) {
             continue;
         }
     }
     $endTime = microtime(true);
     $tplVars = array('siteTitle' => 'Optimize search index', 'duration' => round($endTime - $startTime, 3));
     return $this->renderModuleContent('mod-search-optimize', $tplVars);
 }
 /**
  * Gets the index mapped by the given lucene identifier.
  *
  * @param string $identifier The lucene identifier.
  *
  * @return \ZendSearch\Lucene\Index The lucene index.
  */
 public function getIndex($identifier)
 {
     $config = $this->getConfig($identifier);
     $path = $config['path'];
     if (!$this->checkPath($path)) {
         $this->indexes[$identifier] = Lucene::create($path);
     } else {
         $this->indexes[$identifier] = Lucene::open($path);
     }
     Analyzer::setDefault(new $config['analyzer']());
     $this->indexes[$identifier]->setMaxBufferedDocs($config['max_buffered_docs']);
     $this->indexes[$identifier]->setMaxMergeDocs($config['max_merge_docs']);
     $this->indexes[$identifier]->setMergeFactor($config['merge_factor']);
     ZfFilesystem::setDefaultFilePermissions($config['permissions']);
     if ($config['auto_optimized']) {
         $this->indexes[$identifier]->optimize();
     }
     QueryParser::setDefaultEncoding($config['query_parser_encoding']);
     return $this->indexes[$identifier];
 }
 public function indexAction()
 {
     $request = $this->getRequest();
     if ($request->isPost()) {
         $queryText = $request->getPost()->get('query');
         $searchIndexLocation = $this->getIndexLocation();
         $index = Lucene\Lucene::open($searchIndexLocation);
         $searchResults = $index->find($queryText);
         //            foreach ($searchResults as $searchResult) {
         //                \Zend\Debug\Debug::dump($searchResult->upload_id);
         //            }
     }
     // Подготовка формы поиска
     $form = new \Zend\Form\Form();
     $form->add(array('name' => 'query', 'attributes' => array('type' => 'text', 'id' => 'queryText', 'required' => 'required'), 'options' => array('label' => 'Search String')));
     $form->add(array('name' => 'submit', 'attributes' => array('type' => 'submit', 'value' => 'Search')));
     if ($request->isPost()) {
         $form->get('query')->setValue($request->getPost()->get('query'));
     }
     $viewModel = new ViewModel(array('form' => $form, 'searchResults' => $searchResults));
     return $viewModel;
 }
Esempio n. 14
0
 /**
  * Lists all Post models.
  * @return mixed
  */
 public function actionIndex()
 {
     $searchModel = new PostSearch();
     $dataProvider = $searchModel->search(Yii::$app->request->post());
     //setlocale(LC_ALL, 'en_US.UTF-8');
     setlocale(LC_CTYPE, 'ru_RU.UTF-8');
     //Lucene\Lucene::setDefaultSearchField('contents');
     Lucene\Search\QueryParser::setDefaultEncoding('UTF-8');
     Lucene\Analysis\Analyzer\Analyzer::setDefault(new Lucene\Analysis\Analyzer\Common\Utf8\CaseInsensitive());
     Lucene\Lucene::setResultSetLimit(10);
     // create blog posts index located in /data/posts_index ,make sure the folder is writable
     $index = Lucene\Lucene::create('data/posts_index');
     $posts = Post::find()->all();
     //var_dump($posts);die();
     // iterate through posts and build the index
     foreach ($posts as $p) {
         $doc = new Lucene\Document();
         $doc->addField(Lucene\Document\Field::UnIndexed('entry_id', $p->id));
         $doc->addField(Lucene\Document\Field::Keyword('title', $p->title));
         $doc->addField(Lucene\Document\Field::text('contents', $p->content));
         $index->addDocument($doc);
     }
     // commit the index
     $index->commit();
     //Lucene\Analysis\Analyzer\Analyzer::setDefault(new Lucene\Analysis\Analyzer\Common\Utf8\CaseInsensitive());
     // explode the search query to individual words
     $words = explode(' ', urldecode(Yii::$app->getRequest()->getQueryParam('q')));
     // start a search query and add a term for each word to it
     $query = new Lucene\Search\Query\MultiTerm();
     foreach ($words as $w) {
         $query->addTerm(new Lucene\Index\Term($w));
     }
     // open and query the index
     $index = Lucene\Lucene::open('data/posts_index');
     $results = $index->find($query);
     // the search results
     //var_dump($results);
     return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'search' => $results, 'query' => $query]);
 }
Esempio n. 15
0
 public function autocompleteAction()
 {
     $index = Lucene\Lucene::open('./data/search/');
     $searchResults = $index->find('title:' . $this->params()->fromRoute('term'));
     $result = array();
     foreach ($searchResults as $item) {
         if ($item->score >= 0.8) {
             $href = '';
             $type = '';
             switch ($item->type) {
                 case 'article':
                     $href = $this->url()->fromRoute('greinar/index', array('id' => $item->identifier));
                     $type = "Grein";
                     break;
                 case 'event':
                     $href = $this->url()->fromRoute('vidburdir/index', array('id' => $item->identifier));
                     $type = "Viðburður";
                     break;
                 case 'group':
                     $href = $this->url()->fromRoute('hopur/index', array('id' => $item->identifier));
                     $type = "Hópur";
                     break;
                 case 'news':
                     $href = $this->url()->fromRoute('frettir/index', array('id' => $item->identifier));
                     $type = "Frétt";
                     break;
                 default:
                     $href = "#";
                     $type = "?";
                     break;
             }
             $result[] = (object) array('value' => $item->title, 'type' => $type, 'href' => $href);
         }
     }
     return new JsonModel($result);
 }
Esempio n. 16
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);
Esempio n. 17
0
 public function testTermsStreamInterfaceSkipToTermsRetrievingTwoTermsCase()
 {
     $index = Lucene\Lucene::create(__DIR__ . '/_index/_files');
     // Zero terms
     $doc = new Document();
     $doc->addField(Document\Field::Text('contents', 'someterm word'));
     $index->addDocument($doc);
     unset($index);
     $index = Lucene\Lucene::open(__DIR__ . '/_index/_files');
     $index->resetTermsStream();
     $index->skipTo(new Index\Term('term', 'contents'));
     $this->assertTrue($index->currentTerm() == new Index\Term('word', 'contents'));
     $index->closeTermsStream();
 }
Esempio n. 18
0
 public function sortOutHlCoords()
 {
     //Lucene operators
     $operators = array("and", "or", "not");
     $config = $this->getServiceLocator()->get('config');
     $paramInfo = $this->sortOutParams($config);
     //collect building blocks
     $resLoc = $paramInfo['resLoc'];
     $site = $paramInfo['site'];
     $collection = $paramInfo['collection'];
     $container = $paramInfo['container'];
     $reel = $paramInfo['reel'];
     $page = $paramInfo['page'];
     //the all important query
     $hl = $this->params()->fromRoute('hl', '');
     //coordinates to pass back
     $coords = [];
     //pass back empty coordinate set if any of these parameters
     //are missing
     if ($this->isNullOrEmpty($reel) || $this->isNullOrEmpty($page) || $this->isNullOrEmpty($hl)) {
         return array("imgloc" => '', "indloc" => '', "coords" => $coords);
     }
     //if
     //location of files - ODW file layout
     $resLoc .= '/' . $site . '/' . $collection . '/' . $container . '/' . $reel . '/odw/' . $page . '/';
     $imgLoc = $resLoc . '../../' . $page . '.jpg';
     $iaLoc = $resLoc . 'ia/' . $page . '.jpg';
     //not all images will have IA derivative
     if (file_exists($iaLoc) !== false) {
         $imgLoc = $iaLoc;
     }
     $indLoc = $resLoc . 'index/imgworks';
     //need index directory and segments file to be valid lucene layout
     if (!file_exists($indLoc . '/segments.gen')) {
         return array("imgloc" => $imgLoc, "indloc" => $indLoc, "coords" => $coords);
     }
     //get coordinates from Lucene index
     $searchText = '';
     //use Lucene tokens for searching
     $queryTokens = Analyzer\Analyzer::getDefault()->tokenize($hl);
     foreach ($queryTokens as $token) {
         $searchTerm = $token->getTermText();
         if (!in_array($searchTerm, $operators)) {
             //no snowball analyzer or other stemming option
             //in Lucene 2.x, so create stem seperately
             $searchText .= stem_english($searchTerm);
             //Lucene dropped this limitation after 2.x
             //but this version won't wildcard without
             //at least 3 characters in term
             if (strlen($searchTerm) >= 3) {
                 $searchText .= "* ";
             }
             //if strlen
         }
         //if
     }
     //foreach
     //now do search
     $index = Lucene\Lucene::open($indLoc);
     $searchResults = $index->find($searchText);
     //assemble results
     foreach ($searchResults as $searchResult) {
         array_push($coords, [$searchResult->x1, $searchResult->y1, $searchResult->x2, $searchResult->y2]);
     }
     //foreach
     //pass back image and index location in addition to results
     return array("imgloc" => $imgLoc, "indloc" => $indLoc, "coords" => $coords);
 }
Esempio n. 19
0
 public function testLimitingResult()
 {
     $index = Lucene\Lucene::open(__DIR__ . '/_index23Sample/_files');
     $storedResultSetLimit = Lucene\Lucene::getResultSetLimit();
     Lucene\Lucene::setResultSetLimit(3);
     $hits = $index->find('"reporting bugs"', 'path');
     $this->assertEquals(count($hits), 3);
     $expectedResultset = array(array(7, 0.212395, 'IndexSource/contributing.bugs.html'), array(0, 0.247795, 'IndexSource/contributing.documentation.html'), array(2, 0.176996, 'IndexSource/contributing.patches.html'));
     foreach ($hits as $resId => $hit) {
         $this->assertEquals($hit->id, $expectedResultset[$resId][0]);
         $this->assertTrue(abs($hit->score - $expectedResultset[$resId][1]) < 1.0E-6);
         $this->assertEquals($hit->path, $expectedResultset[$resId][2]);
     }
     Lucene\Lucene::setResultSetLimit($storedResultSetLimit);
 }
Esempio n. 20
0
 /**
  *
  * Инициализация Zend Lucene
  *
  * @param string[]|string $modelClasses
  * @param string $indexLocation
  * @throws SearchCanNotConnectException
  */
 public function __construct($modelClasses, $indexLocation)
 {
     if (!is_array($modelClasses)) {
         $modelClasses = array($modelClasses);
     }
     $this->modelClasses = $this->filterModelClasses($modelClasses);
     $this->defaultAnalyzer = $this->getDefaultAnalyzer();
     $this->analyzerForHighlighter = $this->getAnalyzerForHighlighter();
     Analyzer::setDefault($this->defaultAnalyzer);
     QueryParser::setDefaultEncoding('utf-8');
     // сделаем ограничение количества записей результата поиска
     // Lucene::setResultSetLimit(10000);
     // открываем/создаём новый индекс
     if (file_exists($indexLocation = $indexLocation)) {
         try {
             $this->connection = Lucene::open($indexLocation);
         } catch (\Exception $ex) {
             $this->connection = Lucene::create($indexLocation);
         }
     } else {
         $this->connection = Lucene::create($indexLocation);
     }
     if (!$this->connection) {
         throw new SearchCanNotConnectException($indexLocation);
     }
 }
Esempio n. 21
0
 protected function getIndex()
 {
     if ($this->index != null) {
         return $this->index;
     }
     \ZendSearch\Lucene\Search\QueryParser::setDefaultEncoding('utf-8');
     \ZendSearch\Lucene\Analysis\Analyzer\Analyzer::setDefault(new \ZendSearch\Lucene\Analysis\Analyzer\Common\Utf8Num\CaseInsensitive());
     \ZendSearch\Lucene\Search\QueryParser::setDefaultOperator(\ZendSearch\Lucene\Search\QueryParser::B_AND);
     try {
         $index = \ZendSearch\Lucene\Lucene::open($this->getIndexPath());
     } catch (\ZendSearch\Lucene\Exception\RuntimeException $ex) {
         $index = \ZendSearch\Lucene\Lucene::create($this->getIndexPath());
     }
     $this->index = $index;
     return $index;
 }
Esempio n. 22
0
 public function open($dir)
 {
     $this->index = Lucene::open($this->index_dir . $dir);
     return $this;
 }
Esempio n. 23
0
 public function searchAction()
 {
     $request = $this->getRequest();
     if ($request->isPost()) {
         $queryText = $request->getPost()->get('query');
         $searchIndexLocation = $this->getIndexLocation();
         $index = Lucene\Lucene::open($searchIndexLocation);
         $searchResult = $index->find($queryText);
     }
     $form = new \Zend\Form\Form();
     $form->add(array('name' => 'query', 'attributes' => array('type' => 'text', 'id' => 'queryText'), 'options' => array('label' => 'Search String')));
     $form->add(array('name' => 'submit', 'attributes' => array('type' => 'submit', 'value' => 'Search')));
     $viewModel = new ViewModel(array('form' => $form, 'searchResults' => $searchResult));
     return $viewModel;
 }
 /**
  * @param string $keywords
  * @param string $language
  *
  * @return \stdClass[]
  */
 protected function getCmsSearchResults($keywords, $language)
 {
     $searchModel = new SearchModel($this->cmsController->getDB());
     $searchIndex = Lucene::open($this->cmsController->getCore()->getSiteRoot() . 'index' . DIRECTORY_SEPARATOR . $language);
     /*$query = new Boolean(); // new Fuzzy()
     		$query->addSubquery(QueryParser::parse(
     			$keywords
     		), true);*/
     QueryParser::suppressQueryParsingExceptions();
     $query = QueryParser::parse($keywords);
     //$hits = $searchIndex->find($query, 'score', SORT_NUMERIC, SORT_DESC);
     $hits = $searchIndex->find($query);
     //echo'<pre>'; var_dump(/*$hits, */$indexSize, $documents);
     $searchResultsArr = array();
     $highlighter = new CmsSearchHighlighter($keywords);
     //$highlighter = new DefaultHighlighter();
     foreach ($hits as $hit) {
         /** @var QueryHit $hit */
         $searchResult = new \stdClass();
         // Gibt Zend_Search_Lucene_Document Objekte für diesen Treffer zurück
         /** @var Document $document */
         $document = $hit->getDocument();
         $doc = $searchModel->getDocumentByID($document->getFieldUtf8Value('ID'));
         if ($doc->getID() === null) {
             continue;
         }
         $fldType = $doc->getType();
         if ($fldType !== 'core_page') {
             $contentChunks = $highlighter->highlightMatches(strip_tags($doc->getDescription()), 'UTF-8');
             if ($contentChunks == '') {
                 $contentChunks = null;
             }
             // Gibt ein Zend_Search_Lucene_Field Objekt von
             // Zend_Search_Lucene_Document zurück
             $searchResult->title = $highlighter->highlightMatches(strip_tags($doc->getTitle()), 'UTF-8');
             $searchResult->description = $contentChunks;
             $searchResult->url = $doc->getPath();
             if (isset($searchResultsArr[$fldType]) === false) {
                 $stmntModName = $this->cmsController->getDB()->prepare("\n\t\t\t\t\t\tSELECT manifest_content FROM cms_mod_available WHERE name = ?\n\t\t\t\t\t");
                 $resModName = $this->cmsController->getDB()->select($stmntModName, array($fldType));
                 $displayName = $fldType;
                 try {
                     $manifestObj = JsonUtils::decode($resModName[0]->manifest_content);
                     if (isset($manifestObj->name->{$language})) {
                         $displayName = $manifestObj->name->{$language};
                     } elseif (isset($manifestObj->name->en)) {
                         $displayName = $manifestObj->name->en;
                     }
                 } catch (\Exception $e) {
                 }
                 $searchResultsArr[$fldType] = new \stdClass();
                 $searchResultsArr[$fldType]->title = $displayName;
                 $searchResultsArr[$fldType]->results = array();
             }
             $searchResultsArr[$doc->getType()]->results[] = $searchResult;
         } else {
             $contentChunks = $this->createChunkedHighlighting($highlighter->highlightMatches(strip_tags($doc->getDescription()), 'UTF-8'));
             if ($contentChunks == '') {
                 $contentChunks = null;
             }
             // Gibt ein Zend_Search_Lucene_Field Objekt von
             // Zend_Search_Lucene_Document zurück
             $searchResult->title = $highlighter->highlightMatches(strip_tags($doc->getTitle()), 'UTF-8');
             $searchResult->description = $contentChunks;
             $searchResult->url = $doc->getPath();
             if (isset($searchResultsArr[$fldType]) === false) {
                 $searchResultsArr[$fldType] = new \stdClass();
                 $searchResultsArr[$fldType]->title = 'Andere Suchresultate';
                 $searchResultsArr[$fldType]->results = array();
             }
             $searchResultsArr[$doc->getType()]->results[] = $searchResult;
         }
     }
     return $searchResultsArr;
 }
Esempio n. 25
0
 public static function getLuceneIndex()
 {
     if (file_exists($index = self::getLuceneIndexFile())) {
         return Lucene::open($index);
     }
     return Lucene::create($index);
 }
 private function getIndex() : SearchIndexInterface
 {
     $path = $this->getIndexPath();
     if (!$this->checkIndexPath($path)) {
         $index = Lucene::create($path);
     } else {
         $index = Lucene::open($path);
     }
     Analyzer::setDefault(new CaseInsensitive());
     LuceneFilesystem::setDefaultFilePermissions(0775);
     QueryParser::setDefaultEncoding('UTF-8');
     $index->setMaxBufferedDocs($this->options['max_buffered_docs']);
     $index->setMaxMergeDocs($this->options['max_merge_docs']);
     $index->setMergeFactor($this->options['merge_factor']);
     $index->optimize();
     return $index;
 }
Esempio n. 27
0
 /**
  * 検索メイン処理
  * @param string $word 検索ワード
  * @param string $type 検索方法(and, or)
  * @param boolean $non_format
  * @param string $base ベースとなるページ
  * @return string
  */
 public static function do_search($word, $type = 'and', $non_format = FALSE, $base = '')
 {
     // インデックスファイルを開く
     $index = Lucene::open(CACHE_DIR . self::INDEX_NAME);
     // 検索クエリをパース
     $query = QueryBoolean();
     $keys = QueryParser;
     parse($word);
     // Luceneに渡す
     $query->addSubquery($userQuery, true);
     $query->addSubquery($constructedQuery, true);
     foreach ($keys as $key => $value) {
         $query->addSubquery(new Term($value), true);
     }
     //  検索と実行
     $hits = $index->find($query);
     var_dump($hits);
     if ($non_format) {
         //
     }
     return $hits;
 }
 /**
  * Executa uma busca nos indexes criados
  *
  * @param $query
  */
 public function query($query)
 {
     $dir = realpath(dirname(__FILE__)) . DIRECTORY_SEPARATOR . "data" . DIRECTORY_SEPARATOR;
     $jsonDir = $dir . "json";
     $indexDir = $dir . "index";
     // Percorre os indices
     $files = scandir($jsonDir);
     foreach ($files as $file) {
         if ($file == '.' || $file == '..') {
             continue;
         }
         $indexName = substr($file, 0, -5);
         $index = Lucene\Lucene::open($indexDir . DIRECTORY_SEPARATOR . $indexName);
         // Abre index
         $hits = $index->find($query);
         // Executa query
         // Lista resultados
         foreach ($hits as $hit) {
             $document = $hit->getDocument();
             // return a Zend\Search\Lucene\Field object
             // from the Zend\Search\Lucene\Document
             echo "<h3>" . $document->getFieldValue('url') . "</h3><br />";
             //echo "<p>" . $hit->text . "</p><br /><br />";
         }
     }
 }