Skip to content

Commit

Permalink
Improve content search (#7028)
Browse files Browse the repository at this point in the history
* Improve content search

* Fix search with short word

* Improve URL highlighting on content search

* Test for mysql keyword search
  • Loading branch information
yurabakhtin committed May 27, 2024
1 parent 873d856 commit 7415609
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
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

0 comments on commit 7415609

Please sign in to comment.