Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Elasticsearch deprecation #5446

Closed
dannyvw opened this issue Mar 9, 2023 · 8 comments
Closed

Elasticsearch deprecation #5446

dannyvw opened this issue Mar 9, 2023 · 8 comments

Comments

@dannyvw
Copy link
Contributor

dannyvw commented Mar 9, 2023

API Platform version(s) affected: 3.1

Description
To avoid index calls to ES for doctrine entities on cache build you can define elasticsearch: false on your ApiResource which is documented in https://api-platform.com/docs/core/elasticsearch/#creating-models and added in #5177 but this triggers this deprecation https://github.com/api-platform/core/blob/main/src/Elasticsearch/Metadata/Resource/Factory/ElasticsearchProviderResourceMetadataCollectionFactory.php#L53

Only on false a no index call is performed https://github.com/api-platform/core/blob/main/src/Elasticsearch/Metadata/Resource/Factory/ElasticsearchProviderResourceMetadataCollectionFactory.php#L101. So not sure how we can fix this deprecation?

Also the index name in hasIndices is now from the shortName, but should it not something like this?

        $options = $operation->getStateOptions() instanceof Options ? $operation->getStateOptions() : new Options(index: $this->getIndex($operation));

        try {
            $this->client->cat()->indices(['index' => $options->getIndex() ?? $this->getIndex($operation)]);

            return true;
        } catch (Missing404Exception|NoNodesAvailableException) {
            return false;
        }

    private function getIndex(Operation $operation): string
    {
        return Inflector::tableize($operation->getShortName());
    }

How to reproduce

#[ApiResource(
    elasticsearch: false
)]
@soyuka
Copy link
Member

soyuka commented Mar 9, 2023

@soyuka
Copy link
Member

soyuka commented Mar 9, 2023

Don't use elasticsearch anymore, use something like this:

use ApiPlatform\Elasticsearch\State\CollectionProvider;
use ApiPlatform\Elasticsearch\State\ItemProvider;

#[ApiResource(operations: [new Get(provider: ItemProvider::class), new GetCollection(provider: CollectionProvider::class)], normalizationContext: ['groups' => ['book:read']])]

You can even create your own ES resources and use them to avoid repetition:

use ApiPlatform\Elasticsearch\State\ItemProvider;

# declare this as attribute
class ElasticGet extends Get {
  __construct(provider: ItemProvider::class);
}

@dannyvw
Copy link
Contributor Author

dannyvw commented Mar 10, 2023

I use that configuration for the ES ApiResource which works fine. I only configured elasticsearch: false for disabling the index check for all other ApiResources.

@soyuka
Copy link
Member

soyuka commented Mar 10, 2023

We should fix this, you shouldn't need to disable elasticsearch on other api resources and this class shouldn't even been hit.

@soyuka
Copy link
Member

soyuka commented Mar 10, 2023

Suggestion:

if (null !== $elasticsearch && $this->hasIndices($operation)) {
    $operation = $operation->withElasticsearch(true);
}

https://github.com/api-platform/core/blob/main/src/Elasticsearch/Metadata/Resource/Factory/ElasticsearchProviderResourceMetadataCollectionFactory.php#LL55-L57C22

@dannyvw
Copy link
Contributor Author

dannyvw commented Mar 10, 2023

@soyuka Your suggested change works for all ApiResources. So non ES and ES resources are not checked with this change.

Own resources:

You can even create your own ES resources and use them to avoid repetition:

# declare this as attribute
class ElasticGet extends Get {
  __construct(provider: ItemProvider::class);
}

Get is final, so cannot be used as extends.

@soyuka
Copy link
Member

soyuka commented Mar 10, 2023

My bad you need to use the HttpOperation :). Would my fix resolve your issue?

@dannyvw
Copy link
Contributor Author

dannyvw commented Mar 10, 2023

@soyuka Yes!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants