Пример #1
0
/**
 * 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);
}
Пример #2
0
function plugin_search($hook, $type, $return, $params)
{
    $select = array('start' => $params['offset'], 'rows' => $params['limit'], 'fields' => array('id', 'title', 'description'));
    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);
    $sorts = array('score' => 'desc', 'time_created' => 'desc');
    if ($params['sorts'] && is_array($params['sorts'])) {
        $sorts = $params['sorts'];
    }
    $query->addSorts($sorts);
    $title_boost = elgg_solr_get_title_boost();
    $description_boost = elgg_solr_get_description_boost();
    // get the dismax component and set a boost query
    $dismax = $query->getDisMax();
    $qf = "title^{$title_boost} description^{$description_boost}";
    if ($params['qf']) {
        $qf = $params['qf'];
    }
    $dismax->setQueryFields($qf);
    $dismax->setQueryAlternative('*:*');
    $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 objects:plugin_project
    $params['fq']['type'] = 'type:object';
    $params['fq']['subtype'] = 'subtype:plugin_project';
    if (($category = get_input('category')) && $category != 'all') {
        $params['fq']['plugincat'] = 'tags:"' . elgg_solr_escape_special_chars('plugincat%%' . $category) . '"';
    }
    $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('title', 'description'));
    $hl->setSimplePrefix('<strong class="search-highlight search-highlight-color1">');
    $hl->setSimplePostfix('</strong>');
    // 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();
    $search_results = array();
    foreach ($resultset as $document) {
        $search_results[$document->id] = array();
        $snippet = '';
        // 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 $field => $highlight) {
                $snippet = implode(' (...) ', $highlight);
                $snippet = search_get_highlighted_relevant_substrings(elgg_strip_tags($snippet), $params['query']);
                $search_results[$document->id][$field] = $snippet;
            }
        }
    }
    // get the entities
    $entities = array();
    $entities_unsorted = array();
    if ($search_results) {
        $entities_unsorted = elgg_get_entities(array('guids' => array_keys($search_results), 'limit' => false));
    }
    foreach ($search_results as $guid => $matches) {
        foreach ($entities_unsorted as $e) {
            if ($e->guid == $guid) {
                if ($matches['title']) {
                    $e->setVolatileData('search_matched_title', $matches['title']);
                } else {
                    $e->setVolatileData('search_matched_title', $e->title);
                }
                if ($matches['description']) {
                    $e->setVolatileData('search_matched_description', $matches['description']);
                } else {
                    $e->setVolatileData('search_matched_description', elgg_get_excerpt($e->description, 100));
                }
                $entities[] = $e;
            }
        }
    }
    return array('entities' => $entities, 'count' => $count);
}