Merge pull request #491 from jikan-me/typesense-indexing-fix

 Fixed sort index issue with typesense indexing
This commit is contained in:
pushrbx 2024-02-01 13:56:19 +00:00 committed by GitHub
commit fb5067433c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 76 additions and 2 deletions

View File

@ -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:

View File

@ -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
]
]
];

View File

@ -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

View File

@ -75,4 +75,9 @@ class Profile extends JikanApiSearchableModel
"username"
];
}
public function getTitleAttributeName(): string
{
return "username";
}
}

View File

@ -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());
}
}
}

View 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']);
}
}