Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit 89c13ba

Browse files
author
Charles Ma
committed
ENH: fixed bug #179 Added lucene search
1 parent b3d15c0 commit 89c13ba

File tree

10 files changed

+153
-30
lines changed

10 files changed

+153
-30
lines changed

core/Notification.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
class Notification extends MIDAS_Notification
1515
{
1616
public $_components = array('Utility');
17+
public $_models = array('User', 'Item');
1718

1819
/** init notification process*/
1920
public function init()
2021
{
2122
$this->addCallBack('CALLBACK_CORE_GET_DASHBOARD', 'getDasboard');
23+
$this->addTask('TASK_CORE_RESET_ITEM_INDEXES', 'resetItemIndexes', 'Recompute lucene indexes');
2224
}//end init
2325

2426
/** generate Dasboard information */
@@ -33,5 +35,26 @@ public function getDasboard()
3335

3436
return $return;
3537
}//end _getDasboard
38+
39+
40+
/** reset item indexes */
41+
public function resetItemIndexes()
42+
{
43+
$users = $this->User->getAll();
44+
foreach($users as $user)
45+
{
46+
$items = $this->Item->getOwnedByUser($user, 999999);
47+
foreach($items as $item)
48+
{
49+
$this->Item->save($item);
50+
}
51+
}
52+
53+
require_once BASE_PATH.'/core/controllers/components/SearchComponent.php';
54+
$component = new SearchComponent();
55+
$index = $component->getLuceneItemIndex();
56+
57+
$index->optimize();
58+
}
3659
} //end class
3760
?>

core/controllers/AdminController.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,34 @@ function resetdemoAction()
4040
$this->disableView();
4141
}
4242

43+
/** run a task **/
44+
function taskAction()
45+
{
46+
set_time_limit(0);
47+
if(!$this->logged)
48+
{
49+
$this->haveToBeLogged();
50+
return;
51+
}
52+
if(!$this->userSession->Dao->getAdmin() == 1)
53+
{
54+
throw new Zend_Exception("You should be an administrator");
55+
}
56+
57+
$task = $this->_getParam("task");
58+
$params = $this->_getParam("params");
59+
if(isset($params))
60+
{
61+
$params = JsonComponent::decode($params);
62+
}
63+
64+
$modules = Zend_Registry::get('notifier')->modules;
65+
$tasks = Zend_Registry::get('notifier')->tasks;
66+
call_user_func(array($modules[$tasks[$task]['module']],$tasks[$task]['method']), $params);
67+
$this->disableLayout();
68+
$this->disableView();
69+
}
70+
4371
/** index*/
4472
function indexAction()
4573
{

core/controllers/ItemController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ function viewAction()
113113
$element = $this->_getParam('element');
114114
$qualifier = $this->_getParam('qualifier');
115115
$value = $this->_getParam('value');
116-
if(isset($metadataId))
116+
if(isset($metadataId) && !empty($metadataIds))
117117
{
118118
$this->ItemRevision->deleteMetadata($itemRevision, $metadataId);
119119
}

core/controllers/components/SearchComponent.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,26 @@
1313
/** Search */
1414
class SearchComponent extends AppComponent
1515
{
16+
17+
/** get Zend Lucene index */
18+
public function getLuceneItemIndex()
19+
{
20+
$path = BASE_PATH.'/tmp/cache/searchIndex';
21+
if(!file_exists($path))
22+
{
23+
mkdir($path);
24+
}
25+
26+
$path .= '/item';
27+
if(!file_exists($path))
28+
{
29+
mkdir($path);
30+
Zend_Search_Lucene::create($path);
31+
}
32+
33+
return Zend_Search_Lucene::open($path);
34+
}
35+
1636
/** search all the results */
1737
public function searchAll($userDao, $search, $order)
1838
{

core/models/base/ItemModelBase.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,40 @@ public function save($dao)
6363
}
6464
$dao->setDateUpdate(date('c'));
6565
parent::save($dao);
66+
67+
require_once BASE_PATH.'/core/controllers/components/SearchComponent.php';
68+
$component = new SearchComponent();
69+
$index = $component->getLuceneItemIndex();
70+
71+
$hits = $index->find("item_id:".$dao->getKey());
72+
foreach ($hits as $hit)
73+
{
74+
$index->delete($hit->id);
75+
}
76+
$doc = new Zend_Search_Lucene_Document();
77+
$doc->addField(Zend_Search_Lucene_Field::Text('title', $dao->getName()));
78+
$doc->addField(Zend_Search_Lucene_Field::Keyword('item_id', $dao->getKey()));
79+
$doc->addField(Zend_Search_Lucene_Field::UnStored('description', $dao->getDescription()));
80+
81+
$modelLoad = new MIDAS_ModelLoader();
82+
$revisionModel = $modelLoad->loadModel('ItemRevision');
83+
$revision = $this->getLastRevision($dao);
84+
85+
$metadata = $revisionModel->getMetadata($revision);
86+
$metadataString = '';
87+
88+
foreach($metadata as $m)
89+
{
90+
$doc->addField(Zend_Search_Lucene_Field::Keyword($m->getElement().'-'.$m->getQualifier(), $m->getValue()));
91+
if(!is_numeric($m->getValue()))
92+
{
93+
$metadataString.=' '. $m->getValue();
94+
}
95+
}
96+
97+
$doc->addField(Zend_Search_Lucene_Field::Text('metadata', $metadataString));
98+
$index->addDocument($doc);
99+
$index->commit();
66100
}
67101

68102
/** copy parent folder policies*/

core/models/base/MetadataModelBase.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,18 @@ function addMetadataValue($itemRevisionDao, $type, $element, $qualifier, $value)
8585
{
8686
throw new Zend_Exception("This metadata value already exists for that revision.");
8787
}
88+
89+
$item = $itemRevisionDao->getItem();
90+
$modelLoader = new MIDAS_ModelLoader();
91+
$itemModel = $modelLoader->loadModel('Item');
92+
$lastrevision = $itemModel->getLastRevision($item);
93+
94+
//refresh zend search index
95+
if($lastrevision->getKey() == $itemRevisionDao->getKey())
96+
{
97+
$itemModel->save($item);
98+
}
99+
88100
$this->saveMetadataValue($metadataDao);
89101
} // end addMetadataValue()
90102

core/models/pdo/ItemKeywordModel.php

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -45,30 +45,21 @@ function getItemsFromSearch($searchterm, $userDao, $limit = 14, $group = true, $
4545
}
4646
}
4747

48-
$searchterms = explode(' ', $searchterm);
49-
// Apparently it's slow to do a like in a subquery so we run it first
50-
$sql = $this->database->select()->from(array('i' => 'itemkeyword'), array())
51-
->setIntegrityCheck(false);
52-
$sql->join(array('i2k' => 'item2keyword'), 'i.keyword_id = i2k.keyword_id', array('item_id'));
53-
54-
if(empty($searchterms))
48+
require_once BASE_PATH.'/core/controllers/components/SearchComponent.php';
49+
$component = new SearchComponent();
50+
$index = $component->getLuceneItemIndex();
51+
Zend_Search_Lucene_Search_QueryParser::setDefaultOperator(Zend_Search_Lucene_Search_QueryParser::B_AND);
52+
Zend_Search_Lucene::setResultSetLimit($limit * 3);
53+
if($group && strpos($searchterm, ':') === false)
5554
{
56-
return array();
55+
$rowset = $index->find('title:'.$searchterm);
5756
}
58-
59-
foreach($searchterms as $key => $term)
57+
else
6058
{
61-
if($key == 0)
62-
{
63-
$sql->where('value LIKE ?', '%'.$term.'%');
64-
}
65-
else
66-
{
67-
$sql->orWhere('value LIKE ?', '%'.$term.'%');
68-
}
59+
$rowset = $index->find($searchterm);
6960
}
70-
71-
$rowset = $this->database->fetchAll($sql);
61+
62+
7263
$return = array();
7364
$itemIdsCount = array();
7465
$itemIds = array();
@@ -85,14 +76,7 @@ function getItemsFromSearch($searchterm, $userDao, $limit = 14, $group = true, $
8576
}
8677
foreach($itemIdsCount as $key => $n)
8778
{
88-
if($n < count($searchterms))
89-
{
90-
unset($itemIdsCount[$key]);
91-
}
92-
else
93-
{
94-
$itemIds[] = $key;
95-
}
79+
$itemIds[] = $key;
9680
}
9781

9882
if(empty($itemIds))

core/models/pdo/ItemModel.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,17 @@ function delete($itemdao)
204204
$policy_user_model->delete($policy);
205205
}
206206

207+
208+
require_once BASE_PATH.'/core/controllers/components/SearchComponent.php';
209+
$component = new SearchComponent();
210+
$index = $component->getLuceneItemIndex();
211+
212+
$hits = $index->find("item_id:".$itemdao->getKey());
213+
foreach ($hits as $hit)
214+
{
215+
$index->delete($hit->id);
216+
}
217+
207218
parent::delete($itemdao);
208219
unset($itemdao->item_id);
209220
$itemdao->saved = false;

core/models/pdo/ItemRevisionModel.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ function deleteMetadata($revisiondao, $metadataId)
6060
}
6161

6262
Zend_Registry::get('dbAdapter')->delete('metadatavalue', 'itemrevision_id = '.$revisiondao->getKey().' AND metadata_id = '.$metadataId);
63+
64+
$item = $revisiondao->getItem();
65+
$modelLoader = new MIDAS_ModelLoader();
66+
$itemModel = $modelLoader->loadModel('Item');
67+
$lastrevision = $itemModel->getLastRevision($item);
68+
69+
//refresh zend search index
70+
if($lastrevision->getKey() == $revisiondao->getKey())
71+
{
72+
$itemModel->save($item);
73+
}
6374
return;
6475
} // end getMetadata
6576

notification/MIDASNotifier.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function getNotifications()
2525
}
2626

2727
/** get Tasks */
28-
public function getTaks()
28+
public function getTasks()
2929
{
3030
return $this->tasks;
3131
}

0 commit comments

Comments
 (0)