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

Add 'uuid' as a default initial field and initial sort field. #1

Open
kevinquillen opened this issue Jan 1, 2024 · 0 comments
Open

Comments

@kevinquillen
Copy link
Owner

kevinquillen commented Jan 1, 2024

I believe this module should behave no differently than how the other backend plugins behave.

The addIndex() method was previously commented out and disabled. When creating a new index, a new collection should be made in Typesense as well.

The previous reason seemed to involve the lack of an initial field with default sort field, which Typesense requires on creating a new collection. We can simply add 'id' or 'uuid' as the first field and the sort field so the process of creating and managing an index in Search API is a bit more streamlined. All entities have a ID/UUID.

Later, we can prevent 'removal' of fields if they are set as the default sort and get it locked and try to keep the fields UI congruent to what Typesense expects at a minimum.

It currently looks something like this:

  public function addIndex(IndexInterface $index) {
    try {
      $index_fields = $index->getFields();
      $typesense_fields = [];

      if (empty($index_fields)) {
        $typesense_fields[] = [
          "name" => "id",
          "type" => "int64",
          "facet" => FALSE,
          "optional" => FALSE,
          "index" => TRUE,
          "sort" => TRUE,
          "infix" => FALSE,
          "locale" => "",
        ];
      }

      $collection_name = $index->id();
      $typesense_fields += $index_fields;

      $schema = [
        'name' => $collection_name,
        'fields' => $typesense_fields,
      ];

      $schema['default_sorting_field'] = $default_sorting_field ?? 'id';

      $this->typesense->createCollection($schema);
    } catch (SearchApiTypesenseException $e) {
      $this->logger->error($e->getMessage());
      $this->messenger()->addError($this->t('Unable to add index @index.', [
        '@index' => $collection_name,
      ]));
    }
  }

If the index has no fields, add a default one. It would be critical to also add one to the index in Drupal so its 'mapped' in the UI. This is not similar to how other backends work, but its what Typesense requires so that should not be a problem necessarily.

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

1 participant