Skip to content

Commit

Permalink
Merge pull request #491 from jikan-me/typesense-indexing-fix
Browse files Browse the repository at this point in the history
✅ Fixed sort index issue with typesense indexing
  • Loading branch information
pushrbx authored Feb 1, 2024
2 parents 02613f5 + f590e29 commit fb50674
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions app/JikanApiSearchableModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,20 @@ public abstract function typesenseQueryBy(): array;
*/
public function getCollectionSchema(): array
{
$titleAttributeName = $this->getTitleAttributeName();

return [
'name' => $this->searchableAs(),
'fields' => [
[
'name' => '.*',
'type' => 'auto',
],
[
'name' => $titleAttributeName,
'type' => 'string',
'sort' => true,
'optional' => false
]
]
];
Expand Down
4 changes: 3 additions & 1 deletion app/Producers.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ public static function scrape(int $id)

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
Expand Down
5 changes: 5 additions & 0 deletions app/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,9 @@ public function typesenseQueryBy(): array
"username"
];
}

public function getTitleAttributeName(): string
{
return "username";
}
}
5 changes: 4 additions & 1 deletion app/Testing/ScoutFlush.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ trait ScoutFlush
"App\\Person",
"App\\Club",
"App\\Magazine",
"App\\Producers"
"App\\Producers",
"App\\Profile",
];

public function runScoutFlush(): void
Expand All @@ -39,6 +40,8 @@ public function runScoutFlush(): void
"filter_by" => "mal_id:>0",
"batch_size" => 500
]);

$typeSenseClient->deleteCollection($modelInstance->searchableAs());
}
}
}
Expand Down
52 changes: 52 additions & 0 deletions tests/Unit/JikanApiSearchableModelTest.php
Original file line number Diff line number Diff line change
@@ -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']);
}
}

0 comments on commit fb50674

Please sign in to comment.