2011-03-10 14:12:06 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Implementation of search in lucene index
|
|
|
|
*
|
|
|
|
* @category DMS
|
2013-02-14 11:10:53 +00:00
|
|
|
* @package SeedDMS_Lucene
|
2011-03-10 14:12:06 +00:00
|
|
|
* @license GPL 2
|
|
|
|
* @version @version@
|
|
|
|
* @author Uwe Steinmann <uwe@steinmann.cx>
|
|
|
|
* @copyright Copyright (C) 2010, Uwe Steinmann
|
|
|
|
* @version Release: @package_version@
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class for searching in a lucene index.
|
|
|
|
*
|
|
|
|
* @category DMS
|
2013-02-14 11:10:53 +00:00
|
|
|
* @package SeedDMS_Lucene
|
2011-03-10 14:12:06 +00:00
|
|
|
* @version @version@
|
|
|
|
* @author Uwe Steinmann <uwe@steinmann.cx>
|
|
|
|
* @copyright Copyright (C) 2011, Uwe Steinmann
|
|
|
|
* @version Release: @package_version@
|
|
|
|
*/
|
2013-02-14 11:10:53 +00:00
|
|
|
class SeedDMS_Lucene_Search {
|
2011-03-10 14:12:06 +00:00
|
|
|
/**
|
|
|
|
* @var object $index lucene index
|
|
|
|
* @access protected
|
|
|
|
*/
|
|
|
|
protected $index;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new instance of the search
|
|
|
|
*
|
|
|
|
* @param object $index lucene index
|
2013-02-14 11:10:53 +00:00
|
|
|
* @return object instance of SeedDMS_Lucene_Search
|
2011-03-10 14:12:06 +00:00
|
|
|
*/
|
|
|
|
function __construct($index) { /* {{{ */
|
|
|
|
$this->index = $index;
|
|
|
|
$this->version = '@package_version@';
|
|
|
|
if($this->version[0] == '@')
|
|
|
|
$this->version = '3.0.0';
|
|
|
|
} /* }}} */
|
|
|
|
|
|
|
|
/**
|
2015-08-10 19:40:26 +00:00
|
|
|
* Get document from index
|
|
|
|
*
|
|
|
|
* @param object $index lucene index
|
|
|
|
* @return object instance of SeedDMS_Lucene_Document of false
|
|
|
|
*/
|
|
|
|
function getDocument($id) { /* {{{ */
|
2020-12-12 15:27:53 +00:00
|
|
|
$hits = $this->index->find('document_id:D'.$id);
|
|
|
|
return $hits ? $hits[0] : false;
|
|
|
|
} /* }}} */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get folder from index
|
|
|
|
*
|
|
|
|
* @param object $index lucene index
|
|
|
|
* @return object instance of SeedDMS_Lucene_Document of false
|
|
|
|
*/
|
|
|
|
function getFolder($id) { /* {{{ */
|
|
|
|
$hits = $this->index->find('document_id:F'.$id);
|
2015-08-10 19:40:26 +00:00
|
|
|
return $hits ? $hits[0] : false;
|
|
|
|
} /* }}} */
|
|
|
|
|
|
|
|
/**
|
2011-03-10 14:12:06 +00:00
|
|
|
* Search in index
|
|
|
|
*
|
|
|
|
* @param object $index lucene index
|
2013-02-14 11:10:53 +00:00
|
|
|
* @return object instance of SeedDMS_Lucene_Search
|
2011-03-10 14:12:06 +00:00
|
|
|
*/
|
2020-09-14 14:33:36 +00:00
|
|
|
function search($term, $fields=array(), $limit=array()) { /* {{{ */
|
2013-06-17 08:50:31 +00:00
|
|
|
$querystr = '';
|
2020-12-14 09:44:32 +00:00
|
|
|
$term = trim($term);
|
|
|
|
if($term) {
|
|
|
|
$querystr = substr($term, -1) != '*' ? $term.'*' : $term;
|
|
|
|
}
|
2020-09-14 07:33:01 +00:00
|
|
|
if(!empty($fields['owner'])) {
|
2020-09-12 11:40:53 +00:00
|
|
|
if(is_string($owner)) {
|
|
|
|
if($querystr)
|
|
|
|
$querystr .= ' && ';
|
|
|
|
$querystr .= 'owner:'.$owner;
|
|
|
|
} elseif(is_array($fields['owner'])) {
|
|
|
|
if($querystr)
|
|
|
|
$querystr .= ' && ';
|
|
|
|
$querystr .= '(owner:"';
|
|
|
|
$querystr .= implode('" || owner:"', $fields['owner']);
|
|
|
|
$querystr .= '")';
|
|
|
|
}
|
2012-10-09 09:57:28 +00:00
|
|
|
}
|
2020-09-14 07:33:01 +00:00
|
|
|
if(!empty($fields['category'])) {
|
2013-06-17 08:50:31 +00:00
|
|
|
if($querystr)
|
|
|
|
$querystr .= ' && ';
|
2020-09-12 11:40:53 +00:00
|
|
|
$querystr .= '(category:"';
|
|
|
|
$querystr .= implode('" || category:"', $fields['category']);
|
|
|
|
$querystr .= '")';
|
2011-03-10 14:12:06 +00:00
|
|
|
}
|
2020-09-14 07:33:01 +00:00
|
|
|
if(!empty($fields['status'])) {
|
2013-06-17 08:50:31 +00:00
|
|
|
if($querystr)
|
|
|
|
$querystr .= ' && ';
|
2020-09-12 11:40:53 +00:00
|
|
|
$querystr .= '(status:"';
|
|
|
|
$querystr .= implode('" || status:"', $fields['status']);
|
2013-06-17 08:50:31 +00:00
|
|
|
$querystr .= '")';
|
2011-03-10 14:12:06 +00:00
|
|
|
}
|
2020-09-14 07:33:01 +00:00
|
|
|
if(!empty($fields['user'])) {
|
2020-09-02 06:57:07 +00:00
|
|
|
if($querystr)
|
|
|
|
$querystr .= ' && ';
|
|
|
|
$querystr .= '(users:"';
|
2020-09-12 11:40:53 +00:00
|
|
|
$querystr .= implode('" || users:"', $fields['user']);
|
2020-09-02 06:57:07 +00:00
|
|
|
$querystr .= '")';
|
|
|
|
}
|
2020-12-17 17:33:16 +00:00
|
|
|
if(!empty($fields['rootFolder']) && $fields['rootFolder']->getFolderList()) {
|
|
|
|
if($querystr)
|
|
|
|
$querystr .= ' && ';
|
|
|
|
$querystr .= '(path:"';
|
2020-12-21 10:46:55 +00:00
|
|
|
$tmp[] = $fields['rootFolder']->getID();
|
|
|
|
$querystr .= implode('" && path:"', $tmp);
|
|
|
|
//$querystr .= $fields['rootFolder']->getFolderList().$fields['rootFolder']->getID().':';
|
2020-12-17 17:33:16 +00:00
|
|
|
$querystr .= '")';
|
|
|
|
}
|
|
|
|
if(!empty($fields['startFolder']) && $fields['startFolder']->getFolderList()) {
|
|
|
|
if($querystr)
|
|
|
|
$querystr .= ' && ';
|
|
|
|
$querystr .= '(path:"';
|
2020-12-21 10:46:55 +00:00
|
|
|
// $querystr .= str_replace(':', 'x', $fields['startFolder']->getFolderList().$fields['startFolder']->getID().':');
|
|
|
|
$tmp = array();//explode(':', substr($fields['startFolder']->getFolderList(), 1, -1));
|
|
|
|
$tmp[] = $fields['startFolder']->getID();
|
|
|
|
$querystr .= implode('" && path:"', $tmp);
|
|
|
|
// $querystr .= str_replace(':', ' ', $fields['startFolder']->getFolderList().$fields['startFolder']->getID());
|
2020-12-17 17:33:16 +00:00
|
|
|
$querystr .= '")';
|
|
|
|
}
|
2013-06-17 08:50:31 +00:00
|
|
|
try {
|
|
|
|
$query = Zend_Search_Lucene_Search_QueryParser::parse($querystr);
|
2013-08-13 18:20:12 +00:00
|
|
|
try {
|
|
|
|
$hits = $this->index->find($query);
|
|
|
|
$recs = array();
|
2020-09-14 14:33:36 +00:00
|
|
|
$c = 0;
|
2013-08-13 18:20:12 +00:00
|
|
|
foreach($hits as $hit) {
|
2020-09-14 14:33:36 +00:00
|
|
|
if($c >= $limit['offset'] && ($c-$limit['offset'] < $limit))
|
|
|
|
$recs[] = array('id'=>$hit->id, 'document_id'=>$hit->document_id);
|
|
|
|
$c++;
|
2013-08-13 18:20:12 +00:00
|
|
|
}
|
2020-09-12 11:40:53 +00:00
|
|
|
return array('count'=>count($hits), 'hits'=>$recs, 'facets'=>array());
|
2013-08-13 18:20:12 +00:00
|
|
|
} catch (Zend_Search_Lucene_Exception $e) {
|
2013-08-13 20:08:47 +00:00
|
|
|
return false;
|
2013-06-17 08:50:31 +00:00
|
|
|
}
|
|
|
|
} catch (Zend_Search_Lucene_Search_QueryParserException $e) {
|
2013-08-13 20:08:47 +00:00
|
|
|
return false;
|
2011-03-10 14:12:06 +00:00
|
|
|
}
|
|
|
|
} /* }}} */
|
|
|
|
}
|
|
|
|
?>
|