php - FOS Elastica:如何在没有 Doctrine 的情况下填充索引
问题描述
我在 Symfony 应用程序中使用 FOS Elastica 包,但我没有使用 Doctrine。我尝试使用自定义提供程序填充 Elasticsearch。这是我的代码。
# config/packages/fos_elastica.yaml
fos_elastica:
clients:
default: { url: '%env(ELASTICSEARCH_URL)%' }
indexes:
my_index:
use_alias: true
properties:
my_property:
type: keyword
persistence:
provider:
service: app.search_provider.my_provider
listener: false
# config/services.yaml
services:
# ...
app.search_provider.my_provider:
class: App\SearchProvider\MyProvider
tags:
- { name: fos_elastica.pager_provider, index: my_index, type: my_type }
<?php
// src/SearchProvider/MyProvider.php
namespace App\SearchProvider;
use Elastica\Document;
use FOS\ElasticaBundle\Provider\PagerfantaPager;
use FOS\ElasticaBundle\Provider\PagerInterface;
use FOS\ElasticaBundle\Provider\PagerProviderInterface;
use Pagerfanta\Adapter\ArrayAdapter;
use Pagerfanta\Pagerfanta;
final class MyProvider implements PagerProviderInterface
{
public function provide(array $options = []): PagerInterface
{
$document = new Document();
$document->setData([
'my_property' => 'test',
]);
return new PagerfantaPager(new Pagerfanta(new ArrayAdapter([$document])));
}
}
当我使用该命令时bin/console fos:elastica:populate
,出现以下错误
FOS\ElasticaBundle\Persister\ObjectPersister::__construct(): Argument #3 ($objectClass) must be of type string, null given
另外我想知道如何索引大量文档。我认为它们太多了,无法存储在数组中。有没有办法使用回调而不是数组new Pagerfanta
或类似的东西?
解决方案
我想我需要一个 DTO 来用作模型。以下代码有效:
# config/packages/fos_elastica.yaml
fos_elastica:
clients:
default: { url: '%env(ELASTICSEARCH_URL)%' }
indexes:
my_index:
use_alias: true
properties:
myProperty:
type: keyword
persistence:
model: App\Entity\MyEntity
provider:
service: app.search_provider.my_provider
listener: false
// src/Entity
namespace App\Entity;
final class MyEntity
{
public int $id;
public string $myProperty;
}
// src/SearchProvider/MyProvider.php
namespace App\SearchProvider;
use App\Entity\MyEntity;
use FOS\ElasticaBundle\Provider\PagerfantaPager;
use FOS\ElasticaBundle\Provider\PagerInterface;
use FOS\ElasticaBundle\Provider\PagerProviderInterface;
use Pagerfanta\Adapter\ArrayAdapter;
use Pagerfanta\Pagerfanta;
final class MyProvider implements PagerProviderInterface
{
public function provide(array $options = []): PagerInterface
{
$myEntity = new MyEntity();
$myEntity->id = 1;
$myEntity->myProperty = 'test';
return new PagerfantaPager(new Pagerfanta(new ArrayAdapter([$myEntity])));
}
}
但是我仍然有索引大量数据的问题。我想我不能创建一个包含数百万个元素的数组。
推荐阅读
- python - TensorFlow 模型损失的近似周期性跳跃
- javascript - Babel 输出中这些注释的目的是什么?
- bash - 如何通过 BASH SCRIPT 中的用户定义变量更改 IP 地址?
- elasticsearch - 基于由另一个参数链接的一系列日志中的一个参数在 Kibana 中进行过滤
- tensorflow - 在 Tensorflow v2 中替换 tf.contrib.predictor.from_saved_model
- python - connect() 到 unix:/var/uwsgi/salesproject.sock 失败(111:连接被拒绝)
- android - 当布局通常为空且仅以编程方式填充时,我可以显示示例视图吗?
- php - 在 WooCommerce 中获取产品类别和标签术语作为元关键字
- html - `display: block;` 在 `pre` 标签内的 `code` 标签内会创建不需要的新行,并且滚动时样式不会应用到全长
- mysql - MySQL 从存储过程中检索 last_insert_id