This library provides the integration of Elasticsearch with the main database (for example, PostgreSQL or MySQL) used in the API Platform and Doctrine. It allows you to perform the necessary queries with a simple setup through the Elasticsearch database, providing all the advantages of Elasticsearch in conjunction with the API Platform and Doctrine.
The diagram below shows how the data query process works using the Symfony API Platform and Elasticsearch.
+--------+ +----------------------------+ +----------------+ | Client | ----> | Symfony API | <------- | Main DB | | | <---- | Platform (GetCollection) | | | +--------+ +----------------------------+ +----------------+ | | v | +----------------------------+ | | Request to Elasticsearch | | +----------------------------+ | | | +---------- ID's------------------------>|
Installation from composer
composer require ggbb/api-platform-elasticsearch-integration
doctrine.yaml
doctrine:
orm:
dql:
string_functions:
array_position: Ggbb\ApiPlatformElasticsearchIntegrationBundle\DQL\ArrayPositionFunction
use Ggbb\ApiPlatformElasticsearchIntegrationBundle\Provider\ElasticsearchProvider;
#[ApiResource(
operations: [
new GetCollection(
provider: ElasticsearchProvider::class,
),
],
paginationEnabled: false,
)]
#[ORM\Entity(repositoryClass: PostRepository::class)]
#[ElasticsearchEntity(
index: 'post',
settings: [
"analysis" => [
...
]
],
mappings: [
"properties" => [
...
]
]
)]
class Post
{
... // Implementation of the entity
}
#[ApiFilter(ElasticsearchFilter::class, properties: [
RangeElasticsearchFilter::class => [
'price',
'area',
'floor',
],
SortElasticsearchFilter::class => [
'created_at' => 'desc',
'id' => 'desc',
],
PaginationElasticsearchFilter::class => [
'_page'
],
])]
#[ElasticsearchEntity()
class Post
{
... // Implementation of the entity
}
#[ElasticsearchEntity()
class Post
{
... // Implementation of the entity
#[ORM\Column(nullable: true)]
#[ElasticsearchField]
private ?int $regionCodeAlt = null;
#[ElasticsearchField(name: 'user')]
public function getUserId(): ?int
{
return $this->getUser()?->getId();
}
}
<?php
namespace App\Filter;
use App\Service\User\UserService;
use Ggbb\ApiPlatformElasticsearchIntegrationBundle\Filter\AbstractElasticsearchFilter;
use Ggbb\ApiPlatformElasticsearchIntegrationBundle\Filter\Interface\IgnoreFieldNameInterface;
use Ggbb\ApiPlatformElasticsearchIntegrationBundle\Filter\Query\QueryBuilder;
final class OrganizationElasticsearchFilter extends AbstractElasticsearchFilter implements IgnoreFieldNameInterface
{
public function __construct(private UserService $userService)
{
}
public function filterProperty(?string $property, mixed $value, QueryBuilder &$queryBuilder): void
{
$organization = $this->userService->getOrganization();
$queryBuilder->addFilterTerm('organization', (int) $organization->getId());
}
}
#[ApiFilter(ElasticsearchFilter::class, properties: [
OrganizationElasticsearchFilter::class,
])]
class Post
{
... // Implementation of the entity
}
This command extracts data from the database, generates an index in Elasticsearch based on the specified fields, first clearing, then creating indexes and filling them with data from the specified entity.
php bin/console elasticsearch:import 'App\Entity\Post' --env=prod --batch-size=150000
Mapping output of the entire indexed structure in Elasticsearch.
php bin/console elasticsearch:show-entities