Skip to content

Commit 98abd6b

Browse files
authored
Merge pull request #12130 from nanaya/beatmap-es-index-speedup
Speed up beatmapset es indexing
2 parents 7f7d9ec + 0f188a5 commit 98abd6b

File tree

5 files changed

+29
-4
lines changed

5 files changed

+29
-4
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ CLIENT_CHECK_VERSION=false
326326

327327
# TAGS_CACHE_DURATION=60
328328
# BEATMAP_TAGS_CACHE_DURATION=60
329+
# BEATMAP_TOP_TAG_COUNT=50
329330

330331
# OSU_SENTRY_MIN_LOG_DURATION_MS=500
331332
# SENTRY_TRACES_SAMPLE_RATE=

app/Models/Beatmap.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ public function getAttribute($key)
307307
'baseMaxCombo',
308308
'beatmapDiscussions',
309309
'beatmapOwners',
310+
'beatmapTags',
310311
'beatmapset',
311312
'difficulty',
312313
'difficultyAttribs',
@@ -392,6 +393,22 @@ public function maxCombo()
392393
return $maxCombo?->value;
393394
}
394395

396+
public function slowTopTagIds(): array
397+
{
398+
return $this->memoize(__FUNCTION__, function () {
399+
$countById = [];
400+
foreach ($this->beatmapTags as $vote) {
401+
$countById[$vote->tag_id] ??= ['tag_id' => $vote->tag_id, 'count' => 0];
402+
$countById[$vote->tag_id]['count']++;
403+
}
404+
usort($countById, fn ($a, $b) => $a['count'] === $b['count']
405+
? $a['tag_id'] - $b['tag_id']
406+
: $b['count'] - $a['count']);
407+
408+
return array_slice($countById, 0, $GLOBALS['cfg']['osu']['tags']['top_tag_count']);
409+
});
410+
}
411+
395412
public function status()
396413
{
397414
return array_search($this->approved, Beatmapset::STATES, true);
@@ -405,7 +422,7 @@ public function topTagIds()
405422
fn () => \Cache::remember(
406423
"beatmap_top_tag_ids:{$this->getKey()}",
407424
$GLOBALS['cfg']['osu']['tags']['beatmap_tags_cache_duration'],
408-
fn () => $this->beatmapTags()->topTagIds()->limit(50)->get()->toArray(),
425+
fn () => $this->beatmapTags()->topTagIds()->limit($GLOBALS['cfg']['osu']['tags']['top_tag_count'])->get()->toArray(),
409426
),
410427
);
411428
}

app/Models/BeatmapTag.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,14 @@ class BeatmapTag extends Model
2525
protected $primaryKey = ':composite';
2626
protected $primaryKeys = ['beatmap_id', 'tag_id', 'user_id'];
2727

28+
public function scopeDefault(Builder $query): Builder
29+
{
30+
return $query->whereHas('user', fn ($userQuery) => $userQuery->default());
31+
}
32+
2833
public function scopeTopTagIds(Builder $query)
2934
{
30-
return $query->whereHas('user', fn ($userQuery) => $userQuery->default())
35+
return $query->default()
3136
->groupBy('tag_id')
3237
->select('tag_id')
3338
->selectRaw('COUNT(*) as count')

app/Models/Traits/Es/BeatmapsetSearch.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public static function esIndexingQuery()
2222
return static::withoutGlobalScopes()
2323
->active()
2424
->with('beatmaps') // note that the with query will run with the default scopes.
25+
->with(['beatmaps.beatmapTags' => fn ($q) => $q->default()])
2526
->with('beatmaps.beatmapOwners')
2627
->with('beatmaps.baseDifficultyRatings');
2728
}
@@ -38,7 +39,7 @@ private static function esBeatmapTags(Beatmap $beatmap): array
3839
return array_reject_null(
3940
array_map(
4041
fn ($tagId) => $tags->get($tagId['tag_id'])?->name,
41-
$beatmap->topTagIds()
42+
$beatmap->slowTopTagIds()
4243
)
4344
);
4445
}

config/osu.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,9 @@
205205
'notice' => presence(str_replace('\n', "\n", env('STORE_NOTICE') ?? '')),
206206
],
207207
'tags' => [
208-
'tags_cache_duration' => 60 * (get_int(env('TAGS_CACHE_DURATION')) ?? 60), // in minutes, converted to seconds
209208
'beatmap_tags_cache_duration' => 60 * (get_int(env('BEATMAP_TAGS_CACHE_DURATION')) ?? 60), // in minutes, converted to seconds
209+
'tags_cache_duration' => 60 * (get_int(env('TAGS_CACHE_DURATION')) ?? 60), // in minutes, converted to seconds
210+
'top_tag_count' => get_int(env('BEATMAP_TOP_TAG_COUNT')) ?? 50,
210211
],
211212
'team' => [
212213
'create_require_supporter' => get_bool(env('TEAM_CREATE_REQUIRE_SUPPORTER')) ?? false,

0 commit comments

Comments
 (0)