Skip to content

Commit

Permalink
MDL-53226 search_simpledb: Use databases full-text search capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
danpoltawski authored and David Monllao committed Mar 20, 2018
1 parent 2ee2f53 commit b602463
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 14 deletions.
6 changes: 4 additions & 2 deletions search/classes/document.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,8 @@ public static function format_time_for_engine($timestamp) {
* @return string
*/
public static function format_string_for_engine($string) {
return $string;
//FIXME: this shouldn't be required. Where is bad utf8 coming from?
return fix_utf8($string);
}

/**
Expand All @@ -420,7 +421,8 @@ public static function format_string_for_engine($string) {
* @return string
*/
public static function format_text_for_engine($text) {
return $text;
//FIXME: this shouldn't be required. Where is bad utf8 coming from?
return fix_utf8($text);
}

/**
Expand Down
48 changes: 36 additions & 12 deletions search/engine/simpledb/classes/engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,40 @@ public function execute_query($filters, $usercontexts) {
}

// And finally the main query after applying all AND filters.
$ands[] = '(' .
$DB->sql_like('title', '?', false, false) . ' OR ' .
$DB->sql_like('content', '?', false, false) . ' OR ' .
$DB->sql_like('description1', '?', false, false) . ' OR ' .
$DB->sql_like('description2', '?', false, false) .
')';
$params[] = '%' . $data->q . '%';
$params[] = '%' . $data->q . '%';
$params[] = '%' . $data->q . '%';
$params[] = '%' . $data->q . '%';
switch ($DB->get_dbfamily()) {
case 'postgres':
$ands[] = "(" .
"to_tsvector('simple', title) @@ plainto_tsquery(?) OR ".
"to_tsvector('simple', content) @@ plainto_tsquery(?) OR ".
"to_tsvector('simple', description1) @@ plainto_tsquery(?) OR ".
"to_tsvector('simple', description2) @@ plainto_tsquery(?)".
")";
$params[] = $data->q;
$params[] = $data->q;
$params[] = $data->q;
$params[] = $data->q;
break;
case 'mysql':
$ands[] = "MATCH (title, content, description1, description2) AGAINST (?)";
$params[] = $data->q;
break;
case 'mssql':
$ands[] = "CONTAINS ((title, content, description1, description2), ?)";
$params[] = $data->q;
break;
default:
$ands[] = '(' .
$DB->sql_like('title', '?', false, false) . ' OR ' .
$DB->sql_like('content', '?', false, false) . ' OR ' .
$DB->sql_like('description1', '?', false, false) . ' OR ' .
$DB->sql_like('description2', '?', false, false) .
')';
$params[] = '%' . $data->q . '%';
$params[] = '%' . $data->q . '%';
$params[] = '%' . $data->q . '%';
$params[] = '%' . $data->q . '%';
break;
}

$recordset = $DB->get_recordset_sql($sql . implode(' AND ', $ands), $params, 0, \core_search\manager::MAX_RESULTS);

Expand Down Expand Up @@ -201,8 +225,8 @@ public function add_document($document, $fileindexing = false) {
$DB->insert_record('search_simpledb_index', $doc);
}

} catch (dml_exception $ex) {
debugging('dml error while trying to insert document with id ' . $doc->docid . ': ' . $e->getMessage(),
} catch (\dml_exception $ex) {
debugging('dml error while trying to insert document with id ' . $doc->docid . ': ' . $ex->getMessage(),
DEBUG_DEVELOPER);
}

Expand Down
50 changes: 50 additions & 0 deletions search/engine/simpledb/db/install.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Post installation and migration code.
*
* @package search_simpledb
* @copyright 2016 Dan Poltawski <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die;

function xmldb_search_simpledb_install() {
global $DB;

switch ($DB->get_dbfamily()) {
case 'postgres':
// TODO: There are a few other ways of doing this which avoid the need for individual indicies..
$DB->execute("CREATE INDEX psql_search_title ON {search_simpledb_index} USING gin(to_tsvector('simple', title))");
$DB->execute("CREATE INDEX psql_search_content ON {search_simpledb_index} USING gin(to_tsvector('simple', content))");
$DB->execute("CREATE INDEX psql_search_description1 ON {search_simpledb_index} USING gin(to_tsvector('simple', description1))");
$DB->execute("CREATE INDEX psql_search_description2 ON {search_simpledb_index} USING gin(to_tsvector('simple', description2))");
break;
case 'mysql':
$DB->execute("CREATE FULLTEXT INDEX mysql_search_index
ON {search_simpledb_index} (title, content, description1, description2)");
break;
case 'mssql':
//TODO: workout if fulltext search is installed... select SERVERPROPERTY('IsFullTextInstalled')
$DB->execute("CREATE FULLTEXT CATALOG {search_simpledb_catalog}");
$DB->execute("CREATE FULLTEXT INDEX ON {search_simpledb_index} (title, content, description1, description2)
KEY INDEX {searsimpinde_id_pk} ON {search_simpledb_catalog}");
break;
}
}

36 changes: 36 additions & 0 deletions search/engine/simpledb/db/uninstall.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Uninstall code.
*
* @package search_simpledb
* @copyright 2016 Dan Poltawski <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die;

function xmldb_search_simpledb_uninstall() {
global $DB;

switch ($DB->get_dbfamily()) {
case 'mssql':
$DB->execute("DROP FULLTEXT CATALOG {search_simpledb_catalog}");
break;
}
}

0 comments on commit b602463

Please sign in to comment.