I use FOS Elastica bundle in my Symfony application but I am not using Doctrine. I try to populate the Elasticsearch with a custom provider. Here is my code.
# 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])));
}
}
When I use the command bin/console fos:elastica:populate
I get the following error
FOS\ElasticaBundle\Persister\ObjectPersister::__construct(): Argument #3 ($objectClass) must be of type string, null given
Also I am wondering how I can index a great number of documents. I think they would be too much to store in an array. Is there a way to use a callback instead of an array in new Pagerfanta
or something like that?
I think I need a DTO to use as a model. The following code works:
# 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])));
}
}
But I still have a problem with indexing a large set of data. I think I can not create an array with millions of elements.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.