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;
 }
Esempio n. 2
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. 3
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);
 }
 /**
  * Search page for the term in the index.
  * @param string $term
  * @return array ('results' => array \ZendSearch\Lucene\Search\QueryHit, 'query' => string)
  */
 public function find($term)
 {
     Wildcard::setMinPrefixLength($this->minPrefixLength);
     Lucene::setResultSetLimit($this->resultsLimit);
     return ['results' => $this->luceneIndex->find($term), 'query' => $term];
 }
 /**
  * Search page for the term in the index.
  * @param string $term
  * @param array $fields (string => string)
  * @return array ('results' => \ZendSearch\Lucene\Search\QueryHit[], 'query' => string)
  */
 public function find($term, $fields = [])
 {
     Wildcard::setMinPrefixLength($this->minPrefixLength);
     Lucene::setResultSetLimit($this->resultsLimit);
     if (empty($fields)) {
         return ['results' => $this->luceneIndex->find($term), 'query' => $term];
     }
     $fieldTerms[] = new IndexTerm($term);
     foreach ($fields as $field => $fieldText) {
         $fieldTerms[] = new IndexTerm($fieldText, $field);
     }
     return ['results' => $this->luceneIndex->find(new MultiTerm($fieldTerms)), 'query' => $term];
 }
 /**
  * Search page for the term in the index.
  *
  * @param $term
  * @param array $fields
  * @return array|\ZendSearch\Lucene\Search\QueryHit
  */
 public function search($term, $fields = [])
 {
     Wildcard::setMinPrefixLength($this->minPrefixLength);
     Lucene::setResultSetLimit($this->resultsLimit);
     $query = new MultiTerm();
     if (count($fields)) {
         foreach ($fields as $field => $value) {
             $query->addTerm(new IndexTerm($value, $field), true);
         }
     }
     $subTerm = explode(' ', $term);
     foreach ($subTerm as $value) {
         $query->addTerm(new IndexTerm($value), true);
     }
     return $this->luceneIndex->find($query);
 }