Skip to content

Commit 89dc2fb

Browse files
committed
Don't add wild cards to an empty terms
1 parent 321ed49 commit 89dc2fb

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

classes/query.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,14 @@ private function add_wildcards($q) {
136136
// Add wildcards to start and end of words.
137137
foreach ($terms as $term) {
138138

139+
if (empty($term)) {
140+
continue;
141+
}
142+
143+
$term = str_replace(' ', '', $term);
144+
139145
// Ignore words: and, or.
140-
if ($term == 'and' || $term == 'or') {
146+
if (strtolower($term) == 'and' || strtolower($term) == 'or') {
141147
$wildcardterms[] = $term;
142148
continue;
143149
}

tests/query_test.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,11 +387,15 @@ public function test_construct_wildcard() {
387387
$proxy = $method->invoke(new \search_elastic\query, $q); // Get result of invoked method.
388388
$this->assertEquals('*test*', $proxy);
389389

390-
$q = 'test*';
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* ';
391395
$proxy = $method->invoke(new \search_elastic\query, $q);
392396
$this->assertEquals('*test*', $proxy);
393397

394-
$q = '*test';
398+
$q = ' *test';
395399
$proxy = $method->invoke(new \search_elastic\query, $q);
396400
$this->assertEquals('*test*', $proxy);
397401

@@ -403,13 +407,25 @@ public function test_construct_wildcard() {
403407
$proxy = $method->invoke(new \search_elastic\query, $q);
404408
$this->assertEquals('*lazy* *brown* *dog*', $proxy);
405409

410+
$q = 'lazy brown dog';
411+
$proxy = $method->invoke(new \search_elastic\query, $q);
412+
$this->assertEquals('*lazy* *brown* *dog*', $proxy);
413+
406414
$q = 'this and that';
407415
$proxy = $method->invoke(new \search_elastic\query, $q);
408416
$this->assertEquals('*this* and *that*', $proxy);
409417

418+
$q = 'this AND that';
419+
$proxy = $method->invoke(new \search_elastic\query, $q);
420+
$this->assertEquals('*this* AND *that*', $proxy);
421+
410422
$q = 'this or that';
411423
$proxy = $method->invoke(new \search_elastic\query, $q);
412424
$this->assertEquals('*this* or *that*', $proxy);
425+
426+
$q = 'this Or that';
427+
$proxy = $method->invoke(new \search_elastic\query, $q);
428+
$this->assertEquals('*this* Or *that*', $proxy);
413429
}
414430

415431
}

0 commit comments

Comments
 (0)