Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ public function search(
'page' => $firstPage,
'hitsPerPage' => $perPage,
'filters' => $hasAnyFilter ? implode(' AND ', $filters) : null,
/**
* Only the id is ever read from the response (see below):
* the full record is re-fetched from the DB via findByIdsPreserveOrder.
* Restrict the payload accordingly.
*/
'attributesToRetrieve' => ['objectID', 'id'],
],
static fn ($v) => $v !== null,
),
Expand Down Expand Up @@ -156,12 +162,12 @@ public function search(

/**
* Build an OR-filter that can match different country input shapes:
* - English: country / state_party (exact match)
* - English: country (exact match)
* - Japanese: country_name_jp (exact match)
* - ISO3-like: state_party_codes:<ISO3>
*
* Example output:
* (country:"Japan" OR state_party:"Japan" OR country_name_jp:"日本" OR state_party_codes:JPN)
* (country:"Japan" OR country_name_jp:"日本" OR state_party_codes:JPN)
*/
private function buildCountryOrFilter(string $raw): string
{
Expand All @@ -170,7 +176,6 @@ private function buildCountryOrFilter(string $raw): string

$orParts = [
'country:"' . $quoted . '"',
'state_party:"' . $quoted . '"',
'country_name_jp:"' . $quoted . '"',
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function test_search_builds_algolia_params_with_filters_and_paging(): voi
'filters' =>
'state_party_codes:ECU AND study_region:"South America" AND category:"Natural" ' .
'AND year_inscribed >= 1978 AND year_inscribed <= 1980',
'attributesToRetrieve' => ['objectID', 'id'],
];

$this->client
Expand All @@ -76,4 +77,48 @@ public function test_search_builds_algolia_params_with_filters_and_paging(): voi
$this->assertSame(2, $result->currentPage);
$this->assertSame(1, $result->lastPage);
}

public function test_search_by_country_name_without_iso3_does_not_filter_on_state_party(): void
{
$indexName = 'world_heritage';
$adapter = new AlgoliaWorldHeritageSearchAdapter($this->client, $indexName);

$q = new AlgoliaSearchListQuery(
keyword: null,
countryName: 'Ecuador',
countryIso3: null,
region: null,
category: null,
yearFrom: null,
yearTo: null,
criteria: [],
isEndangered: null,
currentPage: 1,
perPage: 30,
);

$expectedParams = [
'query' => '',
'page' => 0,
'hitsPerPage' => 30,
'filters' => '(country:"Ecuador" OR country_name_jp:"Ecuador")',
'attributesToRetrieve' => ['objectID', 'id'],
];

$this->client
->shouldReceive('searchSingleIndex')
->once()
->with($indexName, $expectedParams)
->andReturn([
'hits' => [],
'nbHits' => 0,
'nbPages' => 0,
'page' => 0,
'hitsPerPage' => 30,
]);

$result = $adapter->search($q, currentPage: 1, perPage: 30);

$this->assertSame([], $result->ids);
}
}
Loading