You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: core/elasticsearch.md
+24-39Lines changed: 24 additions & 39 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -146,10 +146,20 @@ Here is an example of mappings for 2 resources, `User` and `Tweet`, and their mo
146
146
// api/src/Model/User.php
147
147
namespace App\Model;
148
148
149
+
use ApiPlatform\Elasticsearch\State\CollectionProvider;
150
+
use ApiPlatform\Elasticsearch\State\ItemProvider;
151
+
use ApiPlatform\Elasticsearch\State\Options;
149
152
use ApiPlatform\Metadata\ApiProperty;
150
153
use ApiPlatform\Metadata\ApiResource;
151
-
152
-
#[ApiResource]
154
+
use ApiPlatform\Metadata\Get;
155
+
use ApiPlatform\Metadata\GetCollection;
156
+
157
+
#[ApiResource(
158
+
operations: [
159
+
new GetCollection(provider: CollectionProvider::class, stateOptions: new Options(index: 'user')),
160
+
new Get(provider: ItemProvider::class, stateOptions: new Options(index: 'user')),
161
+
],
162
+
)]
153
163
class User
154
164
{
155
165
#[ApiProperty(identifier: true)]
@@ -175,10 +185,20 @@ class User
175
185
// api/src/Model/Tweet.php
176
186
namespace App\Model;
177
187
188
+
use ApiPlatform\Elasticsearch\State\CollectionProvider;
189
+
use ApiPlatform\Elasticsearch\State\ItemProvider;
190
+
use ApiPlatform\Elasticsearch\State\Options;
178
191
use ApiPlatform\Metadata\ApiProperty;
179
192
use ApiPlatform\Metadata\ApiResource;
180
-
181
-
#[ApiResource]
193
+
use ApiPlatform\Metadata\Get;
194
+
use ApiPlatform\Metadata\GetCollection;
195
+
196
+
#[ApiResource(
197
+
operations: [
198
+
new GetCollection(provider: CollectionProvider::class, stateOptions: new Options(index: 'tweet')),
199
+
new Get(provider: ItemProvider::class, stateOptions: new Options(index: 'tweet')),
200
+
],
201
+
)]
182
202
class Tweet
183
203
{
184
204
#[ApiProperty(identifier: true)]
@@ -199,43 +219,8 @@ Keep in mind that it is your responsibility to populate your Elasticsearch index
199
219
a custom [state processors](state-processors.md#creating-a-custom-state-processor) or any other mechanism that suits your
200
220
project (such as an [ETL](https://en.wikipedia.org/wiki/Extract,_transform,_load)).
201
221
202
-
To disable elasticsearch index discovery for non-elasticsearch entities you can set `elasticsearch: false` in the `#[ApiResource]` attribute. If this property is absent, all entities will perform an index check during cache warmup to determine if they are on elasticsearch or not.
203
-
204
222
You're done! The API is now ready to use.
205
223
206
-
### Creating custom mapping
207
-
208
-
If you don't follow the Elasticsearch recommendations, you may want a custom mapping between API Platform resources and
209
-
Elasticsearch indexes/types.
210
-
211
-
For example, consider an index being similar to a database in an SQL database and a type being equivalent to a table.
212
-
So the `User` and `Tweet` resources of the previous example would become `user` and `tweet` types in an index named `app`:
213
-
214
-
```yaml
215
-
# api/config/packages/api_platform.yaml
216
-
parameters:
217
-
# ...
218
-
env(ELASTICSEARCH_HOST): 'http://localhost:9200'
219
-
220
-
api_platform:
221
-
# ...
222
-
223
-
mapping:
224
-
paths: ['%kernel.project_dir%/src/Model']
225
-
226
-
elasticsearch:
227
-
hosts: ['%env(ELASTICSEARCH_HOST)%']
228
-
mapping:
229
-
App\Model\User:
230
-
index: app
231
-
type: user
232
-
App\Model\Tweet:
233
-
index: app
234
-
type: tweet
235
-
236
-
#...
237
-
```
238
-
239
224
## Filtering
240
225
241
226
See how to use Elasticsearch filters and how to create Elasticsearch custom filters in [the Filters chapter](filters.md).
0 commit comments