Exemple #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'];
     });
 }
 public function __construct(array $filterClasses, array $stopWordFiles, FilterFactory $stopwordsFilterFactory)
 {
     QueryParser::setDefaultEncoding('utf-8');
     $this->filterClasses = $filterClasses;
     $this->stopWordFiles = $stopWordFiles;
     $this->stopwordsFilterFactory = $stopwordsFilterFactory;
 }
 public function init()
 {
     QueryParser::setDefaultEncoding('UTF-8');
     if ($this->caseSensitivity) {
         Analyzer::setDefault($this->parseNumeric ? new Utf8Num() : new Utf8());
     } else {
         Analyzer::setDefault($this->parseNumeric ? new CaseInsensitiveNum() : new CaseInsensitive());
     }
     $this->indexDirectory = FileHelper::normalizePath(Yii::getAlias($this->indexDirectory));
     $this->luceneIndex = $this->getLuceneIndex($this->indexDirectory);
 }
 public function __construct(FrontendController $frontendController, $moduleName)
 {
     parent::__construct($frontendController, $moduleName);
     $this->controllerRoutes = array('/' => array('GET' => 'showSearchResults'), '/rebuild-index' => array('GET' => 'rebuildIndex'));
     Analyzer::setDefault(new CaseInsensitive());
     QueryParser::setDefaultEncoding('UTF-8');
     $this->registerService('index', 'generateIndex');
     $cmsPage = $frontendController->getCmsPage();
     if ($cmsPage !== null) {
         $cmsPage->setLastModified(date('Y-m-d H:i:s'));
     }
 }
Exemple #6
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]);
 }
 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;
 }
 /**
  * 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];
 }
Exemple #9
0
 /**
  * Create a new zend instance.
  *
  * @param string $name
  * @param string $driver
  * 
  * @return void
  */
 public function __construct($name, $driver)
 {
     parent::__construct($name, $driver);
     \ZendSearch\Lucene\Search\QueryParser::setDefaultEncoding('UTF-8');
 }
 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;
 }
Exemple #11
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);
     }
 }