diff --git a/framework/core/composer.json b/framework/core/composer.json
index 208bad0d4..b6f68c3bd 100644
--- a/framework/core/composer.json
+++ b/framework/core/composer.json
@@ -88,7 +88,8 @@
"wikimedia/less.php": "^3.2"
},
"require-dev": {
- "flarum/testing": "^1.0.0"
+ "flarum/testing": "^1.0.0",
+ "symfony/var-dumper": "^6.0"
},
"autoload": {
"psr-4": {
diff --git a/framework/core/src/Discussion/Search/Gambit/FulltextGambit.php b/framework/core/src/Discussion/Search/Gambit/FulltextGambit.php
index 20cd7227e..b5c636b21 100644
--- a/framework/core/src/Discussion/Search/Gambit/FulltextGambit.php
+++ b/framework/core/src/Discussion/Search/Gambit/FulltextGambit.php
@@ -30,8 +30,9 @@ class FulltextGambit implements GambitInterface
$query = $search->getQuery();
$grammar = $query->getGrammar();
+ // We give discussion title search score a higher weight than post.
$discussionSubquery = Discussion::select('id')
- ->selectRaw('NULL as score')
+ ->selectRaw('MATCH('.$grammar->wrap('discussions.title').') AGAINST (?)*1.5 as score', [$bit])
->selectRaw('first_post_id as most_relevant_post_id')
->whereRaw('MATCH('.$grammar->wrap('discussions.title').') AGAINST (? IN BOOLEAN MODE)', [$bit]);
@@ -61,8 +62,7 @@ class FulltextGambit implements GambitInterface
->groupBy('discussions.id')
->addBinding($subquery->getBindings(), 'join');
- $search->setDefaultSort(function ($query) use ($grammar, $bit) {
- $query->orderByRaw('MATCH('.$grammar->wrap('discussions.title').') AGAINST (?) desc', [$bit]);
+ $search->setDefaultSort(function ($query) {
$query->orderBy('posts_ft.score', 'desc');
});
diff --git a/framework/core/tests/integration/api/discussions/ListWithFulltextSearchTest.php b/framework/core/tests/integration/api/discussions/ListWithFulltextSearchTest.php
index 81694aefa..fe13ec5fb 100644
--- a/framework/core/tests/integration/api/discussions/ListWithFulltextSearchTest.php
+++ b/framework/core/tests/integration/api/discussions/ListWithFulltextSearchTest.php
@@ -36,17 +36,19 @@ class ListWithFulltextSearchTest extends TestCase
['id' => 3, 'title' => 'not in title either', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 1, 'comment_count' => 1],
['id' => 4, 'title' => 'not in title or text', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 1, 'comment_count' => 1],
['id' => 5, 'title' => 'తెలుగు', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 1, 'comment_count' => 1],
- ['id' => 6, 'title' => '支持中文吗', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 1, 'comment_count' => 1],
+ ['id' => 6, 'title' => '支持中文吗 acme', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 1, 'comment_count' => 1],
+ ['id' => 7, 'title' => 'add "acme for life" module!', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 1, 'comment_count' => 1],
]);
$this->database()->table('posts')->insert([
['id' => 1, 'discussion_id' => 1, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => 'not in text
'],
['id' => 2, 'discussion_id' => 2, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => 'lightsail in text
'],
- ['id' => 3, 'discussion_id' => 2, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => 'another lightsail for discussion 2!
'],
+ ['id' => 3, 'discussion_id' => 2, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => 'another lightsail for discussion 2! acme for life
'],
['id' => 4, 'discussion_id' => 3, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => 'just one lightsail for discussion 3.
'],
- ['id' => 5, 'discussion_id' => 4, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => 'not in title or text
'],
+ ['id' => 5, 'discussion_id' => 4, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => 'not in title or text (acme)
'],
['id' => 6, 'discussion_id' => 4, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => 'తెలుగు
'],
['id' => 7, 'discussion_id' => 2, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '支持中文吗
'],
+ ['id' => 8, 'discussion_id' => 5, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => 'some lorem ipsum acme dolor sit amet life kor for
'],
]);
// We need to call these again, since we rolled back the transaction started by `::app()`.
@@ -87,6 +89,27 @@ class ListWithFulltextSearchTest extends TestCase
$this->assertEqualsCanonicalizing(['2', '1', '3'], $ids, 'IDs do not match');
}
+ /**
+ * @test
+ */
+ public function search_prioritizes_title_search_score_over_post()
+ {
+ $response = $this->send(
+ $this->request('GET', '/api/discussions')
+ ->withQueryParams([
+ 'filter' => ['q' => 'acme for life'],
+ 'include' => 'mostRelevantPost',
+ ])
+ );
+
+ $data = json_decode($response->getBody()->getContents(), true);
+ $ids = array_map(function ($row) {
+ return $row['id'];
+ }, $data['data']);
+
+ $this->assertEquals(['7', '2', '5', '6', '4'], $ids, 'IDs do not match');
+ }
+
/**
* @test
*/