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'];
     });
 }
Exemple #3
0
 /**
  * @param DB $db
  * @param Core $core
  * @param string $indexesBasePath
  */
 public function __construct(DB $db, Core $core, $indexesBasePath)
 {
     $this->db = $db;
     $this->core = $core;
     $this->indexesBasePath = $indexesBasePath;
     $this->indexes = array();
     Analyzer::setDefault(new CaseInsensitive());
 }
 /**
  * @param string $path Path to search index
  */
 public function __construct($path, NodeTypeManagerInterface $nodeTypeManager = null, $hideDestructException = false)
 {
     $this->path = $path;
     $this->filesystem = new Filesystem();
     $this->nodeTypeManager = $nodeTypeManager;
     $this->hideDestructException = $hideDestructException;
     Analyzer::setDefault(new ExactMatchAnalyzer());
     Wildcard::setMinPrefixLength(0);
 }
 /**
  * Set analyzer for words highlighting (not for indexing).
  */
 public function setHighlighterAnalyzer()
 {
     /** @var AbstractCommon $analyzer */
     $analyzer = App::make('ZendSearch\\Lucene\\Analysis\\Analyzer\\Common\\AbstractCommon');
     foreach ($this->filterClasses as $filterClass) {
         $analyzer->addFilter(App::make($filterClass));
     }
     Analyzer::setDefault($analyzer);
 }
 /**
  * Set analyzer for words highlighting (not for indexing).
  */
 public function setHighlighterAnalyzer()
 {
     /** @var AbstractCommon $analyzer */
     $analyzer = App::make(AbstractCommon::class);
     foreach ($this->filterClasses as $filterClass) {
         $analyzer->addFilter(App::make($filterClass));
     }
     Analyzer::setDefault($analyzer);
 }
 public function init()
 {
     if ($this->caseSensitivity) {
         Analyzer::setDefault(new Utf8());
     } else {
         Analyzer::setDefault(new CaseInsensitive());
     }
     $this->indexDirectory = FileHelper::normalizePath(Yii::getAlias($this->indexDirectory));
     $this->luceneIndex = $this->getLuceneIndex($this->indexDirectory);
 }
 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);
 }
Exemple #9
0
 public function testAnalyzer()
 {
     $currentAnalyzer = Analyzer::getDefault();
     $this->assertTrue($currentAnalyzer instanceof AnalyzerInterface);
     /** Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8Num */
     $newAnalyzer = new Common\Utf8Num();
     Analyzer::setDefault($newAnalyzer);
     $this->assertTrue(Analyzer::getDefault() === $newAnalyzer);
     // Set analyzer to the default value (used in other tests)
     Analyzer::setDefault($currentAnalyzer);
 }
 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 #11
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');
     }
 }
Exemple #12
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;
 }
 public function search($expression, $page = 1, $conditions = null, $indexName = null)
 {
     if (!$expression) {
         throw new NotFoundHttpException('Empty expression');
     }
     if (!$indexName) {
         $indexName = $this->kernel->getContainer()->getParameter('symbio_fulltext_search.' . Crawler::DEFAULT_INDEX_PARAM);
     }
     if (mb_strlen($expression, 'utf-8') > 2) {
         $index = $this->indexManager->getIndex($indexName);
         Analyzer::setDefault(new CaseInsensitive());
         $query = $this->prepareQuery($expression, $conditions);
         $results = $index->find($query);
         // strankovani
         $paginator = $this->kernel->getContainer()->get('knp_paginator');
         $pagination = $paginator->paginate($results, $page, $this->kernel->getContainer()->getParameter('symbio_fulltext_search.items_on_page'));
         return array('expression' => $expression, 'pagination' => $pagination, 'pgdata' => $pagination->getPaginationData());
     }
     return false;
 }
 /**
  * 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 #15
0
 /**
  * インデックスファイルを生成
  */
 public static function updateIndex()
 {
     if (empty(self::$igo)) {
         self::$igo = new Tagger(array('dict_dir' => LIB_DIR . 'ipadic', 'reduce_mode' => true));
     }
     Analyzer::setDefault(new Utf8());
     // 索引の作成
     $index = Lucene::create(CACHE_DIR . self::INDEX_NAME);
     foreach (Listing::pages() as $page) {
         if (empty($page)) {
             continue;
         }
         $wiki = Factory::Wiki($page);
         // 読む権限がない場合スキップ
         if (!$wiki->isReadable() || $wiki->isHidden()) {
             continue;
         }
         /*
         			// HTML出力
         			$html[] = '<html><head>';
         			$html[] = '<meta http-equiv="Content-type" content="text/html; charset=UTF-8"/>';
         			$html[] = '<title>' . $wiki->title() . '</title>';
         			$html[] = '</head>';
         			$html[] = '<body>' . $wiki->render() . '</body>';
         			$html[] = '</html>';
         */
         $doc = new LuceneDoc();
         $doc->addField(Field::Text('title', $wiki->title()));
         // Store document URL to identify it in the search results
         $doc->addField(Field::Text('url', $wiki->uri()));
         // Index document contents
         //$contents = join(" ", self::$igo->wakati(strip_tags($wiki->render())));
         $contents = strip_tags($wiki->render());
         $doc->addField(Field::UnStored('contents', $contents));
         // 索引へ文書の登録
         $index->addDocument($doc);
     }
     $index->optimize();
 }
Exemple #16
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;
 }
 public function luceneSearchAetCommunications($index, $searchKeyWord)
 {
     \ZendSearch\Lucene\Analysis\Analyzer\Analyzer::setDefault(new \ZendSearch\Lucene\Analysis\Analyzer\Common\Text\CaseInsensitive());
     $dbIds = array();
     $searchValue = SearchHelper::utf8_to_ascii(mb_strtolower($searchKeyWord, "UTF-8"));
     $em = $this->getDoctrine()->getManager();
     /*
     $term1 = new \ZendSearch\Lucene\Index\Term($searchValue, 'firstname');
     //$subquery1 = new \ZendSearch\Lucene\Search\Query\Term($term1);
     
     $term2 = new \ZendSearch\Lucene\Index\Term($searchValue, 'title');
     //$subquery2 = new \ZendSearch\Lucene\Search\Query\Term($term2);
     
     $term3 = new \ZendSearch\Lucene\Index\Term($searchValue, 'shortdesc');
     //$subquery3 = new \ZendSearch\Lucene\Search\Query\Term($term3);
     
     $term4 = new \ZendSearch\Lucene\Index\Term($searchValue, 'body');
     //$subquery4 = new \ZendSearch\Lucene\Search\Query\Term($term4);
     
     $term5 = new \ZendSearch\Lucene\Index\Term($searchValue, 'author');
     //$subquery5 = new \ZendSearch\Lucene\Search\Query\Term($term5);
     
     
     $terms = array($term1, $term2, $term3, $term4, $term5);
     //$subqueries = array($subquery1, $subquery2, $subquery3, $subquery4, $subquery5);
     $signs = array(null, null, null, null, null);
     
     $termsQuery = new \ZendSearch\Lucene\Search\Query\MultiTerm($terms,$signs);
     */
     \ZendSearch\Lucene\Search\QueryParser::setDefaultOperator(\ZendSearch\Lucene\Search\QueryParser::B_OR);
     $query = \ZendSearch\Lucene\Search\QueryParser::parse($searchValue, 'UTF-8');
     $foundDocuments = $index->find($query);
     //$docNum = count($foundDocuments);
     foreach ($foundDocuments as $foundDoc) {
         $dbIds[] = $foundDoc->dbId;
     }
     $results = $em->getRepository('AetCommunicationBundle:Communication')->findById($dbIds);
     return $results;
 }
 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;
 }
 public function luceneSearchAetUsers($index, $searchKeyWord)
 {
     \ZendSearch\Lucene\Analysis\Analyzer\Analyzer::setDefault(new \ZendSearch\Lucene\Analysis\Analyzer\Common\Text\CaseInsensitive());
     $dbIds = array();
     $searchValue = SearchHelper::utf8_to_ascii(mb_strtolower($searchKeyWord, "UTF-8"));
     $em = $this->getDoctrine()->getManager();
     /*
             $term1 = new \ZendSearch\Lucene\Index\Term($searchValue, 'firstname');
             //$subquery1 = new \ZendSearch\Lucene\Search\Query\Term($term1);
     
             $term2 = new \ZendSearch\Lucene\Index\Term($searchValue, 'lastname');
             //$subquery2 = new \ZendSearch\Lucene\Search\Query\Term($term2);
     
             $term3 = new \ZendSearch\Lucene\Index\Term($searchValue, 'activiteprincipale');
             //$subquery3 = new \ZendSearch\Lucene\Search\Query\Term($term3);
     
             $term4 = new \ZendSearch\Lucene\Index\Term($searchValue, 'codepostal');
             //$subquery4 = new \ZendSearch\Lucene\Search\Query\Term($term4);
     
             $term5 = new \ZendSearch\Lucene\Index\Term($searchValue, 'email');
             //$subquery5 = new \ZendSearch\Lucene\Search\Query\Term($term5);
     
             $term6 = new \ZendSearch\Lucene\Index\Term($searchValue, 'matricule');
             //$subquery6 = new \ZendSearch\Lucene\Search\Query\Term($term6);
     
             $term7 = new \ZendSearch\Lucene\Index\Term($searchValue, 'pays');
             //$subquery7 = new \ZendSearch\Lucene\Search\Query\Term($term7);
     
             $term8 = new \ZendSearch\Lucene\Index\Term($searchValue, 'promotion');
             //$subquery8 = new \ZendSearch\Lucene\Search\Query\Term($term8);
     
             $term9 = new \ZendSearch\Lucene\Index\Term($searchValue, 'telephone');
             //$subquery9 = new \ZendSearch\Lucene\Search\Query\Term($term9);
     
             $term10 = new \ZendSearch\Lucene\Index\Term($searchValue, 'ville');
             //$subquery10 = new \ZendSearch\Lucene\Search\Query\Term($term10);
     
             $term11 = new \ZendSearch\Lucene\Index\Term($searchValue, 'whoami');
             //$subquery11 = new \ZendSearch\Lucene\Search\Query\Term($term11);
     
     
             //$subqueries = array($subquery1, $subquery2, $subquery3, $subquery4, $subquery5, $subquery6, $subquery7, $subquery8, $subquery9, $subquery10, $subquery11);
             $terms = array($term1, $term2, $term3, $term4, $term5, $term6, $term7, $term8, $term9, $term10, $term11);
             $signs = array(null, null, null, null, null, null, null, null, null, null, null);
     
     $termsQuery = new \ZendSearch\Lucene\Search\Query\MultiTerm($terms, $signs);
             //$boolQuery = new \ZendSearch\Lucene\Search\Query\Boolean($subqueries, $signs);
     */
     \ZendSearch\Lucene\Search\QueryParser::setDefaultOperator(\ZendSearch\Lucene\Search\QueryParser::B_OR);
     $query = \ZendSearch\Lucene\Search\QueryParser::parse($searchValue, 'UTF-8');
     $foundDocuments = $index->find($query);
     //$docNum = count($foundDocuments);
     foreach ($foundDocuments as $foundDoc) {
         $dbIds[] = $foundDoc->dbId;
     }
     $results = $em->getRepository('AetAnnuaireBundle:User')->findById($dbIds);
     return $results;
 }
Exemple #21
0
 /**
  * Подсветка результата поиска в html-фрагменте
  *
  * @param string $inputHTMLFragment исходный фрагмента html
  * @param string $inputEncoding Кодировка исходного фрагмента html
  * @param string $outputEncoding Кодировка резульрирующего фрагмента html
  * @return string html фрагмент с подсвеченными результатами поиска
  */
 public function highlightMatches($inputHTMLFragment, $inputEncoding = 'utf-8', $outputEncoding = 'utf-8')
 {
     $highlightedHTMLFragment = '';
     if (!empty($this->lastQuery)) {
         $queryParser = QueryParser::parse($this->lastQuery);
         /**
          * Убираем фильтры стоп-слов для подсветки слов с псевдокорнями типа 'под' и т.п.
          */
         Analyzer::setDefault($this->analyzerForHighlighter);
         $highlightedHTMLFragment = $queryParser->htmlFragmentHighlightMatches($inputHTMLFragment, $inputEncoding, new Highlighter());
         Analyzer::setDefault($this->defaultAnalyzer);
         $highlightedHTMLFragment = mb_convert_encoding($highlightedHTMLFragment, $outputEncoding, 'utf-8');
     }
     return $highlightedHTMLFragment;
 }