pass a config array to search engine, search returns hits and total count

This commit is contained in:
Uwe Steinmann 2020-09-10 15:26:40 +02:00
parent e3e46b853d
commit 1fda490bbe
9 changed files with 66 additions and 63 deletions

View File

@ -29,17 +29,17 @@ class SeedDMS_Lucene_Indexer {
*/ */
protected $indexname; protected $indexname;
static function open($luceneDir) { /* {{{ */ static function open($conf) { /* {{{ */
try { try {
$index = Zend_Search_Lucene::open($luceneDir); $index = Zend_Search_Lucene::open($conf['indexdir']);
return($index); return($index);
} catch (Exception $e) { } catch (Exception $e) {
return null; return null;
} }
} /* }}} */ } /* }}} */
static function create($luceneDir) { /* {{{ */ static function create($conf) { /* {{{ */
$index = Zend_Search_Lucene::create($luceneDir); $index = Zend_Search_Lucene::create($conf['indexdir']);
return($index); return($index);
} /* }}} */ } /* }}} */

View File

@ -93,7 +93,7 @@ class SeedDMS_Lucene_Search {
foreach($hits as $hit) { foreach($hits as $hit) {
$recs[] = array('id'=>$hit->id, 'document_id'=>$hit->document_id); $recs[] = array('id'=>$hit->id, 'document_id'=>$hit->document_id);
} }
return $recs; return array('count'=>count($hits), 'hits'=>$recs);
} catch (Zend_Search_Lucene_Exception $e) { } catch (Zend_Search_Lucene_Exception $e) {
return false; return false;
} }

View File

@ -42,22 +42,22 @@ class SeedDMS_SQLiteFTS_Indexer {
* *
* @param string $indexerDir directory on disk containing the index * @param string $indexerDir directory on disk containing the index
*/ */
static function open($indexerDir) { /* {{{ */ static function open($conf) { /* {{{ */
if(file_exists($indexerDir.'/index.db')) { if(file_exists($conf['indexdir'].'/index.db')) {
return new SeedDMS_SQLiteFTS_Indexer($indexerDir); return new SeedDMS_SQLiteFTS_Indexer($conf['indexdir']);
} else } else
return self::create($indexerDir); return self::create($conf['indexdir']);
} /* }}} */ } /* }}} */
/** /**
* Create a new index * Create a new index
* *
* @param string $indexerDir directory on disk containing the index * @param array $conf $conf['indexdir'] is the directory on disk containing the index
*/ */
static function create($indexerDir) { /* {{{ */ static function create($conf) { /* {{{ */
if(file_exists($indexerDir.'/index.db')) if(file_exists($conf['indexdir'].'/index.db'))
unlink($indexerDir.'/index.db'); unlink($conf['indexdir'].'/index.db');
$index = new SeedDMS_SQLiteFTS_Indexer($indexerDir); $index = new SeedDMS_SQLiteFTS_Indexer($conf['indexdir']);
/* Make sure the sequence of fields is identical to the field list /* Make sure the sequence of fields is identical to the field list
* in SeedDMS_SQLiteFTS_Term * in SeedDMS_SQLiteFTS_Term
*/ */
@ -153,7 +153,7 @@ class SeedDMS_SQLiteFTS_Indexer {
$hits[] = $hit; $hits[] = $hit;
} }
} }
return $hits; return array('count'=>count($hits), 'hits'=>$hits);
} /* }}} */ } /* }}} */
/** /**

View File

@ -90,12 +90,12 @@ class SeedDMS_SQliteFTS_Search {
$querystr .= ''; $querystr .= '';
} }
try { try {
$hits = $this->index->find($querystr); $result = $this->index->find($querystr);
$recs = array(); $recs = array();
foreach($hits as $hit) { foreach($result["hits"] as $hit) {
$recs[] = array('id'=>$hit->id, 'document_id'=>$hit->id); $recs[] = array('id'=>$hit->id, 'document_id'=>$hit->id);
} }
return $recs; return array('count'=>$result['count'], 'hits'=>$recs);
} catch (Exception $e) { } catch (Exception $e) {
return false; return false;
} }

View File

@ -36,12 +36,16 @@ class SeedDMS_FulltextService {
*/ */
protected $maxsize; protected $maxsize;
private $index;
private $search;
public function __construct() { public function __construct() {
$this->services = array(); $this->services = array();
$this->converters = array(); $this->converters = array();
$this->maxsize = 0; $this->maxsize = 0;
$this->indexdir = '';
$this->index = null; $this->index = null;
$this->search = null;
$this->cmdtimeout = 5; $this->cmdtimeout = 5;
} }
@ -57,10 +61,6 @@ class SeedDMS_FulltextService {
$this->maxsize = $maxsize; $this->maxsize = $maxsize;
} }
public function setIndexDir($indexdir) {
$this->indexdir = $indexdir;
}
public function setCmdTimeout($timeout) { public function setCmdTimeout($timeout) {
$this->cmdtimeout = $timeout; $this->cmdtimeout = $timeout;
} }
@ -74,18 +74,25 @@ class SeedDMS_FulltextService {
if($this->index) if($this->index)
return $this->index; return $this->index;
if($this->indexdir) { if($this->services[0]) {
if($recreate) if($recreate)
$this->index = $this->services[0]['Indexer']::create($this->indexdir); $this->index = $this->services[0]['Indexer']::create($this->services[0]['Conf']);
else else
$this->index = $this->services[0]['Indexer']::open($this->indexdir); $this->index = $this->services[0]['Indexer']::open($this->services[0]['Conf']);
return $this->index; return $this->index;
} else } else
return null; return null;
} }
public function Search() { public function Search() {
return new $this->services[0]['Search']($this->index); if($this->search)
return $this->search;
if($this->services[0]) {
$this->search = new $this->services[0]['Search']($this->index);
return $this->search;
} else {
return null;
}
} }
} }

View File

@ -46,8 +46,6 @@ if(!$index) {
} }
if($view) { if($view) {
$view->setParam('luceneclassdir', $settings->_luceneClassDir);
$view->setParam('lucenedir', $settings->_luceneDir);
$view->setParam('index', $index); $view->setParam('index', $index);
$view($_GET); $view($_GET);
exit; exit;

View File

@ -128,23 +128,23 @@ if(isset($_GET["fullsearch"]) && $_GET["fullsearch"] && $settings->_enableFullSe
$startTime = getTime(); $startTime = getTime();
$index = $fulltextservice->Indexer(); $index = $fulltextservice->Indexer();
$lucenesearch = $fulltextservice->Search(); $lucenesearch = $fulltextservice->Search();
$hits = $lucenesearch->search($query, $owner ? $owner->getLogin() : '', '', $categorynames, array(), $user->isAdmin() ? [] : [$user->getLogin()]); $searchresult = $lucenesearch->search($query, $owner ? $owner->getLogin() : '', '', $categorynames, array(), $user->isAdmin() ? [] : [$user->getLogin()]);
if($hits === false) { if($searchresult === false) {
$session->setSplashMsg(array('type'=>'error', 'msg'=>getMLText('splash_invalid_searchterm'))); $session->setSplashMsg(array('type'=>'error', 'msg'=>getMLText('splash_invalid_searchterm')));
$totalPages = 0; $totalPages = 0;
$entries = array(); $entries = array();
$searchTime = 0; $searchTime = 0;
} else { } else {
$entries = array(); $entries = array();
$dcount = 0; $dcount = $searchresult['count']; //0;
$fcount = 0; $fcount = 0;
if($hits) { if($searchresult) {
foreach($hits as $hit) { foreach($searchresult['hits'] as $hit) {
if($tmp = $dms->getDocument($hit['document_id'])) { if($tmp = $dms->getDocument($hit['document_id'])) {
if($tmp->getAccessMode($user) >= M_READ) { if($tmp->getAccessMode($user) >= M_READ) {
$tmp->verifyLastestContentExpriry(); $tmp->verifyLastestContentExpriry();
$entries[] = $tmp; $entries[] = $tmp;
$dcount++; // $dcount++;
} }
} }
} }

View File

@ -34,8 +34,6 @@ class SeedDMS_View_IndexInfo extends SeedDMS_Bootstrap_Style {
function show() { /* {{{ */ function show() { /* {{{ */
$dms = $this->params['dms']; $dms = $this->params['dms'];
$user = $this->params['user']; $user = $this->params['user'];
$luceneclassdir = $this->params['luceneclassdir'];
$lucenedir = $this->params['lucenedir'];
$index = $this->params['index']; $index = $this->params['index'];
$this->htmlStartPage(getMLText('fulltext_info')); $this->htmlStartPage(getMLText('fulltext_info'));
@ -49,8 +47,8 @@ class SeedDMS_View_IndexInfo extends SeedDMS_Bootstrap_Style {
$this->contentContainerStart(); $this->contentContainerStart();
for ($id = 0; $id < $numDocs; $id++) { for ($id = 0; $id < $numDocs; $id++) {
if (!$index->isDeleted($id)) { if (!$index->isDeleted($id)) {
$hit = $index->getDocument($id); if($hit = $index->getDocument($id))
echo "<span title=\"".$hit->document_id."\">".htmlspecialchars($hit->title)."</span>\n"; echo "<span title=\"".$hit->document_id."\">".htmlspecialchars($hit->title)."</span>\n";
} }
} }
$this->contentContainerEnd(); $this->contentContainerEnd();
@ -67,7 +65,7 @@ class SeedDMS_View_IndexInfo extends SeedDMS_Bootstrap_Style {
$this->contentContainerStart(); $this->contentContainerStart();
$field = $term->field; $field = $term->field;
} }
echo htmlspecialchars($term->text)."\n"; echo "<span title=\"".$term->_occurrence."\">".htmlspecialchars($term->text)."</span>\n";
} }
$this->contentContainerEnd(); $this->contentContainerEnd();
// echo "</pre>"; // echo "</pre>";

View File

@ -37,34 +37,34 @@ class SeedDMS_View_Indexer_Process_Folder { /* {{{ */
public function process($folder) { /* {{{ */ public function process($folder) { /* {{{ */
$documents = $folder->getDocuments(); $documents = $folder->getDocuments();
if($documents) { if($documents) {
$lucenesearch = $this->fulltextservice->Search();
echo "<div class=\"folder\">".$folder->getFolderPathPlain()."</div>"; echo "<div class=\"folder\">".$folder->getFolderPathPlain()."</div>";
foreach($documents as $document) { foreach($documents as $document) {
echo "<div class=\"document\">".$document->getId().":".htmlspecialchars($document->getName()); echo "<div class=\"document\">".$document->getId().":".htmlspecialchars($document->getName());
/* If the document wasn't indexed before then just add it */ /* If the document wasn't indexed before then just add it */
$lucenesearch = $this->fulltextservice->Search(); if(!($hit = $lucenesearch->getDocument($document->getId()))) {
if(!($hit = $lucenesearch->getDocument($document->getId()))) {
echo " <span id=\"status_".$document->getID()."\" class=\"indexme indexstatus\" data-docid=\"".$document->getID()."\">".getMLText('index_waiting')."</span>";
} else {
/* Check if the attribute created is set or has a value older
* than the lastet content. Documents without such an attribute
* where added when a new document was added to the dms. In such
* a case the document content wasn't indexed.
*/
try {
$created = (int) $hit->getDocument()->getFieldValue('created');
} catch (/* Zend_Search_Lucene_ */Exception $e) {
$created = 0;
}
$content = $document->getLatestContent();
if($created >= $content->getDate() && !$this->forceupdate) {
echo "<span id=\"status_".$document->getID()."\" class=\"indexstatus\" data-docid=\"".$document->getID()."\">".getMLText('index_document_unchanged')."</span>";
} else {
$this->fulltextservice->Indexer()->delete($hit->id);
echo " <span id=\"status_".$document->getID()."\" class=\"indexme indexstatus\" data-docid=\"".$document->getID()."\">".getMLText('index_waiting')."</span>"; echo " <span id=\"status_".$document->getID()."\" class=\"indexme indexstatus\" data-docid=\"".$document->getID()."\">".getMLText('index_waiting')."</span>";
} else {
/* Check if the attribute created is set or has a value older
* than the lastet content. Documents without such an attribute
* where added when a new document was added to the dms. In such
* a case the document content wasn't indexed.
*/
try {
$created = (int) $hit->getDocument()->getFieldValue('created');
} catch (/* Zend_Search_Lucene_ */Exception $e) {
$created = 0;
}
$content = $document->getLatestContent();
if($created >= $content->getDate() && !$this->forceupdate) {
echo "<span id=\"status_".$document->getID()."\" class=\"indexstatus\" data-docid=\"".$document->getID()."\">".getMLText('index_document_unchanged')."</span>";
} else {
$this->fulltextservice->Indexer()->delete($hit->id);
echo " <span id=\"status_".$document->getID()."\" class=\"indexme indexstatus\" data-docid=\"".$document->getID()."\">".getMLText('index_waiting')."</span>";
}
} }
echo "</div>";
} }
echo "</div>";
}
} }
} /* }}} */ } /* }}} */
} /* }}} */ } /* }}} */