Skip to content

Commit

Permalink
Search combining authority id and author name working
Browse files Browse the repository at this point in the history
  • Loading branch information
eronisko committed Mar 14, 2024
1 parent 64fc302 commit ba7b998
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 14 deletions.
21 changes: 20 additions & 1 deletion app/Http/Controllers/Api/V1/ItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class ItemController extends Controller
{
private $filterables = [
'author',
'authority_id',
'authors.name',
'authors.authority.id',
'topic',
'work_type',
'medium',
Expand Down Expand Up @@ -293,6 +294,7 @@ public function catalogTitle(Request $request)
protected function createQueryBuilder($q, $filter)
{
$builder = Query::bool();
$authorsBuilder = null;

if ($q) {
$query = Query::multiMatch()
Expand All @@ -312,6 +314,16 @@ protected function createQueryBuilder($q, $filter)
continue;
}

if ($field === 'authors.name' || $field === 'authors.authority.id') {
$authorsBuilder ??= Query::bool();
$authorsBuilder->should(
is_array($value)
? ['terms' => ["$field.keyword" => $value]]
: ['term' => ["$field.keyword" => $value]]
);
continue;
}

if (is_string($value) && in_array($field, $this->filterables, true)) {
if ($value === 'false') {
$value = false;
Expand All @@ -338,6 +350,13 @@ protected function createQueryBuilder($q, $filter)
}
}

if ($authorsBuilder) {
$builder->filter(
Query::nested()
->path('authors')
->query($authorsBuilder)
);
}
$builder->filter(['term' => ['frontend' => Frontend::get()]]);
return $builder;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/Api/V1/ItemsAggregationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public function test_search_for_authors_with_same_name()
app(ItemRepository::class)->refreshIndex();

$this->getAggregations([
'filter' => ['authors' => ['name' => 'Galanda, Mikuláš']],
'filter' => ['authors.name' => ['Galanda, Mikuláš']],
'terms' => ['authors' => 'authors'],
])->assertSimilarJson([
'authors' => [
Expand Down
38 changes: 27 additions & 11 deletions tests/Feature/Api/V1/ItemsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,23 +98,29 @@ public function test_filtering_by_color()

public function test_distinguishes_authors_with_same_name()
{
$author1 = Authority::factory()->create(['name' => 'Věšín, Jaroslav']);
$author2 = Authority::factory()->create(['name' => 'Věšín, Jaroslav']);

Item::factory()
->create()
->authorities()
->save($author1);
->has(
Authority::factory()->state([
'id' => 'same-name-1',
'name' => 'Věšín, Jaroslav',
])
)
->create(['author' => 'Věšín, Jaroslav; Neznámy autor']);

Item::factory()
->create()
->authorities()
->save($author2);
->has(
Authority::factory()->state([
'id' => 'same-name-2',
'name' => 'Věšín, Jaroslav',
])
)
->create(['author' => 'Věšín, Jaroslav']);

app(ItemRepository::class)->refreshIndex();

$searchByName = route('api.v1.items.index', [
'size' => 10,
'filter[author]' => 'Věšín, Jaroslav',
'filter[authors.name]' => 'Věšín, Jaroslav',
]);

$this->getJson($searchByName)->assertJson([
Expand All @@ -123,12 +129,22 @@ public function test_distinguishes_authors_with_same_name()

$searchById = route('api.v1.items.index', [
'size' => 10,
'filter[authority_id]' => $author1->id,
'filter[authors.authority.id]' => 'same-name-1',
]);

$this->getJson($searchById)->assertJson([
'total' => 1,
]);

$searchByNameOrId = route('api.v1.items.index', [
'size' => 10,
'filter[authors.authority.id]' => 'same-name-2',
'filter[authors.name]' => 'Neznámy autor',
]);

$this->getJson($searchByNameOrId)->assertJson([
'total' => 2,
]);
}

public function test_sorting()
Expand Down
2 changes: 1 addition & 1 deletion tests/Models/ItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public function testAuthorsWithAuthoritiesAttribute()
$this->assertEquals($authority->id, $data[0]->authority->id);

$this->assertEquals('Philips Wouwerman', $data[1]->name);
$this->assertFalse(property_exists($data[1], 'authority'));
$this->assertEquals(null, $data[1]->authority);
}

protected function createFreeItem()
Expand Down

0 comments on commit ba7b998

Please sign in to comment.