/** * Returns a workspace list by json * * It can be filtered by by keyword. */ public function actionSearchJson() { $keyword = Yii::app()->request->getParam('keyword', ""); // guid of user/workspace $page = (int) Yii::app()->request->getParam('page', 1); // current page (pagination) $limit = (int) Yii::app()->request->getParam('limit', HSetting::Get('paginationSize')); // current page (pagination) $keyword = Yii::app()->input->stripClean($keyword); $hitCount = 0; $query = "model:Space "; if (strlen($keyword) > 2) { // Include Keyword if (strpos($keyword, "@") === false) { $keyword = str_replace(".", "", $keyword); $query .= "AND (title:" . $keyword . "* OR tags:" . $keyword . "*)"; } } //, $limit, $page $hits = new ArrayObject(HSearch::getInstance()->Find($query)); $hitCount = count($hits); // Limit Hits $hits = new LimitIterator($hits->getIterator(), ($page - 1) * $limit, $limit); $json = array(); #$json['totalHits'] = $hitCount; #$json['limit'] = $limit; #$results = array(); foreach ($hits as $hit) { $doc = $hit->getDocument(); $model = $doc->getField("model")->value; if ($model == "Space") { $workspaceId = $doc->getField('pk')->value; $workspace = Space::model()->findByPk($workspaceId); if ($workspace != null) { $wsInfo = array(); $wsInfo['guid'] = $workspace->guid; $wsInfo['title'] = CHtml::encode($workspace->name); $wsInfo['tags'] = CHtml::encode($workspace->tags); $wsInfo['image'] = $workspace->getProfileImage()->getUrl(); $wsInfo['link'] = $workspace->getUrl(); #$results[] = $wsInfo; $json[] = $wsInfo; } else { Yii::log("Could not load workspace with id " . $userId . " from search index!", CLogger::LEVEL_ERROR); } } else { Yii::log("Got no workspace hit from search index!", CLogger::LEVEL_ERROR); } } #$json['results'] = $results; print CJSON::encode($json); Yii::app()->end(); }
/** * JSON Search interface for Mentioning */ public function actionMentioning() { $results = array(); $keyword = Yii::app()->request->getParam('keyword', ""); $keyword = Yii::app()->input->stripClean(trim($keyword)); if (strlen($keyword) >= 3) { $hits = new ArrayObject(HSearch::getInstance()->Find($keyword . "* AND (model:User OR model:Space)")); $hitCount = count($hits); $hits = new LimitIterator($hits->getIterator(), 0, 10); foreach ($hits as $hit) { $doc = $hit->getDocument(); $model = $doc->getField('model')->value; $pk = $doc->getField('pk')->value; $object = $model::model()->findByPk($pk); if ($object !== null && $object instanceof HActiveRecordContentContainer) { $result = array(); $result['guid'] = $object->guid; if ($object instanceof Space) { $result['name'] = CHtml::encode($object->name); $result['type'] = 's'; } elseif ($object instanceof User) { $result['name'] = CHtml::encode($object->displayName); $result['type'] = 'u'; } $result['image'] = $object->getProfileImage()->getUrl(); $result['link'] = $object->getUrl(); $results[] = $result; } } } print CJSON::encode($results); }
/** * Hook the posts 'where' and search Lucene * We modify the search SQL to return the posts found from Lucene * * @param string $where WordPress SQL * @return string SQL **/ function posts_where( $where ) { $_GET['s'] = get_query_var( 's' ); $this->terms = array( $_GET['s'] ); $lucene = $this->open(); if ( $lucene ) { $modules = Search_Module_Factory::running(); $where = 'AND 1=2'; // Nothing was found try { $this->terms = array_filter( preg_split( '/[\s,]+/', trim( get_query_var( 's' ) ) ) ); $this->query = new Zend_Search_Lucene_Search_Query_Boolean(); $have_comments = false; // Add queries for all the modules foreach ( (array)$modules AS $module ) { if ( $module->is_comment() ) $have_comments = true; $sub = Zend_Search_Lucene_Search_QueryParser::parse( get_query_var( 's' ), get_option( 'blog_charset' ) ); $this->query->addSubquery( $sub, true ); if ( isset( $_GET[$module->field_name()] ) ) { $value = $module->field_value( $_GET[$module->field_name()] ); // XXX remove any braces from and value if ( $value !== false ) { $sub = Zend_Search_Lucene_Search_QueryParser::parse( $module->field_name().':('.$value.')', get_option( 'blog_charset' ) ); $this->query->addSubquery( $sub, true ); } } } // Add restrictions for status $this->query->addSubquery( Zend_Search_Lucene_Search_QueryParser::parse( $this->get_restricted_posts() ), true ); // Do the Lucene query $hits = new ArrayObject( $lucene->find( $this->query ) ); if ( count( $hits ) > 0 ) { global $wpdb; $page = get_query_var( 'paged' ) ? get_query_var( 'paged' ) - 1 : 0; $start = get_query_var( 'posts_per_page' ) * ( $page ); $this->total_hits = count( $hits ); if ( $have_comments ) $hits = new LimitIterator( $hits->getIterator(), 0 ); else $hits = new LimitIterator( $hits->getIterator(), $start ); foreach ( $hits AS $hit ) { $this->post_ids[] = $hit->post_id; if ( !$have_comments && count( $this->post_ids ) >= get_query_var( 'posts_per_page' ) ) break; } if ( $have_comments ) { $this->post_ids = array_unique( $this->post_ids ); $this->total_hits = count( $this->post_ids ); $this->post_ids = array_slice( $this->post_ids, $start, get_query_var( 'posts_per_page' ) ); } $where = "AND ID IN (".implode( ',', $this->post_ids ).')'; add_filter( 'the_posts', array( &$this, 'the_posts' ) ); } } catch( Zend_Search_Lucene_Exception $e ) { } } return $where; }
public function find($keyword, array $options) { $options = $this->setDefaultFindOptions($options); $index = $this->getIndex(); $keyword = str_replace(array('*', '?', '_', '$'), ' ', mb_strtolower($keyword, 'utf-8')); if (!isset($options['sortField']) || $options['sortField'] == "") { $hits = new \ArrayObject($index->find($this->buildQuery($keyword, $options))); } else { $hits = new \ArrayObject($index->find($this->buildQuery($keyword, $options), $options['sortField'])); } $resultSet = new SearchResultSet(); $resultSet->total = count($hits); $resultSet->pageSize = $options['pageSize']; $resultSet->page = $options['page']; $hits = new \LimitIterator($hits->getIterator(), ($options['page'] - 1) * $options['pageSize'], $options['pageSize']); foreach ($hits as $hit) { $document = $hit->getDocument(); $result = new SearchResult(); $result->model = $document->getField('model')->getUtf8Value(); $result->pk = $document->getField('pk')->getUtf8Value(); $result->type = $document->getField('type')->getUtf8Value(); $resultSet->results[] = $result; } return $resultSet; }
/** * search function * searches the index * * @param mixed $Model * @param mixed $query * @param int $limit * @param int $page * @access public * @return void */ function search(&$Model, $query, $limit = 20, $page = 1) { // open the index if (!$this->open_index($Model)) { return false; } try { // set the default encoding Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('utf-8'); // zend search results limiting (We will use the LimitIterator) // we can use it for some maximum value like 1000 if its likely that there could be more results Zend_Search_Lucene::setResultSetLimit(1000); // set the parser default operator to AND Zend_Search_Lucene_Search_QueryParser::setDefaultOperator(Zend_Search_Lucene_Search_QueryParser::B_AND); // utf-8 num analyzer Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8Num_CaseInsensitive()); // parse the query $Query = Zend_Search_Lucene_Search_QueryParser::parse($query); $Terms = $Query->getQueryTerms(); foreach ($Terms as $Term) { $this->terms[] = $Term->text; } // do the search $Hits = new ArrayObject($this->Index->find($Query)); } catch (Zend_Search_Lucene_Exception $e) { $this->log("Zend_Search_Lucene error: " . $e->getMessage(), 'searchable'); } $this->hits_count = count($Hits); if (!count($Hits)) { return null; } $Hits = new LimitIterator($Hits->getIterator(), ($page - 1) * $limit, $limit); $results = array(); foreach ($Hits as $Hit) { $Document = $Hit->getDocument(); $fields = $Document->getFieldNames(); $result = array(); foreach ($fields as $field) { $result['Result'][$field] = $Document->{$field}; } $results[] = $result; } return $results; }
/** * Space Section of directory * * Provides a list of all visible spaces. * * @todo Dont pass lucene hits to view, build user array inside of action */ public function actionSpaces() { $keyword = Yii::app()->request->getParam('keyword', ""); // guid of user/workspace $page = (int) Yii::app()->request->getParam('page', 1); // current page (pagination) $keyword = Yii::app()->input->stripClean($keyword); $hits = array(); $query = ""; $hitCount = 0; $sortField = null; $query = "model:Space"; if ($keyword != "") { $query .= " AND " . $keyword; } else { $sortField = 'title'; } //$hits = new ArrayObject( // HSearch::getInstance()->Find($query, HSetting::Get('paginationSize'), $page // )); $hits = new ArrayObject(HSearch::getInstance()->Find($query, $sortField)); $hitCount = count($hits); // Limit Hits $hits = new LimitIterator($hits->getIterator(), ($page - 1) * HSetting::Get('paginationSize'), HSetting::Get('paginationSize')); // Create Pagination Class $pages = new CPagination($hitCount); $pages->setPageSize(HSetting::Get('paginationSize')); $_GET['keyword'] = $keyword; // Fix for post var // Add Meber Statistic Sidebar Yii::app()->interceptor->preattachEventHandler('DirectorySidebarWidget', 'onInit', function ($event) { $event->sender->addWidget('application.modules_core.directory.widgets.NewSpacesWidget', array(), array('sortOrder' => 10)); $event->sender->addWidget('application.modules_core.directory.widgets.SpaceStatisticsWidget', array(), array('sortOrder' => 20)); }); $this->render('spaces', array('keyword' => $keyword, 'hits' => $hits, 'pages' => $pages, 'hitCount' => $hitCount, 'pageSize' => HSetting::Get('paginationSize'))); }
/** * SearchAction * * Modes: normal for full page, quick as partial for lightbox */ public function actionIndex() { // Get Parameters $keyword = Yii::app()->request->getParam('keyword', ""); $spaceGuid = Yii::app()->request->getParam('sguid', ""); $mode = Yii::app()->request->getParam('mode', "normal"); $page = (int) Yii::app()->request->getParam('page', 1); // current page (pagination) // Cleanup $keyword = Yii::app()->input->stripClean($keyword); $spaceGuid = Yii::app()->input->stripClean($spaceGuid); if ($mode != 'quick') { $mode = "normal"; } $limit = HSetting::Get('paginationSize'); // Show Hits $hitCount = 0; // Total Hit Count $query = ""; // Lucene Query $append = " AND (model:User OR model:Space)"; // Appends for Lucene Query $moreResults = false; // Indicates if there are more hits $results = array(); // Quick Search shows always 1 if ($mode == 'quick') { $limit = 5; } // Load also Space if requested $currentSpace = null; if ($spaceGuid) { $currentSpace = Space::model()->findByAttributes(array('guid' => $spaceGuid)); } /* * $index = new Zend_Search_Lucene_Interface_MultiSearcher(); * $index->addIndex(Zend_Search_Lucene::open('search/index1')); * $index->addIndex(Zend_Search_Lucene::open('search/index2')); * $index->find('someSearchQuery'); */ // Do Search if ($keyword != "") { if ($currentSpace != null) { $append = " AND (model:User OR model:Space OR (belongsToType:Space AND belongsToId:" . $currentSpace->id . "))"; } $hits = new ArrayObject(HSearch::getInstance()->Find($keyword . "* " . $append)); $hitCount = count($hits); // Limit Hits $hits = new LimitIterator($hits->getIterator(), ($page - 1) * $limit, $limit); if ($hitCount > $limit) { $moreResults = true; } // Build Results Array foreach ($hits as $hit) { $doc = $hit->getDocument(); $model = $doc->getField('model')->value; $pk = $doc->getField('pk')->value; $object = $model::model()->findByPk($pk); $results[] = $object->getSearchResult(); } } // Create Pagination Class $pages = new CPagination($hitCount); $pages->setPageSize($limit); $_GET['keyword'] = $keyword; // Fix for post var if ($mode == 'quick') { $this->renderPartial('quick', array('keyword' => $keyword, 'results' => $results, 'spaceGuid' => $spaceGuid, 'moreResults' => $moreResults, 'hitCount' => $hitCount)); } else { $this->render('index', array('keyword' => $keyword, 'results' => $results, 'spaceGuid' => $spaceGuid, 'moreResults' => $moreResults, 'pages' => $pages, 'pageSize' => $limit, 'hitCount' => $hitCount)); } }