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);
In Laravel the new created ReindexProvider
need to be tagged correctly:
<?php // app/Providers/AppServiceProvider.php
namespace App\Providers;
class AppServiceProvider extends \Illuminate\Support\ServiceProvider
{
// ...
public function boot(): void
{
$this->app->singleton(\App\Search\BlogReindexProvider::class, fn () => new \App\Search\BlogReindexProvider());
$this->app->tag(\App\Search\BlogReindexProvider::class, 'cmsig_seal.reindex_provider');
}
}
After correctly tagging the ReindexProvider
with seal.reindex_provider
the
cmsig:seal:reindex
command can be used to index all documents:
# reindex all indexes
php artisan cmsig:seal:reindex
# reindex specific index and drop data before
php artisan cmsig:seal:reindex --index=blog --drop
In Symfony autoconfigure
feature should already tag the new ReindexProvider
correctly
with the seal.reindex_provider
tag. If not you can tag it manually:
# config/services.yaml
services:
App\Search\BlogReindexProvider:
tags:
- { name: cmsig_seal.reindex_provider }
After correctly tagging the ReindexProvider
use the following command to index all documents:
# reindex all indexes
bin/console cmsig:seal:reindex
bin/console cmsig:seal:reindex --index=blog --drop
In Spiral the new created ReindexProvider
need to be registered correctly as reindex provider:
<?php // app/config/cmsig_seal.php
return [
// ...
'reindex_providers' => [
\App\Search\BlogReindexProvider::class,
],
];
After correctly registering the ReindexProvider
use the following command to index all documents:
# reindex all indexes
php app.php cmsig:seal:reindex
php app.php cmsig:seal:reindex --index=blog --drop
In Mezzio the new created ReindexProvider
need to be registered correctly as reindex provider:
<?php // src/App/src/ConfigProvider.php
class ConfigProvider
{
public function __invoke(): array
{
return [
// ...
'cmsig_seal' => [
// ...
'reindex_providers' => [
\App\Search\BlogReindexProvider::class,
],
],
];
}
public function getDependencies(): array
{
return [
// ...
'invokables' => [
\App\Search\BlogReindexProvider::class => \App\Search\BlogReindexProvider::class,
],
// ...
];
}
}
After correctly registering the ReindexProvider
use the following command to index all documents:
# reindex all indexes
vendor/bin/laminas cmsig:seal:reindex
vendor/bin/laminas cmsig:seal:reindex --index=blog --drop
In Yii the new created ReindexProvider
need to be registered correctly as reindex provider:
<?php // config/common/params.php
return [
// ...
'cmsig/seal-yii-module' => [
// ...
'reindex_providers' => [
\App\Search\BlogReindexProvider::class,
],
],
];
After correctly registering the ReindexProvider
use the following command to index all documents:
# reindex all indexes
./yii cmsig:seal:reindex
./yii cmsig:seal:reindex --index=blog --drop
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.