コード例 #1
0
 /**
  * performs a search on the users index
  * 
  * @param string $query lucene search query
  * @return \OCA\Search_Lucene\Search\LuceneResult[]
  */
 public function search($query)
 {
     $app = new Application();
     $container = $app->getContainer();
     $results = array();
     if ($query !== null) {
         // * query * kills performance for bigger indexes
         // query * works ok
         // query is still best
         //FIXME emulates the old search but breaks all the nice lucene search query options
         //$query = '*' . $query . '*';
         //if (strpos($query, '*')===false) {
         //	$query = $query.='*'; // append query *, works ok
         //	TODO add end user guide for search terms ...
         //}
         try {
             $index = $container->query('Index');
             //default is 3, 0 needed to keep current search behaviour
             //Zend_Search_Lucene_Search_Query_Wildcard::setMinPrefixLength(0);
             //$term  = new Zend_Search_Lucene_Index_Term($query);
             //$query = new Zend_Search_Lucene_Search_Query_Term($term);
             $hits = $index->find($query);
             //limit results. we cant show more than ~30 anyway. TODO use paging later
             for ($i = 0; $i < 30 && $i < count($hits); $i++) {
                 $results[] = new LuceneResult($hits[$i]);
             }
         } catch (\Exception $e) {
             $container->query('Logger')->error($e->getMessage() . ' Trace:\\n' . $e->getTraceAsString());
         }
     }
     return $results;
 }
コード例 #2
0
ファイル: deletejob.php プロジェクト: ntvis/search_lucene
 /**
  * @param array $arguments
  */
 public function run($arguments)
 {
     if (!App::isEnabled('search_lucene')) {
         return;
     }
     $app = new Application();
     $container = $app->getContainer();
     /** @var Logger $logger */
     $logger = $container->query('Logger');
     if (empty($arguments['user'])) {
         $logger->debug('indexer job did not receive user in arguments: ' . json_encode($arguments));
         return;
     }
     $userId = $arguments['user'];
     $logger->debug('background job optimizing index for ' . $userId);
     $container->query('FileUtility')->setUpIndexFolder($userId);
     /** @var Index $index */
     $index = $container->query('Index');
     /** @var StatusMapper $mapper */
     $mapper = $container->query('StatusMapper');
     $deletedIds = $mapper->getDeleted();
     $count = 0;
     foreach ($deletedIds as $fileId) {
         $logger->debug('deleting status for (' . $fileId . ') ');
         //delete status
         $status = new Status($fileId);
         $mapper->delete($status);
         //delete from lucene
         $count += $index->deleteFile($fileId);
     }
     $logger->debug('removed ' . $count . ' files from index');
 }
コード例 #3
0
 function testOptimizeJob()
 {
     // preparation
     $app = new Application();
     $container = $app->getContainer();
     $job = new OptimizeJob();
     $job->run(array('user' => 'test'));
     // make sure we can still find documents
     // get an index
     /** @var Index $index */
     $index = $container->query('Index');
     // search for it
     /** @var QueryHit[] $hits */
     $hits = $index->find('foo');
     // get the document from the query hit
     $foundDoc = $hits[0]->getDocument();
     $this->assertSame('document.txt', basename($foundDoc->getFieldValue('path')));
 }
コード例 #4
0
ファイル: optimizejob.php プロジェクト: ntvis/search_lucene
 /**
  * @param array $arguments
  */
 public function run($arguments)
 {
     if (!App::isEnabled('search_lucene')) {
         return;
     }
     $app = new Application();
     $container = $app->getContainer();
     /** @var Logger $logger */
     $logger = $container->query('Logger');
     if (!empty($arguments['user'])) {
         $userId = $arguments['user'];
         $logger->debug('background job optimizing index for ' . $userId);
         $container->query('FileUtility')->setUpIndexFolder($userId);
         $container->query('Index')->optimizeIndex();
     } else {
         $logger->debug('indexer job did not receive user in arguments: ' . json_encode($arguments));
     }
 }
コード例 #5
0
ファイル: testindexjob.php プロジェクト: ntvis/search_lucene
 function testIndexJob()
 {
     // preparation
     $app = new Application();
     $container = $app->getContainer();
     $this->userSession->setUser(null);
     \OC_Util::tearDownFS();
     $job = new IndexJob();
     $job->run(array('user' => 'test'));
     // get an index
     /** @var Index $index */
     $index = $container->query('Index');
     // search for it
     /** @var QueryHit[] $hits */
     $hits = $index->find('foo');
     // get the document from the query hit
     $foundDoc = $hits[0]->getDocument();
     $this->assertSame('document.txt', basename($foundDoc->getFieldValue('path')));
 }
コード例 #6
0
ファイル: testindex.php プロジェクト: ntvis/search_lucene
 function testUpdate()
 {
     // preparation
     $app = new Application();
     $container = $app->getContainer();
     // get an index
     /** @var Index $index */
     $index = $container->query('Index');
     // add a document
     $doc = new Document();
     $doc->addField(Document\Field::Keyword('fileId', '1'));
     $doc->addField(Document\Field::Text('path', '/somewhere/deep/down/the/rabbit/hole', 'UTF-8'));
     $doc->addField(Document\Field::Text('users', 'alice', 'UTF-8'));
     $index->index->addDocument($doc);
     $index->commit();
     // search for it
     $idTerm = new Term('1', 'fileId');
     $idQuery = new Query\Term($idTerm);
     $query = new Query\Boolean();
     $query->addSubquery($idQuery);
     /** @var QueryHit $hit */
     $hits = $index->find($query);
     // get the document from the query hit
     $foundDoc = $hits[0]->getDocument();
     $this->assertEquals('alice', $foundDoc->getFieldValue('users'));
     // delete the document from the index
     //$index->index->delete($hit);
     // change the 'users' key of the document
     $foundDoc->addField(Document\Field::Text('users', 'bob', 'UTF-8'));
     $this->assertEquals('bob', $foundDoc->getFieldValue('users'));
     // add the document back to the index
     $index->updateFile($foundDoc, '1');
     $idTerm2 = new Term('1', 'fileId');
     $idQuery2 = new Query\Term($idTerm2);
     $query2 = new Query\Boolean();
     $query2->addSubquery($idQuery2);
     /** @var QueryHit $hit */
     $hits2 = $index->find($query2);
     // get the document from the query hit
     $foundDoc2 = $hits2[0]->getDocument();
     $this->assertEquals('bob', $foundDoc2->getFieldValue('users'));
 }
コード例 #7
0
ファイル: files.php プロジェクト: ntvis/search_lucene
 /**
  * handle file renames (triggers indexing and deletion)
  * 
  * @param $param array from postRenameFile-Hook
  */
 public static function renameFile(array $param)
 {
     $app = new Application();
     $container = $app->getContainer();
     if (!empty($param['oldpath'])) {
         //delete from lucene index
         $container->query('Index')->deleteFile($param['oldpath']);
     }
     if (!empty($param['newpath'])) {
         /** @var Folder $userFolder */
         $userFolder = $container->query('ServerContainer')->getUserFolder();
         $node = $userFolder->get($param['newpath']);
         // only index files
         if ($node instanceof File) {
             $mapper = $container->query('StatusMapper');
             $mapper->getOrCreateFromFileId($node->getId());
             self::indexFile(array('path' => $param['newpath']));
         }
     }
 }
コード例 #8
0
ファイル: indexjob.php プロジェクト: ntvis/search_lucene
 /**
  * @param array $arguments
  */
 public function run($arguments)
 {
     if (!App::isEnabled('search_lucene')) {
         return;
     }
     $app = new Application();
     $container = $app->getContainer();
     /** @var Logger $logger */
     $logger = $container->query('Logger');
     if (isset($arguments['user'])) {
         $userId = $arguments['user'];
         $folder = $container->query('FileUtility')->setUpUserFolder($userId);
         if ($folder) {
             $fileIds = $container->query('StatusMapper')->getUnindexed();
             $logger->debug('background job indexing ' . count($fileIds) . ' files for ' . $userId);
             $container->query('Indexer')->indexFiles($fileIds);
         }
     } else {
         $logger->debug('indexer job did not receive user in arguments: ' . json_encode($arguments));
     }
 }
コード例 #9
0
ファイル: teststatus.php プロジェクト: ntvis/search_lucene
 /**
  * @dataProvider statusDataProvider
  */
 function testMarkingMethods($fileName, $method, $expectedStatus)
 {
     // preparation
     $fileId = $this->getFileId($fileName);
     $this->assertNotNull($fileId, 'Precondition failed: file id not found!');
     $app = new Application();
     $container = $app->getContainer();
     /** @var StatusMapper $mapper */
     $mapper = $container->query('StatusMapper');
     // run test
     $status = $mapper->getOrCreateFromFileId($fileId);
     $mapper->{$method}($status);
     $this->assertInstanceOf('OCA\\Search_Lucene\\Db\\Status', $status);
     $this->assertEquals($fileId, $status->getFileId());
     $this->assertEquals($expectedStatus, $status->getStatus());
     //check after loading from db
     $status2 = $mapper->getOrCreateFromFileId($fileId);
     $this->assertInstanceOf('OCA\\Search_Lucene\\Db\\Status', $status2);
     $this->assertEquals($fileId, $status2->getFileId());
     $this->assertEquals($status->getFileId(), $status2->getFileId());
     $this->assertEquals($expectedStatus, $status2->getStatus());
 }
コード例 #10
0
ファイル: testcase.php プロジェクト: ntvis/search_lucene
 public function tearDown()
 {
     if (is_null($this->storage)) {
         return;
     }
     $cache = $this->storage->getCache();
     $ids = $cache->getAll();
     $cache->clear();
     $app = new Application();
     $container = $app->getContainer();
     /** @var StatusMapper $mapper */
     $mapper = $container->query('StatusMapper');
     foreach ($ids as $id) {
         $status = new Status($id);
         $mapper->delete($status);
     }
 }