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

Include user tags in Beatmapset search #11763

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion app/Jobs/EsDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class EsDocument implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable;

private array $modelMeta;
protected array $modelMeta;

/**
* Create a new job instance.
Expand Down
25 changes: 25 additions & 0 deletions app/Jobs/EsDocumentUnique.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

declare(strict_types=1);

namespace App\Jobs;

use Illuminate\Contracts\Queue\ShouldBeUnique;

class EsDocumentUnique extends EsDocument implements ShouldBeUnique
{
public int $uniqueFor = 600;

public function __construct($model)
{
parent::__construct($model);
}

public function uniqueId(): string
{
return "{$this->modelMeta['class']}-{$this->modelMeta['id']}";
}
}
6 changes: 6 additions & 0 deletions app/Libraries/Search/BeatmapsetSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ public function getQuery()
->should(['term' => ['_id' => ['value' => $this->params->queryString, 'boost' => 100]]])
->should(QueryHelper::queryString($this->params->queryString, $partialMatchFields, 'or', 1 / count($terms)))
->should(QueryHelper::queryString($this->params->queryString, [], 'and'))
->should([
'nested' => [
'path' => 'beatmaps',
'query' => QueryHelper::queryString($this->params->queryString, ['beatmaps.top_tags'], 'or', 0.5 / count($terms)),
],
])
);
}

Expand Down
4 changes: 2 additions & 2 deletions app/Models/Beatmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace App\Models;

use App\Exceptions\InvariantException;
use App\Jobs\EsDocument;
use App\Jobs\EsDocumentUnique;
use App\Libraries\Transactions\AfterCommit;
use App\Traits\Memoizes;
use DB;
Expand Down Expand Up @@ -223,7 +223,7 @@ public function afterCommit()
$beatmapset = $this->beatmapset;

if ($beatmapset !== null) {
dispatch(new EsDocument($beatmapset));
dispatch(new EsDocumentUnique($beatmapset));
}
}

Expand Down
4 changes: 2 additions & 2 deletions app/Models/Beatmapset.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use App\Exceptions\ImageProcessorServiceException;
use App\Exceptions\InvariantException;
use App\Jobs\CheckBeatmapsetCovers;
use App\Jobs\EsDocument;
use App\Jobs\EsDocumentUnique;
use App\Jobs\Notifications\BeatmapsetDiscussionLock;
use App\Jobs\Notifications\BeatmapsetDiscussionUnlock;
use App\Jobs\Notifications\BeatmapsetDisqualify;
Expand Down Expand Up @@ -1502,7 +1502,7 @@ public function refreshCache(bool $resetEligibleMainRulesets = false): void

public function afterCommit()
{
dispatch(new EsDocument($this));
dispatch(new EsDocumentUnique($this));
}

public function notificationCover()
Expand Down
36 changes: 24 additions & 12 deletions app/Models/Traits/Es/BeatmapsetSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,17 @@ private function esBeatmapsValues()
foreach ($this->beatmaps as $beatmap) {
$beatmapValues = [];
foreach ($mappings as $field => $mapping) {
$beatmapValues[$field] = $beatmap->$field;
$value = match ($field) {
'top_tags' => $this->esBeatmapTags($beatmap),
// TODO: remove adding $beatmap->user_id once everything else also populated beatmap_owners by default.
// Duplicate user_id in the array should be fine for now since the field isn't scored for querying.
'user_id' => $beatmap->beatmapOwners->pluck('user_id')->add($beatmap->user_id),
default => $beatmap->$field,
};

$beatmapValues[$field] = $value;
}

// TODO: remove adding $beatmap->user_id once everything else also populated beatmap_owners by default.
// Duplicate user_id in the array should be fine for now since the field isn't scored for querying.
$beatmapValues['user_id'] = $beatmap->beatmapOwners->pluck('user_id')->add($beatmap->user_id);
$values[] = $beatmapValues;

if ($beatmap->playmode === Beatmap::MODES['osu']) {
Expand All @@ -92,15 +97,10 @@ private function esBeatmapsValues()
continue;
}

$convert = clone $beatmap;
$convert->playmode = $modeInt;
$convert->convert = true;
$convertValues = [];
foreach ($mappings as $field => $mapping) {
$convertValues[$field] = $convert->$field;
}
$convertValues = $beatmapValues;
$convertValues['playmode'] = $modeInt;
$convertValues['convert'] = true;

$convertValues['user_id'] = $beatmapValues['user_id']; // just add a copy for converts too.
$values[] = $convertValues;
}
}
Expand All @@ -109,6 +109,18 @@ private function esBeatmapsValues()
return $values;
}

private function esBeatmapTags(Beatmap $beatmap): array
{
$tags = app('tags');

return array_filter(
array_map(
fn ($tagId) => $tags->get($tagId['tag_id'])?->name,
$beatmap->topTagIds()
)
);
}

private function esDifficultiesValues()
{
$mappings = static::esMappings()['difficulties']['properties'];
Expand Down
3 changes: 3 additions & 0 deletions config/schemas/beatmapsets.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@
"playmode": {
"type": "byte"
},
"top_tags": {
"type": "text"
},
"total_length": {
"type": "long"
},
Expand Down
Loading