ggbb/api-platform-elasticsearch-integration 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ggbb/api-platform-elasticsearch-integration

最新稳定版本:v0.0.1

Composer 安装命令:

composer require ggbb/api-platform-elasticsearch-integration

包简介

The permissions system for users in symfony

README 文档

README

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

Installation from composer

composer require ggbb/api-platform-elasticsearch-integration

doctrine.yaml

doctrine:
  orm:
    dql:
      string_functions:
        array_position: Ggbb\ApiPlatformElasticsearchIntegrationBundle\DQL\ArrayPositionFunction

Usage examples

Connecting to an entity in Symfony.

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
}

Adding filters

#[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
}

Adding the field to the elasticsearch indexing

#[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();
    }
}

Creating a custom filter

<?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
}

Management commands

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

统计信息

  • 总下载量: 2
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-11-14