Skip to content

Commit f69b727

Browse files
committed
Split implicitwildcard to wildcardstart and wildcardend settings
1 parent 53b652a commit f69b727

File tree

4 files changed

+120
-75
lines changed

4 files changed

+120
-75
lines changed

classes/index_form.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,26 @@ public function definition() {
8989

9090
// Search Settings.
9191
$mform->addElement('header', 'searchsettings', get_string('searchsettings', 'search_elastic'));
92+
9293
$mform->addElement(
9394
'advcheckbox',
94-
'implicitwildcard',
95-
get_string ('implicitwildcard', 'search_elastic'),
96-
'Enable', array(), array(0, 1));
97-
$mform->setType('implicitwildcard', PARAM_INT);
98-
$mform->addHelpButton('implicitwildcard', 'implicitwildcard', 'search_elastic');
99-
if (isset($config->signing)) {
100-
$mform->setDefault('implicitwildcard', $config->signing);
101-
} else {
102-
$mform->setDefault('implicitwildcard', 0);
103-
}
95+
'wildcardend',
96+
get_string ('wildcardend', 'search_elastic'),
97+
get_string('enable'), array(), array(0, 1));
98+
$mform->setType('wildcardend', PARAM_INT);
99+
$mform->addHelpButton('wildcardend', 'wildcardend', 'search_elastic');
100+
$wildcardend = isset($config->wildcardend) ? $config->wildcardend : 0;
101+
$mform->setDefault('wildcardend', $wildcardend);
102+
103+
$mform->addElement(
104+
'advcheckbox',
105+
'wildcardstart',
106+
get_string ('wildcardstart', 'search_elastic'),
107+
get_string('enable'), array(), array(0, 1));
108+
$mform->setType('wildcardstart', PARAM_INT);
109+
$mform->addHelpButton('wildcardstart', 'wildcardstart', 'search_elastic');
110+
$wildcardstart = isset($config->wildcardstart) ? $config->wildcardstart : 0;
111+
$mform->setDefault('wildcardstart', $wildcardstart);
104112

105113
// Request Signing settings.
106114
$mform->addElement('header', 'signingsettings', get_string('signingsettings', 'search_elastic'));

classes/query.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,15 @@ private function get_search_fields() {
123123
}
124124

125125
/**
126-
* Add * wildcard to beginning and end of search string.
126+
* Add * wildcard to beginning and/or end of search string if required.
127127
*
128128
* @param string $q The query string.
129+
* @param bool $start Add a wildcard at the start?
130+
* @param bool $end Add a wildcard at the end?
131+
129132
* @return string $q The query string with added wildcards.
130133
*/
131-
private function add_wildcards($q) {
134+
private function add_wildcards($q, $start = false, $end = false) {
132135

133136
$terms = explode(" ", $q); // Break search string into individual words.
134137
$wildcardterms = array();
@@ -148,11 +151,15 @@ private function add_wildcards($q) {
148151
continue;
149152
}
150153

151-
// Add wild card to start of word.
152-
$term = '*' . trim($term, '*');
154+
if ($start) {
155+
// Add wild card to start of word.
156+
$term = '*' . ltrim($term, '*');
157+
}
153158

154-
// Add wild card to end of word.
155-
$term = rtrim($term, '*') . '*';
159+
if ($end) {
160+
// Add wild card to end of word.
161+
$term = rtrim($term, '*') . '*';
162+
}
156163

157164
$wildcardterms[] = $term;
158165
}
@@ -174,10 +181,11 @@ private function add_wildcards($q) {
174181
* @return array
175182
*/
176183
private function construct_q($q) {
177-
178-
if (get_config('search_elastic', 'implicitwildcard')) {
179-
$q = $this->add_wildcards($q);
180-
}
184+
$q = $this->add_wildcards(
185+
$q,
186+
get_config('search_elastic', 'wildcardstart'),
187+
get_config('search_elastic', 'wildcardend')
188+
);
181189

182190
$searchfields = $this->get_search_fields();
183191
$qobj = array('query_string' => array('query' => $q, 'fields' => $searchfields));

lang/en/search_elastic.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,12 @@
5353
$string['imagerecognitionsettingsdesc'] = 'Image recognition extracts details about the content of an image and adds these to the search index.
5454
5555
These settings control what process or service is used to extract data out of an image and how the image data is added to the search engine.';
56-
$string['implicitwildcard'] = 'Enable implicit wildcards';
57-
$string['implicitwildcard_help'] = 'When enabled Moodle will add implicit wildcards to search terms. This can improve behaviour of searches.
58-
For example: searching for "math" will become "*math*" prior to be sent to the search engine. This means the search will now match "math", "maths" and "mathematics".';
56+
$string['wildcardstart'] = 'Wildcard at the start';
57+
$string['wildcardstart_help'] = 'When enabled Moodle will add implicit wildcards at the start of search terms. This can improve behaviour of searches.
58+
For example: searching for "scrip" will become "*scrip" prior to be sent to the search engine. This means the search will now match "script" and "description".';
59+
$string['wildcardend'] = 'Wildcard at the end';
60+
$string['wildcardend_help'] = 'When enabled Moodle will add implicit wildcards at the end of search terms. This can improve behaviour of searches.
61+
For example: searching for "math" will become "math*" prior to be sent to the search engine. This means the search will now match "math", "maths" and "mathematics".';
5962
$string['indexfail'] = 'Failed to create index';
6063
$string['maxlabels'] = 'Maxiumum Labels';
6164
$string['maxlabels_help'] = 'The maximum number of result labels returned by Rekognition.';

tests/query_test.php

Lines changed: 78 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -374,66 +374,92 @@ public function test_get_query_add_filters_based_on_accessinfo() {
374374
$this->assertEquals($expected, $result['query']['bool']['filter']['bool']['must'][0]['terms']['contextid']);
375375
}
376376

377+
/**
378+
* A data provider for test_construct_wildcard.
379+
* @return array
380+
*/
381+
public function construct_wildcard_data_provider() {
382+
return [
383+
['test', null, null, 'test'],
384+
385+
['test', false, false, 'test'],
386+
['test', true, false, '*test'],
387+
['test', false, true, 'test*'],
388+
['test', true, true, '*test*'],
389+
390+
[' test ', false, false, 'test'],
391+
[' test ', true, false, '*test'],
392+
[' test ', false, true, 'test*'],
393+
[' test ', true, true, '*test*'],
394+
395+
['test* ', false, false, 'test*'],
396+
['test* ', true, false, '*test*'],
397+
['test* ', false, true, 'test*'],
398+
['test* ', true, true, '*test*'],
399+
400+
['test* *', false, false, 'test* *'],
401+
['test* *', true, false, '*test* *'],
402+
['test* *', false, true, 'test* *'],
403+
['test* *', true, true, '*test* *'],
404+
405+
['test**', false, false, 'test**'],
406+
['test**', true, false, '*test**'],
407+
['test**', false, true, 'test*'],
408+
['test**', true, true, '*test*'],
409+
410+
['*test*', false, false, '*test*'],
411+
['*test*', true, false, '*test*'],
412+
['*test*', false, true, '*test*'],
413+
['*test*', true, true, '*test*'],
414+
415+
['lazy brown dog', false, false, 'lazy brown dog'],
416+
['lazy brown dog', true, false, '*lazy *brown *dog'],
417+
['lazy brown dog', false, true, 'lazy* brown* dog*'],
418+
['lazy brown dog', true, true, '*lazy* *brown* *dog*'],
419+
420+
['lazy brown dog', false, false, 'lazy brown dog'],
421+
['lazy brown dog', true, false, '*lazy *brown *dog'],
422+
['lazy brown dog', false, true, 'lazy* brown* dog*'],
423+
['lazy brown dog', true, true, '*lazy* *brown* *dog*'],
424+
425+
['this and that', false, false, 'this and that'],
426+
['this and that', true, false, '*this and *that'],
427+
['this and that', false, true, 'this* and that*'],
428+
['this and that', true, true, '*this* and *that*'],
429+
430+
['this AND that', false, false, 'this AND that'],
431+
['this AND that', true, false, '*this AND *that'],
432+
['this AND that', false, true, 'this* AND that*'],
433+
['this AND that', true, true, '*this* AND *that*'],
434+
435+
['this or that', false, false, 'this or that'],
436+
['this or that', true, false, '*this or *that'],
437+
['this or that', false, true, 'this* or that*'],
438+
['this or that', true, true, '*this* or *that*'],
439+
440+
['this Or that', false, false, 'this Or that'],
441+
['this Or that', true, false, '*this Or *that'],
442+
['this Or that', false, true, 'this* Or that*'],
443+
['this Or that', true, true, '*this* Or *that*'],
444+
];
445+
}
377446

378447
/**
379448
* Test query add wildcards construction.
449+
* @dataProvider construct_wildcard_data_provider
450+
*
451+
* @param string $q The query string.
452+
* @param bool $start Add a wildcard at the start?
453+
* @param bool $end Add a wildcard at the end?
454+
* @param string $expected Expected result.
380455
*/
381-
public function test_construct_wildcard() {
456+
public function test_construct_wildcard($q, $start, $end, $expected) {
382457
// We're testing a private method, so we need to setup reflector magic.
383458
$method = new ReflectionMethod('\search_elastic\query', 'add_wildcards');
384459
$method->setAccessible(true); // Allow accessing of private method.
385460

386-
$q = 'test';
387-
$proxy = $method->invoke(new \search_elastic\query, $q); // Get result of invoked method.
388-
$this->assertEquals('*test*', $proxy);
389-
390-
$q = 'test ';
391-
$proxy = $method->invoke(new \search_elastic\query, $q); // Get result of invoked method.
392-
$this->assertEquals('*test*', $proxy);
393-
394-
$q = 'test* ';
395-
$proxy = $method->invoke(new \search_elastic\query, $q);
396-
$this->assertEquals('*test*', $proxy);
397-
398-
$q = 'test**';
399-
$proxy = $method->invoke(new \search_elastic\query, $q);
400-
$this->assertEquals('*test*', $proxy);
401-
402-
$q = ' *test';
403-
$proxy = $method->invoke(new \search_elastic\query, $q);
404-
$this->assertEquals('*test*', $proxy);
405-
406-
$q = '***test';
407-
$proxy = $method->invoke(new \search_elastic\query, $q);
408-
$this->assertEquals('*test*', $proxy);
409-
410-
$q = '*test*';
411-
$proxy = $method->invoke(new \search_elastic\query, $q);
412-
$this->assertEquals('*test*', $proxy);
413-
414-
$q = 'lazy brown dog';
415-
$proxy = $method->invoke(new \search_elastic\query, $q);
416-
$this->assertEquals('*lazy* *brown* *dog*', $proxy);
417-
418-
$q = 'lazy brown dog';
419-
$proxy = $method->invoke(new \search_elastic\query, $q);
420-
$this->assertEquals('*lazy* *brown* *dog*', $proxy);
421-
422-
$q = 'this and that';
423-
$proxy = $method->invoke(new \search_elastic\query, $q);
424-
$this->assertEquals('*this* and *that*', $proxy);
425-
426-
$q = 'this AND that';
427-
$proxy = $method->invoke(new \search_elastic\query, $q);
428-
$this->assertEquals('*this* AND *that*', $proxy);
429-
430-
$q = 'this or that';
431-
$proxy = $method->invoke(new \search_elastic\query, $q);
432-
$this->assertEquals('*this* or *that*', $proxy);
433-
434-
$q = 'this Or that';
435-
$proxy = $method->invoke(new \search_elastic\query, $q);
436-
$this->assertEquals('*this* Or *that*', $proxy);
461+
$proxy = $method->invoke(new \search_elastic\query, $q, $start, $end);
462+
$this->assertEquals($expected, $proxy);
437463
}
438464

439465
}

0 commit comments

Comments
 (0)