Index Operations#

In the Getting Started documentation we already saw how we can add documents to our created Index.

The following shows the basic usage as already shown in the “Getting Started” documentation. Under the $this->engine variable we assume that you have already injected your created Engine instance.

Save document#

To add a document to an index we can use the saveDocument method. The only required field for the document is the defined IdentifierField all others are optional and don’t need to be provided or can be null.

<?php

$this->engine->saveDocument('blog', [
    'id' => '1',
    'title' => 'My first blog post',
    'description' => 'This is the description of my first blog post',
    'tags' => ['UI', 'UX'],
]);

To update a document the same method saveDocument need to be used with the same identifier value.

Note

Currently, you can use some kind of normalizer like symfony/serializer to convert an object to an array and back to an object at current state a Document Mapper or ODM package does not yet exist. If provided in future it will be part of an own package which make usage of SEAL. Example like doctrine/orm using doctrine/dbal. See ODM issue.

Delete document#

To remove a document from an index we can use the deleteDocument method. It only requires the name of the index and the identifier of the document which should be removed.

<?php

$this->engine->deleteDocument('blog', '1');

Reindex operations#

To reindex documents it is required to create atleast one ReindexProvider for the index you want to reindex. The ReindexProvider is a class which implements the ReindexProviderInterface and provides the documents for your index.

<?php

class BlogReindexProvider implements ReindexProviderInterface
{
    public function total(): ?int
    {
        return 3;
    }

    public function provide(): \Generator
    {
        yield [
            'id' => 1,
            'title' => 'Title 1',
            'description' => 'Description 1',
        ];

        yield [
            'id' => 2,
            'title' => 'Title 2',
            'description' => 'Description 2',
        ];

        yield [
            'id' => 3,
            'title' => 'Title 3',
            'description' => 'Description 3',
        ];
    }

    public static function getIndex(): string
    {
        return 'blog';
    }
}

After that you can use the reindex to index all documents:

When using the Standalone version you need to reindex the documents in your search engines via the Engine instance which was created before:

<?php

$reindexProviders = [
    new BlogReindexProvider(),
];

// reindex all indexes
$engine->reindex($reindexProviders);

// reindex specific index and drop data before
$engine->reindex($reindexProviders, 'blog', dropIndex: true);

Bulk operations#

Currently no bulk operations are implemented. Add your opinion to the Bulk issue on Github.

Next Steps#

After this short introduction about indexing we are able to add and remove our documents from the defined indexes.

In the next chapter, we examine the different conditions of Search & Filter Conditions the abstraction provides.