![](/img/trans.png)
[英]Elasticsearch: no such index - Symfony FOS/elastica-bundle
[英]FOS Elastica: How can I populate an index without 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 来用作 model。 以下代码有效:
# 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])));
}
}
但是我仍然有索引大量数据的问题。 我想我不能创建一个包含数百万个元素的数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.