diff options
Diffstat (limited to 'mod/search')
-rw-r--r-- | mod/search/search_hooks.php | 76 | ||||
-rw-r--r-- | mod/search/start.php | 40 |
2 files changed, 71 insertions, 45 deletions
diff --git a/mod/search/search_hooks.php b/mod/search/search_hooks.php index 86a6b452f..516bb2098 100644 --- a/mod/search/search_hooks.php +++ b/mod/search/search_hooks.php @@ -24,18 +24,19 @@ function search_objects_hook($hook, $type, $value, $params) { $params['joins'] = array($join); $fields = array('title', 'description'); - $where = search_get_where_sql('oe', $fields, $params); + $where = search_get_where_sql('oe', $fields, $params, FALSE); $params['wheres'] = array($where); - - $entities = elgg_get_entities($params); $params['count'] = TRUE; $count = elgg_get_entities($params); - + // no need to continue if nothing here. if (!$count) { return array('entities' => array(), 'count' => $count); } + + $params['count'] = FALSE; + $entities = elgg_get_entities($params); // add the volatile data for why these entities have been returned. foreach ($entities as $entity) { @@ -68,25 +69,28 @@ function search_groups_hook($hook, $type, $value, $params) { $join = "JOIN {$CONFIG->dbprefix}groups_entity ge ON e.guid = ge.guid"; $params['joins'] = array($join); + + $fields = array('name', 'description'); + + // force into boolean mode because we've having problems with the + // "if > 50% match 0 sets are returns" problem. + $where = search_get_where_sql('ge', $fields, $params, FALSE); - $where = "(ge.guid = e.guid - AND (ge.name LIKE '%$query%' - OR ge.description LIKE '%$query%' - ) - )"; $params['wheres'] = array($where); // override subtype -- All groups should be returned regardless of subtype. $params['subtype'] = ELGG_ENTITIES_ANY_VALUE; - $entities = elgg_get_entities($params); $params['count'] = TRUE; $count = elgg_get_entities($params); - + // no need to continue if nothing here. if (!$count) { return array('entities' => array(), 'count' => $count); } + + $params['count'] = FALSE; + $entities = elgg_get_entities($params); // add the volatile data for why these entities have been returned. foreach ($entities as $entity) { @@ -122,17 +126,20 @@ function search_users_hook($hook, $type, $value, $params) { $join = "JOIN {$CONFIG->dbprefix}users_entity ue ON e.guid = ue.guid"; $params['joins'] = array($join); - $where = "(ue.guid = e.guid - AND (ue.username LIKE '%$query%' - OR ue.name LIKE '%$query%' - ) - )"; +// $where = "(ue.guid = e.guid +// AND (ue.username LIKE '%$query%' +// OR ue.name LIKE '%$query%' +// ) +// )"; + + $fields = array('username', 'name'); + $where = search_get_where_sql('ue', $fields, $params, FALSE); + $params['wheres'] = array($where); // override subtype -- All users should be returned regardless of subtype. $params['subtype'] = ELGG_ENTITIES_ANY_VALUE; - $entities = elgg_get_entities($params); $params['count'] = TRUE; $count = elgg_get_entities($params); @@ -140,6 +147,9 @@ function search_users_hook($hook, $type, $value, $params) { if (!$count) { return array('entities' => array(), 'count' => $count); } + + $params['count'] = FALSE; + $entities = elgg_get_entities($params); // add the volatile data for why these entities have been returned. foreach ($entities as $entity) { @@ -212,7 +222,6 @@ function search_tags_hook($hook, $type, $value, $params) { $params['wheres'][] = "(msn.string IN ($tags_in) AND msv.string = '$query' AND $access)"; - $entities = elgg_get_entities($params); $params['count'] = TRUE; $count = elgg_get_entities($params); @@ -220,6 +229,9 @@ function search_tags_hook($hook, $type, $value, $params) { if (!$count) { return array('entities' => array(), 'count' => $count); } + + $params['count'] = FALSE; + $entities = elgg_get_entities($params); // add the volatile data for why these entities have been returned. foreach ($entities as $entity) { @@ -332,7 +344,7 @@ function search_comments_hook($hook, $type, $value, $params) { $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 + $q = "SELECT count(DISTINCT a.id) as total FROM {$CONFIG->dbprefix}annotations a JOIN {$CONFIG->dbprefix}metastrings msn ON a.name_id = msn.id JOIN {$CONFIG->dbprefix}metastrings msv ON a.value_id = msv.id JOIN {$CONFIG->dbprefix}entities e ON a.entity_guid = e.guid @@ -341,13 +353,20 @@ function search_comments_hook($hook, $type, $value, $params) { AND $e_access AND $a_access $container_and - - LIMIT {$params['offset']}, {$params['limit']} "; - $comments = get_data($q); - - $q = "SELECT count(DISTINCT a.id) as total FROM {$CONFIG->dbprefix}annotations a + 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); + } + + $q = "SELECT DISTINCT a.*, msv.string as comment FROM {$CONFIG->dbprefix}annotations a JOIN {$CONFIG->dbprefix}metastrings msn ON a.name_id = msn.id JOIN {$CONFIG->dbprefix}metastrings msv ON a.value_id = msv.id JOIN {$CONFIG->dbprefix}entities e ON a.entity_guid = e.guid @@ -356,14 +375,11 @@ function search_comments_hook($hook, $type, $value, $params) { AND $e_access AND $a_access $container_and - "; - $result = get_data($q); - $count = $result[0]->total; + LIMIT {$params['offset']}, {$params['limit']} + "; - if (!is_array($comments)) { - return FALSE; - } + $comments = get_data($q); // @todo if plugins are disabled causing subtypes // to be invalid and there are comments on entities of those subtypes, diff --git a/mod/search/start.php b/mod/search/start.php index 781afb8ec..ae5e71c97 100644 --- a/mod/search/start.php +++ b/mod/search/start.php @@ -259,8 +259,12 @@ function search_highlight_words($words, $string) { ); foreach ($words as $word) { + // remove any boolean mode operators + $word = preg_replace("/([\-\+~])([\w]+)/i", '$2', $word); + // escape the delimiter and any other regexp special chars $word = preg_quote($word, '/'); + $search = "/($word)/i"; // must replace with placeholders in case one of the search terms is @@ -297,7 +301,9 @@ function search_remove_ignored_words($query, $format = 'array') { global $CONFIG; // don't worry about "s or boolean operators - $query = str_replace(array('"', '-', '+', '~'), '', stripslashes(strip_tags($query))); + //$query = str_replace(array('"', '-', '+', '~'), '', stripslashes(strip_tags($query))); + $query = stripslashes(strip_tags($query)); + $words = explode(' ', $query); $min_chars = $CONFIG->search_info['min_chars']; @@ -386,12 +392,8 @@ function search_get_where_sql($table, $fields, $params, $use_fulltext = TRUE) { $fields[$i] = "$table.$field"; } } - - // if we're not using full text, rewrite the query for bool mode. - // exploiting a feature(ish) of bool mode where +-word is the same as -word - if (!$use_fulltext) { - $query = '+' . str_replace(' ', ' +', $query); - } + + $where = ''; // if query is shorter than the min for fts words // it's likely a single acronym or similar @@ -405,22 +407,30 @@ function search_get_where_sql($table, $fields, $params, $use_fulltext = TRUE) { $likes_str = implode(' OR ', $likes); $where = "($likes_str)"; } else { - // if using advanced or paired "s, switch into boolean mode - if (!$use_fulltext - || (isset($params['advanced_search']) && $params['advanced_search']) - || elgg_substr_count($query, '"') >= 2 ) { + // if we're not using full text, rewrite the query for bool mode. + // exploiting a feature(ish) of bool mode where +-word is the same as -word + if (!$use_fulltext) { + $query = '+' . str_replace(' ', ' +', $query); + } + + // if using advanced, boolean operators, or paired "s, switch into boolean mode + $booleans_used = preg_match("/([\-\+~])([\w]+)/i", $query); + $advanced_search = (isset($params['advanced_search']) && $params['advanced_search']); + $quotes_used = (elgg_substr_count($query, '"') >= 2); + + if (!$use_fulltext || $booleans_used || $advanced_search || $quotes_used) { $options = 'IN BOOLEAN MODE'; } else { // natural language mode is default and this keyword isn't supported in < 5.1 //$options = 'IN NATURAL LANGUAGE MODE'; $options = ''; } - + // if short query, use query expansion. // @todo doesn't seem to be working well. - if (elgg_strlen($query) < 5) { - //$options .= ' WITH QUERY EXPANSION'; - } +// if (elgg_strlen($query) < 5) { +// $options .= ' WITH QUERY EXPANSION'; +// } $query = sanitise_string($query); $fields_str = implode(',', $fields); |