Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve content search #7028

Merged
merged 4 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,10 @@ private function createMysqlFullTextQuery(SearchQuery $query, array $matchFields
$againstQuery = '';

foreach ($query->terms as $term) {
$againstQuery .= '+' . $this->prepareTerm($term) . ' ';
if (strlen($term) >= $this->minAndTermLength) {
$againstQuery .= '+';// Search with "AND" condition
}
$againstQuery .= $this->prepareTerm($term) . ' ';
}
foreach ($query->notTerms as $term) {
$againstQuery .= '-' . $this->prepareTerm($term) . ' ';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
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\ResultSet;
use humhub\modules\content\search\SearchRequest;
use humhub\modules\content\services\ContentSearchService;
Expand Down Expand Up @@ -91,7 +90,7 @@ private function getSearchRequest(): SearchRequest
return new SearchRequest();
}

private function getSearchResultByKeyword(string $keyword): ResultSet
protected function getSearchResultByKeyword(string $keyword): ResultSet
{
$request = $this->getSearchRequest();
$request->keyword = $keyword;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,39 @@

namespace humhub\modules\content\tests\codeception\unit\search;

use humhub\modules\content\models\Content;
use humhub\modules\content\search\driver\AbstractDriver;
use humhub\modules\content\search\driver\MysqlDriver;
use humhub\modules\post\models\Post;
use humhub\modules\space\models\Space;

class MysqlDriverTest extends AbstractDriverTestSuite
{
protected function createDriver(): AbstractDriver
{
return new MysqlDriver();
}

public function testKeywords()
{
parent::testKeywords();

$space = Space::findOne(['id' => 1]);

(new Post($space, Content::VISIBILITY_PUBLIC, ['message' => 'Apple & Banana']))->save();
$this->assertCount(1, $this->getSearchResultByKeyword('Apple & Banana')->results);
$this->assertCount(1, $this->getSearchResultByKeyword('"Apple & Banana"')->results);

(new Post($space, Content::VISIBILITY_PUBLIC, ['message' => 'Here\'s a sentence']))->save();
$this->assertCount(1, $this->getSearchResultByKeyword('Here\'s a sentence')->results);
$this->assertCount(1, $this->getSearchResultByKeyword('"Here\'s a sentence"')->results);

(new Post($space, Content::VISIBILITY_PUBLIC, ['message' => 'a ab bc']))->save();
$this->assertCount(0, $this->getSearchResultByKeyword('a ab bc')->results);
$this->assertCount(0, $this->getSearchResultByKeyword('"a ab bc"')->results);

(new Post($space, Content::VISIBILITY_PUBLIC, ['message' => 'Test with `code1234` in text']))->save();
$this->assertCount(0, $this->getSearchResultByKeyword('with code1234')->results);
$this->assertCount(1, $this->getSearchResultByKeyword('"with code1234"')->results);
}
}
5 changes: 3 additions & 2 deletions static/js/humhub/humhub.ui.additions.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,14 +304,15 @@ humhub.module('ui.additions', function (module, require, $) {
}

if (typeof words === 'string' && words !== '') {
words = words.split(/[^\p{Script=Latin}\d\-’']+/u)
words = words.match(/[^\s]+\/[^\s]+|"[^"]+"|[\p{L}\d]+(?:['’`]\p{L}+)?/gu)
.map(item => item.replace(/"/g, ''));
words = [...new Set(words)].sort((a, b) => b.length - a.length);
}
if (!Array.isArray(words)) {
return;
}

words.forEach(function (word) {
word = word.replace(/^([^a-z\d]*)(.+?)([^a-z\d]*)$/i, '$2');
$node.highlight(word);
word.indexOf("'") > -1 && $node.highlight(word.replace("'", '’'));
word.indexOf("’") > -1 && $node.highlight(word.replace('’', "'"));
Expand Down
Loading