Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array access method on Synonyms class don't work #22

Open
joachim-n opened this issue Jan 4, 2022 · 6 comments
Open

Array access method on Synonyms class don't work #22

joachim-n opened this issue Jan 4, 2022 · 6 comments
Assignees

Comments

@joachim-n
Copy link

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

@AbdullahFaqeir
Copy link
Collaborator

Hello @joachim-n

Can you provide a snippet from your code please?

@AbdullahFaqeir AbdullahFaqeir self-assigned this Jan 4, 2022
@joachim-n
Copy link
Author

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.

@joachim-n
Copy link
Author

joachim-n commented Jan 4, 2022

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

@arayiksmbatyan
Copy link
Collaborator

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

@joachim-n
Copy link
Author

@HoppGwyn
Copy link

HoppGwyn commented May 30, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants