I’m trying to follow this page:
https://api-platform.com/docs/v3.0/core/elasticsearch/
I’m storing data in elastic search without also storing it in my database. I understand that the API-Platform has read capability for elastic search, but I had to do a lot of tweaks from what was published in order to get anything to work from elastic search.
Has anyone ran into this?
Here’s my versions from composer.json
"require": {
"php": ">=8.2",
"api-platform/core": "^3.2",
"doctrine/dbal": "^3",
"doctrine/doctrine-bundle": "^2.12",
"doctrine/doctrine-fixtures-bundle": "^3.5",
"doctrine/doctrine-migrations-bundle": "^3.3",
"doctrine/orm": "^2.5",
"elasticsearch/elasticsearch": "^7.11"
},
My elastic search running locally:
"name": "****************",
"cluster_name": "elasticsearch",
"cluster_uuid": "k8_akPPcRmSFZAGmm5IA_g",
"version": {
"number": "8.13.3",
"build_flavor": "default",
"build_type": "deb",
"build_hash": "617f7b76c4ebcb5a7f1e70d409a99c437c896aea",
"build_date": "2024-04-29T22:05:16.051731935Z",
"build_snapshot": false,
"lucene_version": "9.10.0",
"minimum_wire_compatibility_version": "7.17.0",
"minimum_index_compatibility_version": "7.0.0"
},
"tagline": "You Know, for Search"
}
Following the page
https://api-platform.com/docs/v3.0/core/elasticsearch/
I get errors when putting the index definitions in for user
and tweet
.
The _doc
is not accepted in elastic search anymore so I had to remove that key from the example.
When I made the models in the model bundle, they appeared in OpenAPI docs for my API but they were full collection and not tied to elastic search?
I’m not sure from the directions how/why they think the OpenAPI code will know those specific entities are in elastic search?
I had to change the APIResource definitions to for user:
use ApiPlatformElasticsearchStateCollectionProvider;
use ApiPlatformElasticsearchStateItemProvider;
use ApiPlatformElasticsearchStateOptions;
#[ApiResource(
operations: [
new GetCollection(provider: CollectionProvider::class, stateOptions: new Options(index: 'user')),
new Get(provider: ItemProvider::class, stateOptions: new Options(index: 'user')),
],
)]
The page states:
API Platform will automatically disable write operations and snake case document fields will automatically be converted to camel case object properties during serialization.
This did not work for me, I had to change the model definition for user to exactly match the naming convention used when creating my schema in elastic search. This meant renaming the $firstName
to $first_name
and $lastName
to $last_name
in the model for user
.
I guess my questions are:
- has anyone followed the directions on that page and got it to work without all these tweaks?
- am I missing something or just elasticsearch and the elasticsearch library just made changes and the documentation hasn’t kept up?
- Anyone have any direction before I start using this with my own project entities/indexes that the page is omitting?
Jim Glenn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.