From d568a1e9fd63c97eb86cd84a51bb6770747ec37c Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sat, 17 Jan 2009 00:52:50 +0000 Subject: Implement relevance ranked boolean searching on a full text index of item and comment data. Whew! It's not pretty yet. And you have to manually update the index currently in admin/maintenance. But it works. --- modules/search/helpers/search.php | 77 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 modules/search/helpers/search.php (limited to 'modules/search/helpers/search.php') diff --git a/modules/search/helpers/search.php b/modules/search/helpers/search.php new file mode 100644 index 00000000..02c5f184 --- /dev/null +++ b/modules/search/helpers/search.php @@ -0,0 +1,77 @@ +escape_str($q); + + if (!user::active()->admin) { + foreach (user::group_ids() as $id) { + $fields[] = "`view_$id` = " . access::ALLOW; + } + $accesS_sql = "AND (" . join(" AND ", $fields) . ")"; + } else { + $access_sql = ""; + } + + // Count the total number of rows. We can't do this with our regular query because of the + // limit statement. It's possible that if we get rid of the limit (but keep the offset) on + // the 2nd query and combine the two, it might be faster than making 2 separate queries. + $count_query = "SELECT COUNT(*) AS C " . + "FROM `items` JOIN `search_records` ON (`items`.`id` = `search_records`.`item_id`) " . + "WHERE MATCH(`search_records`.`data`) AGAINST ('$q' IN BOOLEAN MODE) " . + $access_sql; + $count = $db->query($count_query)->current()->C; + + $query = "SELECT `items`.*, MATCH(`search_records`.`data`) AGAINST ('$q') AS `score` " . + "FROM `items` JOIN `search_records` ON (`items`.`id` = `search_records`.`item_id`) " . + "WHERE MATCH(`search_records`.`data`) AGAINST ('$q' IN BOOLEAN MODE) " . + $access_sql . + "ORDER BY `score` DESC " . + "LIMIT $offset, $limit"; + + return array($count, new ORM_Iterator(ORM::factory("item"), $db->query($query))); + } + + static function check_index() { + if ($count = ORM::factory("search_record")->where("dirty", 1)->count_all()) { + site_status::warning( + t("Your search index needs to be updated. %link_startFix this now%link_end", + array("link_start" => "", + "link_end" => "")), + "search_index_out_of_date"); + } + } + + static function update_record($record) { + $data = array(); + foreach (module::installed() as $module_name => $module_info) { + $class_name = "{$module_name}_search"; + if (method_exists($class_name, "item_index_data")) { + $data[] = call_user_func(array($class_name, "item_index_data"), $record->item()); + } + } + $record->data = join(" ", $data); + $record->dirty = 0; + $record->save(); + } +} -- cgit v1.2.3