Skip to content

Commit 1620688

Browse files
authored
Merge pull request #23 from aneno-m-e/add-unaccented-search
Implement unaccented search
2 parents 74c7aa7 + 48604fb commit 1620688

5 files changed

Lines changed: 53 additions & 22 deletions

File tree

README.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@ The following features are provided by this plugin:
1010
* Group support (new in Moodle 3.5)
1111
* Ordering by course and context (new in Moodle 3.5)
1212

13-
## Supported Moodle Versions
14-
This plugin currently supports Moodle:
15-
16-
* 3.1
17-
* 3.4
18-
* 3.5
19-
* 3.6
20-
* 3.9
21-
* 4.0
13+
## Supported Versions
14+
15+
| Moodle version | Branch |
16+
| ----------------- | ------------------ |
17+
| Moodle 4.4+ | `MOODLE_404_STABLE`|
18+
| Moodle 3.1 to 4.3 | `main` |
19+
| Totara 19 | `TOTARA_19_STABLE` |
20+
| Totara up to 18 | `main` |
2221

2322
## Requirements
2423

classes/engine.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,13 @@ public function execute_query($filters, $accessinfo, $limit = 0) {
112112
$highlightopen = self::HIGHLIGHT_START;
113113
$highlightclose = self::HIGHLIGHT_END;
114114

115-
$title = "ts_headline(x.title, websearch_to_tsquery(?), 'StartSel=$highlightopen, StopSel=$highlightclose') AS title";
115+
// Define dictionary to be used
116+
$dict = "'en'";
117+
118+
$title = "ts_headline(" . $dict . ", x.title, websearch_to_tsquery(" . $dict . ", ?), 'StartSel=$highlightopen, StopSel=$highlightclose') AS title";
116119
$fullselectparams[] = $data->q;
117120

118-
$content = "ts_headline(x.content, websearch_to_tsquery(?), 'StartSel=$highlightopen, StopSel=$highlightclose') AS content";
121+
$content = "ts_headline(" . $dict . ", x.content, websearch_to_tsquery(" . $dict . ", ?), 'StartSel=$highlightopen, StopSel=$highlightclose') AS content";
119122
$fullselectparams[] = $data->q;
120123

121124
// Fulltext ranking SQL fragment.
@@ -140,9 +143,9 @@ public function execute_query($filters, $accessinfo, $limit = 0) {
140143

141144
$rank = "(
142145
GREATEST (
143-
ts_rank(fulltextindex, websearch_to_tsquery(?)),
146+
ts_rank(fulltextindex, websearch_to_tsquery(" . $dict . ", ?)),
144147
MAX(
145-
ts_rank(filefulltextindex, websearch_to_tsquery(?))
148+
ts_rank(filefulltextindex, websearch_to_tsquery(" . $dict . ", ?))
146149
)
147150
)
148151
$courseboostsql $contextboostsql
@@ -290,9 +293,9 @@ public function execute_query($filters, $accessinfo, $limit = 0) {
290293

291294
// And finally the main query after applying all AND filters.
292295
if (!empty($data->q)) {
293-
$whereands[] = "t.fulltextindex @@ websearch_to_tsquery(?) ";
296+
$whereands[] = "t.fulltextindex @@ websearch_to_tsquery(" . $dict . ", ?) ";
294297
$whereparams[] = $data->q;
295-
$fileands[] = " f.fulltextindex @@ websearch_to_tsquery(?) ";
298+
$fileands[] = " f.fulltextindex @@ websearch_to_tsquery(" . $dict . ", ?) ";
296299
$fileparams[] = $data->q;
297300
}
298301

@@ -385,6 +388,9 @@ public function execute_query($filters, $accessinfo, $limit = 0) {
385388
public function add_document($document, $fileindexing = false) {
386389
global $DB;
387390

391+
// Define dictionary to be used
392+
$dict = "'en'";
393+
388394
$doc = (object)$document->export_for_engine();
389395

390396
$doc->docid = $doc->id;
@@ -400,10 +406,10 @@ public function add_document($document, $fileindexing = false) {
400406
}
401407

402408
$sql = "UPDATE {search_postgresfulltext} SET fulltextindex =
403-
setweight(to_tsvector(coalesce(title, '')), 'A') ||
404-
setweight(to_tsvector(coalesce(content, '')), 'B') ||
405-
setweight(to_tsvector(coalesce(description1, '')), 'C') ||
406-
setweight(to_tsvector(coalesce(description2, '')), 'C')
409+
setweight(to_tsvector(" . $dict . ", coalesce(title, '')), 'A') ||
410+
setweight(to_tsvector(" . $dict . ", coalesce(content, '')), 'B') ||
411+
setweight(to_tsvector(" . $dict . ", coalesce(description1, '')), 'C') ||
412+
setweight(to_tsvector(" . $dict . ", coalesce(description2, '')), 'C')
407413
WHERE id = ? ";
408414

409415
$DB->execute($sql, array($id));
@@ -584,6 +590,9 @@ protected function delete_indexed_file($fileid) {
584590
protected function add_stored_file($document, $storedfile) {
585591
global $DB, $CFG;
586592

593+
// Define dictionary to be used
594+
$dict = "'en'";
595+
587596
if ($storedfile->get_filesize() > ($this->config->maxindexfilekb * 1024) || $this->config->maxindexfilekb == 0 ) {
588597
echo "Skipping ".$storedfile->get_filename()." larger than {$this->config->maxindexfilekb} KB\n";
589598
return true;
@@ -602,7 +611,7 @@ protected function add_stored_file($document, $storedfile) {
602611
try {
603612
$sql = "UPDATE {search_postgresfulltext_file}
604613
SET title = :title, fileid = :fileid, modified = :modified, filecontenthash = :filecontenthash,
605-
fulltextindex = setweight(to_tsvector(:textdoc), 'B') || setweight(to_tsvector(:texttitle), 'A')
614+
fulltextindex = setweight(to_tsvector(" . $dict . ", :textdoc), 'B') || setweight(to_tsvector(" . $dict . ", :texttitle), 'A')
606615
WHERE id = :id";
607616

608617
$params = array(

db/install.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,12 @@ function xmldb_search_postgresfulltext_install() {
4242
$DB->execute("CREATE INDEX {search_postgresfulltext_file_index}
4343
ON {search_postgresfulltext_file} USING GIN (fulltextindex)");
4444

45+
$DB->execute("CREATE EXTENSION IF NOT EXISTS unaccent");
46+
47+
$DB->execute("DROP TEXT SEARCH CONFIGURATION IF EXISTS en");
48+
49+
$DB->execute("CREATE TEXT SEARCH CONFIGURATION en ( COPY = english)");
50+
51+
$DB->execute("ALTER TEXT SEARCH CONFIGURATION en
52+
ALTER MAPPING FOR hword, hword_part, word WITH unaccent, english_stem");
4553
}

db/upgrade.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,21 @@ function xmldb_search_postgresfulltext_upgrade($oldversion) {
5555
upgrade_plugin_savepoint(true, 2018040400, 'search', 'postgresfulltext');
5656
}
5757

58+
if ($oldversion < 2025052500) {
59+
60+
$DB->execute("CREATE EXTENSION IF NOT EXISTS unaccent");
61+
62+
$DB->execute("DROP TEXT SEARCH CONFIGURATION IF EXISTS en");
63+
64+
$DB->execute("CREATE TEXT SEARCH CONFIGURATION en ( COPY = english)");
65+
66+
$DB->execute("ALTER TEXT SEARCH CONFIGURATION en
67+
ALTER MAPPING FOR hword, hword_part, word WITH unaccent, english_stem");
68+
69+
// Postgresfulltext savepoint reached.
70+
upgrade_plugin_savepoint(true, 2025052500, 'search', 'postgresfulltext');
71+
}
72+
5873
return true;
5974

6075
}

version.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
defined('MOODLE_INTERNAL') || die();
2626

27-
$plugin->version = 2018051601;
28-
$plugin->requires = 2017051509;
27+
$plugin->version = 2025052500;
28+
$plugin->requires = 2025012300;
2929
$plugin->component = 'search_postgresfulltext';
3030
$plugin->maturity = MATURITY_STABLE;
3131
$plugin->release = '1.2 (Build 2018122700)';

0 commit comments

Comments
 (0)