FriendsOfSymfony / FOSElasticaBundle

Elasticsearch PHP integration for your Symfony project using Elastica.
http://friendsofsymfony.github.io
MIT License
1.25k stars 792 forks source link

Can not use the bundle without Doctrine #1780

Open ghost opened 3 years ago

ghost commented 3 years ago

I do not have Doctrine installed in my Symfony application. I use FOSElasticaBundle 6.0.x-dev with the following configuration:

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:
                    insert: false
                    update: false
                    delete: false
                    flush: false

When I clear the cache or try to populate the index I get the following error:

PHP Fatal error: Class "Doctrine\ORM\Events" not found in /var/www/html/vendor/friendsofsymfony/elastica-bundle/src/DependencyInjection/FOSElasticaExtension.php on line 607

When I install Doctrine I do not get this error. Can't I use the bundle without Doctrine?

deguif commented 3 years ago

@joergmoldenhauer could you try with:

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

That should avoid passing in the listener configuration method.

ghost commented 3 years ago

That worked. Thank you.

I am also having some problems with my custom provider like described here: https://stackoverflow.com/questions/66693334/fos-elastica-how-can-i-populate-an-index-without-doctrine

Do you have an idea?

deguif commented 3 years ago

Do you have an idea?

@joergmoldenhauer not for the moment, I would have to investigate to understand what breaks ;)

ghost commented 3 years ago

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.