/** * NOTE - this is only used in Elgg 1.8 as comments are annotations * * @param type $hook * @param type $type * @param type $return * @param type $params * @return null */ function elgg_solr_comment_search($hook, $type, $return, $params) { $entities = array(); $select = array('start' => $params['offset'], 'rows' => $params['limit'] ? $params['limit'] : 10, 'fields' => array('id', 'container_guid', 'description', 'owner_guid', 'time_created', 'score')); if ($params['select'] && is_array($params['select'])) { $select = array_merge($select, $params['select']); } // create a client instance $client = elgg_solr_get_client(); // get an update query instance $query = $client->createSelect($select); $default_sort = array('score' => 'desc', 'time_created' => 'desc'); $sorts = $params['sorts'] ? $params['sorts'] : $default_sort; $query->addSorts($sorts); $description_boost = elgg_solr_get_description_boost(); // get the dismax component and set a boost query $dismax = $query->getEDisMax(); $qf = "description^{$description_boost}"; if ($params['qf']) { $qf = $params['qf']; } $dismax->setQueryFields($qf); $boostQuery = elgg_solr_get_boost_query(); if ($boostQuery) { $dismax->setBoostQuery($boostQuery); } // this query is now a dismax query $query->setQuery($params['query']); // make sure we're only getting comments $params['fq']['type'] = 'type:annotation'; $params['fq']['subtype'] = 'subtype:generic_comment'; $default_fq = elgg_solr_get_default_fq($params); if ($params['fq']) { $filter_queries = array_merge($default_fq, $params['fq']); } else { $filter_queries = $default_fq; } if (!empty($filter_queries)) { foreach ($filter_queries as $key => $value) { $query->createFilterQuery($key)->setQuery($value); } } // get highlighting component and apply settings $hl = $query->getHighlighting(); $hl->setFields(array('description')); $hl->setSimplePrefix('<span data-hl="elgg-solr">'); $hl->setSimplePostfix('</span>'); $fragsize = elgg_solr_get_fragsize(); if (isset($params['fragsize'])) { $fragsize = (int) $params['fragsize']; } $hl->setFragSize($fragsize); // this executes the query and returns the result try { $resultset = $client->select($query); } catch (Exception $e) { error_log($e->getMessage()); return null; } // Get the highlighted snippet try { $highlighting = $resultset->getHighlighting(); } catch (Exception $e) { error_log($e->getMessage()); return null; } // Count the total number of documents found by solr $count = $resultset->getNumFound(); $hl_prefix = elgg_solr_get_hl_prefix(); $hl_suffix = elgg_solr_get_hl_suffix(); $show_score = elgg_get_plugin_setting('show_score', 'elgg_solr'); $config = HTMLPurifier_Config::createDefault(); $purifier = new HTMLPurifier($config); foreach ($resultset as $document) { // comments entity_guid stored as container_guid in solr $entity = get_entity($document->container_guid); if (!$entity) { $entity = new ElggObject(); $entity->setVolatileData('search_unavailable_entity', TRUE); } // highlighting results can be fetched by document id (the field defined as uniquekey in this schema) $highlightedDoc = $highlighting->getResult($document->id); if ($highlightedDoc) { foreach ($highlightedDoc as $highlight) { $snippet = implode(' (...) ', $highlight); // get our highlight based on the wrapped tokens // note, this is to prevent partial html from breaking page layouts $match = array(); preg_match('/<span data-hl="elgg-solr">(.*)<\\/span>/', $snippet, $match); if ($match[1]) { $snippet = str_replace($match[1], $hl_prefix . $match[1] . $hl_suffix, $snippet); $snippet = $purifier->purify($snippet); } } } if (!$snippet) { $snippet = search_get_highlighted_relevant_substrings(elgg_get_excerpt($document->description), $params['query']); } if ($show_score == 'yes' && elgg_is_admin_logged_in()) { $snippet .= elgg_view('output/longtext', array('value' => elgg_echo('elgg_solr:relevancy', array($document->score)), 'class' => 'elgg-subtext')); } $comments_data = $entity->getVolatileData('search_comments_data'); if (!$comments_data) { $comments_data = array(); } $comments_data[] = array('annotation_id' => substr(strstr(elgg_strip_tags($document->id), ':'), 1), 'text' => $snippet, 'owner_guid' => $document->owner_guid, 'time_created' => $document->time_created); $entity->setVolatileData('search_comments_data', $comments_data); $entities[] = $entity; } return array('entities' => $entities, 'count' => $count); }
/** * Get comments that match the search parameters. * * @param string $hook Hook name * @param string $type Hook type * @param array $value Empty array * @param array $params Search parameters * @return array */ function search_comments_hook($hook, $type, $value, $params) { $db_prefix = elgg_get_config('dbprefix'); $query = sanitise_string($params['query']); $limit = sanitise_int($params['limit']); $offset = sanitise_int($params['offset']); $params['annotation_names'] = array('generic_comment', 'group_topic_post'); $params['joins'] = array("JOIN {$db_prefix}annotations a on e.guid = a.entity_guid", "JOIN {$db_prefix}metastrings msn on a.name_id = msn.id", "JOIN {$db_prefix}metastrings msv on a.value_id = msv.id"); $fields = array('string'); // force IN BOOLEAN MODE since fulltext isn't // available on metastrings (and boolean mode doesn't need it) $search_where = search_get_where_sql('msv', $fields, $params, FALSE); $container_and = ''; if ($params['container_guid'] && $params['container_guid'] !== ELGG_ENTITIES_ANY_VALUE) { $container_and = 'AND e.container_guid = ' . sanitise_int($params['container_guid']); } $e_access = get_access_sql_suffix('e'); $a_access = get_access_sql_suffix('a'); // @todo this can probably be done through the api.. $q = "SELECT count(DISTINCT a.id) as total FROM {$db_prefix}annotations a\n\t\tJOIN {$db_prefix}metastrings msn ON a.name_id = msn.id\n\t\tJOIN {$db_prefix}metastrings msv ON a.value_id = msv.id\n\t\tJOIN {$db_prefix}entities e ON a.entity_guid = e.guid\n\t\tWHERE msn.string IN ('generic_comment', 'group_topic_post')\n\t\t\tAND ({$search_where})\n\t\t\tAND {$e_access}\n\t\t\tAND {$a_access}\n\t\t\t{$container_and}\n\t\t"; if (!($result = get_data($q))) { return FALSE; } $count = $result[0]->total; // don't continue if nothing there... if (!$count) { return array('entities' => array(), 'count' => 0); } // no full text index on metastrings table if ($params['sort'] == 'relevance') { $params['sort'] = 'created'; } $order_by = search_get_order_by_sql('a', null, $params['sort'], $params['order']); if ($order_by) { $order_by = "ORDER BY {$order_by}"; } $q = "SELECT DISTINCT a.*, msv.string as comment FROM {$db_prefix}annotations a\n\t\tJOIN {$db_prefix}metastrings msn ON a.name_id = msn.id\n\t\tJOIN {$db_prefix}metastrings msv ON a.value_id = msv.id\n\t\tJOIN {$db_prefix}entities e ON a.entity_guid = e.guid\n\t\tWHERE msn.string IN ('generic_comment', 'group_topic_post')\n\t\t\tAND ({$search_where})\n\t\t\tAND {$e_access}\n\t\t\tAND {$a_access}\n\t\t\t{$container_and}\n\t\t\n\t\t{$order_by}\n\t\tLIMIT {$offset}, {$limit}\n\t\t"; $comments = get_data($q); // @todo if plugins are disabled causing subtypes // to be invalid and there are comments on entities of those subtypes, // the counts will be wrong here and results might not show up correctly, // especially on the search landing page, which only pulls out two results. // probably better to check against valid subtypes than to do what I'm doing. // need to return actual entities // add the volatile data for why these entities have been returned. $entities = array(); foreach ($comments as $comment) { $entity = get_entity($comment->entity_guid); // hic sunt dracones if (!$entity) { //continue; $entity = new ElggObject(); $entity->setVolatileData('search_unavailable_entity', TRUE); } $comment_str = search_get_highlighted_relevant_substrings($comment->comment, $query); $comments_data = $entity->getVolatileData('search_comments_data'); if (!$comments_data) { $comments_data = array(); } $comments_data[] = array('annotation_id' => $comment->id, 'text' => $comment_str, 'owner_guid' => $comment->owner_guid, 'time_created' => $comment->time_created); $entity->setVolatileData('search_comments_data', $comments_data); $entities[] = $entity; } return array('entities' => $entities, 'count' => $count); }
/** * Return default results for searches on comments. * * @param unknown_type $hook * @param unknown_type $type * @param unknown_type $value * @param unknown_type $params * @return unknown_type */ function search_comments_hook($hook, $type, $value, $params) { global $CONFIG; $query = sanitise_string($params['query']); $params['annotation_names'] = array('generic_comment', 'group_topic_post'); $params['joins'] = array("JOIN {$CONFIG->dbprefix}annotations a on e.guid = a.entity_guid", "JOIN {$CONFIG->dbprefix}metastrings msn on a.name_id = msn.id", "JOIN {$CONFIG->dbprefix}metastrings msv on a.value_id = msv.id"); $fields = array('string'); // force IN BOOLEAN MODE since fulltext isn't // available on metastrings (and boolean mode doesn't need it) $search_where = search_get_where_sql('msv', $fields, $params, FALSE); $e_access = get_access_sql_suffix('e'); $a_access = get_access_sql_suffix('a'); // @todo this can probably be done through the api.. $q = "SELECT DISTINCT a.*, msv.string as comment FROM {$CONFIG->dbprefix}annotations a\n\t\tJOIN {$CONFIG->dbprefix}metastrings msn ON a.name_id = msn.id\n\t\tJOIN {$CONFIG->dbprefix}metastrings msv ON a.value_id = msv.id\n\t\tJOIN {$CONFIG->dbprefix}entities e ON a.entity_guid = e.guid\n\t\tWHERE msn.string IN ('generic_comment', 'group_topic_post')\n\t\t\tAND ({$search_where})\n\t\t\tAND {$e_access}\n\t\t\tAND {$a_access}\n\n\t\tLIMIT {$params['offset']}, {$params['limit']}\n\t\t"; $comments = get_data($q); $q = "SELECT count(DISTINCT a.id) as total FROM {$CONFIG->dbprefix}annotations a\n\t\tJOIN {$CONFIG->dbprefix}metastrings msn ON a.name_id = msn.id\n\t\tJOIN {$CONFIG->dbprefix}metastrings msv ON a.value_id = msv.id\n\t\tJOIN {$CONFIG->dbprefix}entities e ON a.entity_guid = e.guid\n\t\tWHERE msn.string IN ('generic_comment', 'group_topic_post')\n\t\t\tAND ({$search_where})\n\t\t\tAND {$e_access}\n\t\t\tAND {$a_access}\n\t\t"; $result = get_data($q); $count = $result[0]->total; if (!is_array($comments)) { return FALSE; } // @todo if plugins are disabled causing subtypes // to be invalid and there are comments on entities of those subtypes, // the counts will be wrong here and results might not show up correctly, // especially on the search landing page, which only pulls out two results. // probably better to check against valid subtypes than to do what I'm doing. // need to return actual entities // add the volatile data for why these entities have been returned. $entities = array(); foreach ($comments as $comment) { $entity = get_entity($comment->entity_guid); // hic sunt dracones if (!$entity) { //continue; $entity = new ElggObject(); $entity->setVolatileData('search_unavailable_entity', TRUE); } $comment_str = search_get_highlighted_relevant_substrings($comment->comment, $query); $entity->setVolatileData('search_match_annotation_id', $comment->id); $entity->setVolatileData('search_matched_comment', $comment_str); $entity->setVolatileData('search_matched_comment_owner_guid', $comment->owner_guid); $entity->setVolatileData('search_matched_comment_time_created', $comment->time_created); $entities[] = $entity; } return array('entities' => $entities, 'count' => $count); }