Example #1
0
 public function testUnIndexed()
 {
     $field = Document\Field::UnIndexed('field', 'value');
     $this->assertEquals($field->boost, 1);
     $this->assertEquals($field->encoding, 'UTF-8');
     $this->assertEquals($field->isBinary, false);
     $this->assertEquals($field->isIndexed, false);
     $this->assertEquals($field->isStored, true);
     $this->assertEquals($field->isTokenized, false);
     $this->assertEquals($field->name, 'field');
     $this->assertEquals($field->value, 'value');
 }
Example #2
0
 /**
  * @param $data
  * @param SearchIndexInterface $index
  *
  * @return IndexInterface
  */
 public function index($data, SearchIndexInterface $index)
 {
     $this->unindex($data, $index);
     $indexDoc = new Document();
     $indexDoc->addField(Field::Keyword('group_id', $data->id));
     $indexDoc->addField(Field::UnIndexed('type', "group"));
     $indexDoc->addField(Field::UnIndexed('identifier', $data->url));
     $indexDoc->addField(Field::UnIndexed('date_time', date('c')));
     $indexDoc->addField(Field::UnIndexed('date', date('j. M. Y')));
     $indexDoc->addField(Field::Text('title', $data->name_short, 'utf-8'));
     $indexDoc->addField(Field::Text('body', $data->description, 'utf-8'));
     $index->addDocument($indexDoc);
     return $this;
 }
Example #3
0
 /**
  * @param $data
  * @param SearchIndexInterface $index
  *
  * @return IndexInterface
  */
 public function index($data, SearchIndexInterface $index)
 {
     $this->unindex($data, $index);
     $indexDoc = new Document();
     $indexDoc->addField(Field::Keyword('news_id', $data->id));
     $indexDoc->addField(Field::UnIndexed('type', "news"));
     $indexDoc->addField(Field::UnIndexed('identifier', $data->id));
     $indexDoc->addField(Field::UnIndexed('date_time', $data->created_date->format('c')));
     $indexDoc->addField(Field::UnIndexed('date', $data->created_date->format('j. M. Y')));
     $indexDoc->addField(Field::Text('title', $data->title, 'utf-8'));
     $indexDoc->addField(Field::Text('body', $data->body, 'utf-8'));
     $index->addDocument($indexDoc);
     return $this;
 }
Example #4
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]);
 }