function elgg_solr_group_search($hook, $type, $return, $params) { $select = array('start' => $params['offset'], 'rows' => $params['limit'] ? $params['limit'] : 10, 'fields' => array('id', 'name', 'description', '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_sorts = array('score' => 'desc', 'time_created' => 'desc'); $sorts = $params['sorts'] ? $params['sorts'] : $default_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->getEDisMax(); $qf = "name^{$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 groups $params['fq']['type'] = 'type:group'; $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(); $hlfields = array('name', 'description'); if ($params['hlfields']) { $hlfields = $params['hlfields']; } $hl->setFields($hlfields); $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(); $search_results = array(); $config = HTMLPurifier_Config::createDefault(); $purifier = new HTMLPurifier($config); 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); // get our highlight based on the wrapped tokens // note, this is to prevent partial html from breaking page layouts preg_match_all('/<span data-hl="elgg-solr">(.*?)<\\/span>/', $snippet, $match); if ($match[1]) { $matches = array_unique($match[1]); foreach ($matches as $m) { $snippet = str_replace($m, $hl_prefix . $m . $hl_suffix, $snippet); } $snippet = $purifier->purify($snippet); } $search_results[$document->id][$field] = $snippet; } } $search_results[$document->id]['score'] = $document->score; } // get the entities $entities = array(); $entities_unsorted = array(); if ($search_results) { $entities_unsorted = elgg_get_entities(array('guids' => array_keys($search_results), 'limit' => false)); } $show_score = elgg_get_plugin_setting('show_score', 'elgg_solr'); foreach ($search_results as $guid => $matches) { foreach ($entities_unsorted as $e) { $desc_suffix = ''; if ($show_score == 'yes' && elgg_is_admin_logged_in()) { $desc_suffix .= elgg_view('output/longtext', array('value' => elgg_echo('elgg_solr:relevancy', array($matches['score'])), 'class' => 'elgg-subtext')); } if ($e->guid == $guid) { if ($matches['name']) { $name = $matches['name']; $e->setVolatileData('search_matched_name', $name); $e->setVolatileData('search_matched_title', $name); } else { $name = $e->name; $e->setVolatileData('search_matched_name', $name); $e->setVolatileData('search_matched_title', $name); } if ($matches['description']) { $desc = $matches['description']; } else { $desc = search_get_highlighted_relevant_substrings($e->description, $params['query']); } unset($matches['name']); unset($matches['description']); unset($matches['score']); $desc .= implode('...', $matches); $e->setVolatileData('search_matched_description', $desc . $desc_suffix); $entities[] = $e; } } } return array('entities' => $entities, 'count' => $count); }
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); }