MDL-53226 search_simpledb: Use databases full-text search capabilities

This commit is contained in:
Dan Poltawski
2018-03-20 18:33:03 +01:00
committed by David Monllao
parent 2ee2f53021
commit b602463f07
4 changed files with 126 additions and 14 deletions
+4 -2
View File
@@ -407,7 +407,8 @@ class document implements \renderable, \templatable {
* @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);
}
/**
@@ -420,7 +421,8 @@ class document implements \renderable, \templatable {
* @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);
}
/**
+36 -12
View File
@@ -120,16 +120,40 @@ class engine extends \core_search\engine {
}
// 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);
@@ -201,8 +225,8 @@ class engine extends \core_search\engine {
$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);
}
+50
View File
@@ -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
View File
@@ -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;
}
}