Merge pull request #437 from jikan-me/hotfix/search2

Fixed bug around non anime/manga search
This commit is contained in:
Irfan (Nekomata) 2023-10-07 18:26:15 +05:00 committed by GitHub
commit 28ff8a20b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 59 additions and 11 deletions

View File

@ -93,4 +93,9 @@ class Character extends JikanApiSearchableModel
'name_kanji'
];
}
public function getTitleAttributeName(): string
{
return 'name';
}
}

View File

@ -98,4 +98,9 @@ class Club extends JikanApiSearchableModel
{
return ['name'];
}
public function getTitleAttributeName(): string
{
return 'name';
}
}

View File

@ -73,4 +73,9 @@ class GenreAnime extends JikanApiSearchableModel
'name'
];
}
public function getTitleAttributeName(): string
{
return 'name';
}
}

View File

@ -72,4 +72,9 @@ class GenreManga extends JikanApiSearchableModel
'name'
];
}
public function getTitleAttributeName(): string
{
return 'name';
}
}

View File

@ -88,4 +88,9 @@ abstract class JikanApiSearchableModel extends JikanApiModel implements Typesens
}
return preg_replace("/[^[:alnum:][:space:]]/u", ' ', $val) ?? "";
}
public function getTitleAttributeName(): string
{
return 'title';
}
}

View File

@ -78,4 +78,9 @@ class Magazine extends JikanApiSearchableModel
'name'
];
}
public function getTitleAttributeName(): string
{
return 'name';
}
}

View File

@ -104,4 +104,9 @@ class Person extends JikanApiSearchableModel
"alternate_names"
];
}
public function getTitleAttributeName(): string
{
return 'name';
}
}

View File

@ -77,4 +77,9 @@ class Producers extends JikanApiSearchableModel
'titles'
];
}
public function getTitleAttributeName(): string
{
return 'titles';
}
}

View File

@ -62,13 +62,15 @@ class TypeSenseScoutSearchService implements ScoutSearchService
}
$modelInstance = $this->repository->createEntity();
$modelTitleAttributeName = "title";
if ($modelInstance instanceof JikanApiSearchableModel) {
$options = $this->setQueryByWeights($options, $modelInstance);
$options = $this->setSortOrder($options, $modelInstance);
$options = $this->overrideSortingOrder($options, $modelInstance, $orderByField, $sortDirectionDescending);
$modelTitleAttributeName = $modelInstance->getTitleAttributeName();
}
$options = $this->adaptToShortQueries($query, $options);
$options = $this->adaptToShortQueries($query, $modelTitleAttributeName, $options);
$results = $documents->search($options);
$this->recordSearchTelemetry($query, $results);
@ -77,7 +79,7 @@ class TypeSenseScoutSearchService implements ScoutSearchService
};
}
private function adaptToShortQueries(string $query, array $options): array
private function adaptToShortQueries(string $query, string $modelTitleAttributeName, array $options): array
{
if (strlen($query) <= 3) {
$options['num_typos'] = 0;
@ -87,15 +89,19 @@ class TypeSenseScoutSearchService implements ScoutSearchService
$options['infix'] = 'off';
$options['prioritize_token_position'] = 'true';
if (Str::startsWith($options["sort_by"], "_text_match")) {
$options["sort_by"] = "_text_match:desc,title:asc";
if (array_key_exists("sort_by", $options)) {
if (Str::startsWith($options["sort_by"], "_text_match")) {
$options["sort_by"] = "_text_match:desc," . $modelTitleAttributeName .":asc";
} else {
// move text_match to the beginning if there is an orderby parameter set.
$options["sort_by"] = Str::replace("(buckets:". $this->jikanConfig->textMatchBuckets().")", "", $options["sort_by"]);
$parts = collect(explode(",", $options["sort_by"]));
$last = $parts->pop();
$parts = $parts->prepend($last);
$options["sort_by"] = $parts->implode(",");
}
} else {
// move text_match to the beginning if there is an orderby parameter set.
$options["sort_by"] = Str::replace("(buckets:". $this->jikanConfig->textMatchBuckets().")", "", $options["sort_by"]);
$parts = collect(explode(",", $options["sort_by"]));
$last = $parts->pop();
$parts = $parts->prepend($last);
$options["sort_by"] = $parts->implode(",");
$options["sort_by"] = "_text_match:desc," . $modelTitleAttributeName .":asc";
}
}
@ -122,7 +128,9 @@ class TypeSenseScoutSearchService implements ScoutSearchService
$sortBy .= ',';
}
$sortBy = rtrim($sortBy, ',');
$options['sort_by'] = $sortBy;
$options["sort_by"] = $sortBy;
} else if (is_null($sortByFields) && !array_key_exists("sort_by", $options)) {
$options["sort_by"] = "_text_match:desc";
}
return $options;