1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6namespace App\Models\Traits\Es;
7
8use Carbon\Carbon;
9
10trait UserSearch
11{
12 use BaseDbIndexable;
13
14 public static function esIndexName()
15 {
16 return $GLOBALS['cfg']['osu']['elasticsearch']['prefix'].'users';
17 }
18
19 public static function esIndexingQuery()
20 {
21 return static::withoutGlobalScopes()
22 ->with('usernameChangeHistoryPublic');
23 }
24
25 public static function esSchemaFile()
26 {
27 return config_path('schemas/users.json');
28 }
29
30 public function toEsJson()
31 {
32 $mappings = static::esMappings();
33
34 $document = [];
35 foreach ($mappings as $field => $mapping) {
36 $value = match ($field) {
37 'id' => $this->getKey(),
38 'is_old' => $this->isOld(),
39 'previous_usernames' => $this->previousUsernames(true)->unique()->values(),
40 'user_lastvisit' => $this->displayed_last_visit,
41 default => $this->$field,
42 };
43
44 if ($value instanceof Carbon) {
45 $value = $value->toIso8601String();
46 }
47
48 $document[$field] = $value;
49 }
50
51 return $document;
52 }
53}