mirror of
https://github.com/jikan-me/jikan-rest.git
synced 2025-02-20 11:23:35 +08:00
Merge pull request #491 from jikan-me/typesense-indexing-fix
✅ Fixed sort index issue with typesense indexing
This commit is contained in:
commit
fb5067433c
4
.github/workflows/tests.yml
vendored
4
.github/workflows/tests.yml
vendored
@ -115,6 +115,10 @@ jobs:
|
||||
|
||||
run: ./vendor/bin/phpunit --coverage-clover coverage.xml
|
||||
|
||||
- name: Show logs after test failure
|
||||
if: failure()
|
||||
run: find ./storage/logs/daily/*.log | head -n 1 | xargs cat
|
||||
|
||||
- name: Upload coverage reports
|
||||
uses: codecov/codecov-action@v3
|
||||
with:
|
||||
|
@ -21,12 +21,20 @@ abstract class JikanApiSearchableModel extends JikanApiModel implements Typesens
|
||||
*/
|
||||
public function getCollectionSchema(): array
|
||||
{
|
||||
$titleAttributeName = $this->getTitleAttributeName();
|
||||
|
||||
return [
|
||||
'name' => $this->searchableAs(),
|
||||
'fields' => [
|
||||
[
|
||||
'name' => '.*',
|
||||
'type' => 'auto',
|
||||
],
|
||||
[
|
||||
'name' => $titleAttributeName,
|
||||
'type' => 'string',
|
||||
'sort' => true,
|
||||
'optional' => false
|
||||
]
|
||||
]
|
||||
];
|
||||
|
@ -59,11 +59,13 @@ class Producers extends JikanApiSearchableModel
|
||||
|
||||
public function toSearchableArray(): array
|
||||
{
|
||||
$titles = !is_null($this->titles) ? collect($this->titles)->map(fn ($x) => $x["title"])->toArray() : [''];
|
||||
|
||||
return [
|
||||
'id' => (string) $this->mal_id,
|
||||
'mal_id' => (int) $this->mal_id,
|
||||
'url' => !is_null($this->url) ? $this->url : '',
|
||||
'titles' => !is_null($this->titles) ? collect($this->titles)->map(fn ($x) => $x["title"])->toArray() : [''],
|
||||
'titles' => implode(', ', $titles),
|
||||
'established' => $this->convertToTimestamp($this->established),
|
||||
'favorites' => $this->favorites,
|
||||
'count' => $this->count
|
||||
|
@ -75,4 +75,9 @@ class Profile extends JikanApiSearchableModel
|
||||
"username"
|
||||
];
|
||||
}
|
||||
|
||||
public function getTitleAttributeName(): string
|
||||
{
|
||||
return "username";
|
||||
}
|
||||
}
|
||||
|
@ -18,7 +18,8 @@ trait ScoutFlush
|
||||
"App\\Person",
|
||||
"App\\Club",
|
||||
"App\\Magazine",
|
||||
"App\\Producers"
|
||||
"App\\Producers",
|
||||
"App\\Profile",
|
||||
];
|
||||
|
||||
public function runScoutFlush(): void
|
||||
@ -39,6 +40,8 @@ trait ScoutFlush
|
||||
"filter_by" => "mal_id:>0",
|
||||
"batch_size" => 500
|
||||
]);
|
||||
|
||||
$typeSenseClient->deleteCollection($modelInstance->searchableAs());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
52
tests/Unit/JikanApiSearchableModelTest.php
Normal file
52
tests/Unit/JikanApiSearchableModelTest.php
Normal file
@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use Tests\TestCase;
|
||||
|
||||
final class JikanApiSearchableModelFixture extends \App\JikanApiSearchableModel
|
||||
{
|
||||
public string $titleAttributeNameFixture;
|
||||
|
||||
public function typesenseQueryBy(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function getTitleAttributeName(): string
|
||||
{
|
||||
return $this->titleAttributeNameFixture;
|
||||
}
|
||||
}
|
||||
|
||||
final class JikanApiSearchableModelTest extends TestCase
|
||||
{
|
||||
public function titleFieldDataProvider()
|
||||
{
|
||||
return [
|
||||
["name"],
|
||||
["username"],
|
||||
["title"]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider titleFieldDataProvider
|
||||
*/
|
||||
public function testGetCollectionSchemaShouldReturnSortableTitleFieldInSchemaConfig($titleAttributeNameFixture)
|
||||
{
|
||||
$fixture = new JikanApiSearchableModelFixture();
|
||||
$fixture->titleAttributeNameFixture = $titleAttributeNameFixture;
|
||||
$schema = $fixture->getCollectionSchema();
|
||||
|
||||
$this->assertArrayHasKey('fields', $schema);
|
||||
$this->assertArrayHasKey('name', $schema['fields'][1]);
|
||||
$this->assertArrayHasKey('type', $schema['fields'][1]);
|
||||
$this->assertArrayHasKey('sort', $schema['fields'][1]);
|
||||
$this->assertArrayHasKey('optional', $schema['fields'][1]);
|
||||
$this->assertEquals($titleAttributeNameFixture, $schema['fields'][1]['name']);
|
||||
$this->assertEquals('string', $schema['fields'][1]['type']);
|
||||
$this->assertTrue($schema['fields'][1]['sort']);
|
||||
$this->assertFalse($schema['fields'][1]['optional']);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user