Skip to content
Merged
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
25 changes: 22 additions & 3 deletions app/Libraries/Search/UserSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

class UserSearch extends RecordSearch
{
private const BOOST_GROUPS = [
2 => ['alumni', 'loved', 'beatmap_spotlights', 'tournament_staff'],
5 => ['ppy', 'gmt', 'nat', 'bng', 'bng_limited', 'dev', 'support', 'featured_artist'],
];

public function __construct(?UserSearchParams $params = null)
{
parent::__construct(
Expand Down Expand Up @@ -43,20 +48,34 @@ public function getQuery()
'fields' => ['username', 'username._*'],
];

static $boostGroups;

if (!isset($boostGroups)) {
$allGroupsByIdentifier = app('groups')->allByIdentifier();
foreach (self::BOOST_GROUPS as $boost => $identifiers) {
$boostGroups[$boost] = array_reject_null(array_map(fn ($identifier) => ($allGroupsByIdentifier[$identifier] ?? null)?->getKey(), $identifiers));
}
}

$query = (new BoolQuery())
->mustNot(['terms' => ['_id' => $this->params->blockedUserIds()]])
->mustNot(['term' => ['is_old' => true]])
->filter(['term' => ['user_warnings' => 0]])
->filter(['term' => ['user_type' => 0]]);

if ($this->params->queryString !== null) {
$query->shouldMatch(1)
$query->must((new BoolQuery())->shouldMatch(1)
->should(['term' => ['_id' => ['value' => $this->params->queryString, 'boost' => 100]]])
->should(['match' => ['username.raw' => ['query' => $this->params->queryString, 'boost' => 5]]])
->should(['match' => ['username.raw' => ['query' => $this->params->queryString, 'boost' => 10]]])
->should(['match' => ['previous_usernames' => ['query' => $this->params->queryString]]])
->should(['multi_match' => array_merge(['query' => $this->params->queryString], $lowercaseStick)])
->should(['multi_match' => array_merge(['query' => $this->params->queryString], $whitespaceStick)])
->should(['match_phrase' => ['username._slop' => $this->params->queryString]]);
->should(['match_phrase' => ['username._slop' => $this->params->queryString]]));

foreach ($boostGroups as $boost => $groupIds) {
$query->should(['terms' => ['groups' => $groupIds, 'boost' => $boost]]);
}
$query->should(['range' => ['user_lastvisit' => ['gte' => 'now-30d/d', 'boost' => 1.5]]]);
}

if ($this->params->recentOnly) {
Expand Down
4 changes: 3 additions & 1 deletion app/Models/Traits/Es/UserSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public static function esIndexName()
public static function esIndexingQuery()
{
return static::withoutGlobalScopes()
->with('userGroups')
->with('usernameChangeHistoryPublic');
}

Expand All @@ -30,8 +31,9 @@ protected function getEsFieldValue(string $field)
return match ($field) {
'id' => $this->getKey(),
'is_old' => $this->isOld(),
'previous_usernames' => $this->previousUsernames(true)->unique()->values(),
'previous_usernames' => $this->previousUsernames(true)->unique()->all(),
'user_lastvisit' => $this->displayed_last_visit,
'groups' => $this->userGroups->pluck('group_id')->all(),
default => $this->$field,
};
}
Expand Down
3 changes: 3 additions & 0 deletions config/schemas/users.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
"user_warnings": {
"type": "short"
},
"groups": {
"type": "integer"
},
"username": {
"type": "text",
"fields": {
Expand Down