Fix search tests

This commit is contained in:
Yuriy Bakhtin 2024-03-25 12:39:21 +01:00
parent 2973661099
commit 22d2feb50b
2 changed files with 15 additions and 14 deletions

View File

@ -123,19 +123,19 @@ class MysqlDriver extends AbstractDriver
$againstQuery = '';
foreach ($query->andTerms as $keyword) {
$againstQuery .= '+' . $keyword . '* ';
$againstQuery .= '+' . rtrim($keyword, '*') . '* ';
}
foreach ($query->orTerms as $keyword) {
$againstQuery .= $keyword . '* ';
$againstQuery .= rtrim($keyword, '*') . '* ';
}
foreach ($query->orTerms as $keyword) {
$againstQuery .= $keyword . ' ';
foreach ($query->notTerms as $keyword) {
$againstQuery .= '-' . $keyword . ' ';
}
return sprintf(
'MATCH(%s) AGAINST (%s IN BOOLEAN MODE)',
implode(', ', $matchFields),
Yii::$app->db->quoteValue($againstQuery)
Yii::$app->db->quoteValue(trim($againstQuery))
);
}

View File

@ -5,7 +5,7 @@ namespace humhub\modules\content\tests\codeception\unit\search;
use humhub\modules\content\models\Content;
use humhub\modules\content\Module;
use humhub\modules\content\search\driver\AbstractDriver;
use humhub\modules\content\search\driver\MysqlDriver;
use humhub\modules\content\search\driver\ZendLucenceDriver;
use humhub\modules\content\search\ResultSet;
use humhub\modules\content\search\SearchRequest;
use humhub\modules\content\services\ContentSearchService;
@ -47,28 +47,29 @@ abstract class AbstractDriverTestSuite extends HumHubDbTestCase
#$this->assertEquals(1, count($this->getSearchResultByKeyword('Marabru')->results));
$this->assertEquals(1, count($this->getSearchResultByKeyword('Marabru Leav Abcd')->results));
$this->assertEquals(0, count($this->getSearchResultByKeyword('+Marabru +Leav +Abcd')->results));
$this->assertEquals(0, count($this->getSearchResultByKeyword('+Marabru +Leav* +Abcd')->results));
$this->assertEquals(0, count($this->getSearchResultByKeyword('Marabru Leav +Abcd')->results));
$this->assertEquals(1, count($this->getSearchResultByKeyword('Some -Marabru')->results));
// Wildcards
$this->assertEquals(1, count($this->getSearchResultByKeyword('Marabr*')->results));
}
public function testShortKeywords()
{
if ($this->searchDriver instanceof MysqlDriver) {
// Not possible on MySQLDriver
return;
}
$space = Space::findOne(['id' => 1]);
$this->becomeUser('Admin');
(new Post($space, Content::VISIBILITY_PUBLIC, ['message' => 'Some Other']))->save();
(new Post($space, Content::VISIBILITY_PUBLIC, ['message' => 'Marabru Leav Test X']))->save();
// Short keywords
$this->assertEquals(0, count($this->getSearchResultByKeyword('M')->results));
$this->assertEquals(1, count($this->getSearchResultByKeyword('X')->results));
$this->assertEquals(0, count($this->getSearchResultByKeyword('R')->results));
$this->assertEquals(1, count($this->getSearchResultByKeyword('T')->results));
if ($this->searchDriver instanceof ZendLucenceDriver) {
// MysqlDriver can find only at least 2 char exist after "X"
$this->assertEquals(1, count($this->getSearchResultByKeyword('X')->results));
}
}
private function getSearchRequest(): SearchRequest