Array access method on Synonyms class don't work
Description
The array access methods such as offsetExists() on the Synonyms class don't work as expected.
Steps to reproduce
If I get the Synonyms object for a collection and call retrieve() on it, the result is an array whose 'synonyms' key shows I have a number of synonyms. Each synonym array has the root, ID, and synonym words.
However, if I do isset($my_synonyms[$id])) with one of the IDs, I get FALSE.
This appears to be because $this->synonyms is never populated with data from the API call.
Expected Behavior
Array access method should reflect the actual data.
Actual Behavior
Always returns FALSE.
Metadata
Typsense Version: v4.7.0
OS: MacOS
Hello @joachim-n
Can you provide a snippet from your code please?
It's a little tricky, as I'm using the Drupal https://www.drupal.org/project/search_api_typesense module, which is itself using this package.
Ignoring how I get hold of the collection in the first place, here's some sample code:
/** @var \Typesense\Collection $collection */
$collection = $typesense->retrieveCollection($collection_name);
// This is an array whose 'synonyms' key shows synonym data.
$synonym_data = $collection->synonyms->retrieve();
// In particular, we can get the ID of the first synonym in the list like
// this:
$first_synonym_id = $synonym_data['synonyms'][0]['id'];
/** @var \Typesense\Synonyms $synonyms */
$synonyms = $collection->synonyms;
// Array access on the synonyms object should allow us to get the synonym
// from its ID.
// BUT this shows FALSE:
dump(isset($synonyms[$first_synonym_id]));
// This shows an empty synonym, because \Typesense\Synonyms::offsetGet()
// creates a synonym on the fly:
dump($synonyms[$first_synonym_id]);
@joachim-n can you please explain from where you are getting this method and what it is contain.
$collection = $typesense->retrieveCollection($collection_name);
I can not find it in our package.
Sorry - it's from the integration module in Drupal: https://git.drupalcode.org/project/search_api_typesense/-/blob/1.0.x/src/Api/SearchApiTypesenseService.php#L124
Looks like you would be better off using the method outlined here in the docs
eg.
$synonym_data = $client->collections['products']->synonyms->retrieve();
It might be you are looking at an outdated php client or that it is a custom one. Do this instead of getting the collection first.