Skip to content

Commit c0b37a0

Browse files
committed
support testing for result order
1 parent 5c5a980 commit c0b37a0

5 files changed

Lines changed: 39 additions & 24 deletions

File tree

app/Libraries/Search/BeatmapsetSearch.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,30 @@ public function getQuery()
6363
$query = new BoolQuery();
6464

6565
if (!empty($this->tokens['include'])) {
66+
$implodedInclude = implode(' ', $this->tokens['include']);
6667
// the subscoping is not necessary but prevents unintentional accidents when combining other matchers
6768
$boolQuery = new BoolQuery()
68-
// results must contain at least one of the terms and boosted by containing all of them,
69-
// or match the id of the beatmapset.
69+
// results boosted by containing all terms, or match the id of the beatmapset.
7070
->shouldMatch(1)
71-
->should(['term' => ['_id' => ['value' => implode(' ', $this->tokens['include']), 'boost' => 100]]]);
71+
->should(['term' => ['_id' => ['value' => $implodedInclude, 'boost' => 100]]])
72+
->should([
73+
'multi_match' => [
74+
'fields' => $fullMatchFields,
75+
'type' => 'phrase',
76+
'query' => $implodedInclude,
77+
],
78+
]);
7279

80+
// Look for maybe relevant results.
81+
// "Something like this but I'm not exactly sure" kind of search.
7382
foreach ($this->tokens['include'] as $include) {
7483
$isQuoted = static::isQuoted($include);
7584
$boolQuery
7685
->should([
7786
'multi_match' => [
87+
'boost' => $isQuoted ? 1 : 1 / count($this->tokens['include']),
7888
'fields' => $isQuoted ? $fullMatchFields : $partialMatchFields,
79-
'type' => $isQuoted ? 'phrase' : 'most_fields',
89+
'type' => $isQuoted ? 'phrase' : 'cross_fields',
8090
'query' => $include,
8191
],
8292
])

database/factories/BeatmapsetFactory.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ public function definition(): array
4747
];
4848
}
4949

50+
// Set values that can affect search scoring to a consistent value.
51+
public function consistent()
52+
{
53+
return $this->state(['favourite_count' => 0]);
54+
}
55+
5056
public function deleted()
5157
{
5258
return $this->state(['deleted_at' => now()]);

tests/Libraries/Search/BeatmapsetSearch/QueryStringTest.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,16 @@ class QueryStringTest extends TestCase
1313
{
1414
public static function dataProvider(): array
1515
{
16-
// TODO: support scoring order in tests.
1716
return [
1817
[['q' => ''], [0, 1, 2, 3]],
1918
[['q' => '2'], [1]], // id only search.
2019
[['q' => '2 3'], []], // putting more than one id becomes a normal string search.
21-
[['q' => 'triangles'], [0, 1, 3]],
20+
[['q' => 'triangles'], [0, 1, 3], true],
2221
[['q' => '-triangles'], [2]],
23-
[['q' => 'triangles -revival'], [0, 3]],
22+
[['q' => 'triangles -revival'], [0, 3], true],
2423
[['q' => 'cross circle'], [2, 3]],
2524
[['q' => 'cross -circle'], []], // exclusion shouldn't add new matches.
26-
[['q' => 'tri'], [0, 1, 3]],
25+
[['q' => 'tri'], [0, 1, 3], true],
2726
[['q' => 'tri -circ'], [0, 1, 3]],
2827
[['q' => 'tri -circle'], [0, 1, 3]],
2928
[['q' => '-tri'], [0, 1, 2, 3]],
@@ -34,7 +33,7 @@ public static function setUpBeforeClass(): void
3433
{
3534
static::withDbAccess(function () {
3635
// use something besides empty string so it doesn't match empty string.
37-
$factory = Beatmapset::factory()->state(['artist' => 'a', 'creator' => 'a'])->ranked()->withBeatmaps();
36+
$factory = Beatmapset::factory()->consistent()->state(['artist' => 'a', 'creator' => 'a'])->ranked()->withBeatmaps();
3837
static::$beatmapsets = [
3938
$factory->create(['title' => 'Triangles']),
4039
$factory->create(['title' => 'Triangles Revival']),

tests/Libraries/Search/BeatmapsetSearch/TestCase.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,11 @@ public static function tearDownAfterClass(): void
7474
}
7575

7676
#[DataProvider('dataProvider')]
77-
public function testSearch(array $params, array $expected): void
77+
public function testSearch(array $params, array $expected, ?bool $order = null): void
7878
{
79-
$this->assertEqualsCanonicalizing(
80-
array_map(fn (int $index) => static::$beatmapsets[$index]->getKey(), $expected),
81-
new BeatmapsetSearch(new BeatmapsetSearchRequestParams($params))->response()->ids()
82-
);
79+
$ids = new BeatmapsetSearch(new BeatmapsetSearchRequestParams($params))->response()->ids();
80+
$method = $order ?? false ? 'assertEquals' : 'assertEqualsCanonicalizing';
81+
$this->$method(array_map(fn (int $index) => static::$beatmapsets[$index]->getKey(), $expected), $ids);
8382
}
8483

8584
protected function setUp(): void

tests/Libraries/Search/BeatmapsetSearch/TitleFilterTest.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,30 @@ class TitleFilterTest extends TestCase
1414
public static function dataProvider(): array
1515
{
1616
return [
17-
[['q' => 'best'], [0, 1, 2, 3]],
18-
[['q' => 'best beatmap'], [0, 1, 2, 3]],
19-
[['q' => '"best beatmap"'], [1, 2]],
17+
[['q' => 'best'], [0, 1, 2, 3, 4]],
18+
[['q' => 'best beatmap'], [3, 1, 2, 0, 4], true],
19+
[['q' => '"best beatmap"'], [3, 1, 2], true],
2020
[['q' => '-best'], []],
2121
[['q' => '-best -beatmap'], []],
22-
[['q' => '-"best beatmap"'], [0, 3]],
22+
[['q' => '-"best beatmap"'], [0, 4]],
2323

24-
[['q' => 'title=best'], [0, 1, 2]],
25-
[['q' => 'title="best beatmap"'], [1, 2]],
26-
[['q' => 'title="the beatmap"'], [1, 2]],
27-
[['q' => 'title=""best beatmap""'], [1, 2]],
24+
[['q' => 'title=best'], [0, 1, 2, 3]],
25+
[['q' => 'title="best beatmap"'], [1, 2, 3]],
26+
[['q' => 'title="the beatmap"'], [2, 1], true],
27+
[['q' => 'title=""best beatmap""'], [3, 2, 1], true],
2828
[['q' => 'title=""the beatmap""'], []],
2929
];
3030
}
3131

3232
public static function setUpBeforeClass(): void
3333
{
3434
static::withDbAccess(function () {
35-
$factory = Beatmapset::factory()->ranked()->withBeatmaps();
35+
$factory = Beatmapset::factory()->consistent()->ranked()->withBeatmaps();
3636
static::$beatmapsets = [
3737
$factory->create(['title' => 'best']),
3838
$factory->create(['title' => 'the best beatmap']),
39-
$factory->create(['title_unicode' => 'the best beatmapよ']),
39+
$factory->create(['title_unicode' => 'the best beatmapよ']), // this sets title as well, giving it higher field relevancy scoring.
40+
$factory->create(['title' => 'best beatmap']),
4041
$factory->create(['artist' => 'the best artist']),
4142
];
4243
});

0 commit comments

Comments
 (0)