Skip to content

Commit f7e7cd4

Browse files
authored
Update elasticsearch documentation (#1733)
1 parent 8174eb9 commit f7e7cd4

File tree

1 file changed

+24
-39
lines changed

1 file changed

+24
-39
lines changed

core/elasticsearch.md

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,20 @@ Here is an example of mappings for 2 resources, `User` and `Tweet`, and their mo
146146
// api/src/Model/User.php
147147
namespace App\Model;
148148

149+
use ApiPlatform\Elasticsearch\State\CollectionProvider;
150+
use ApiPlatform\Elasticsearch\State\ItemProvider;
151+
use ApiPlatform\Elasticsearch\State\Options;
149152
use ApiPlatform\Metadata\ApiProperty;
150153
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+
)]
153163
class User
154164
{
155165
#[ApiProperty(identifier: true)]
@@ -175,10 +185,20 @@ class User
175185
// api/src/Model/Tweet.php
176186
namespace App\Model;
177187

188+
use ApiPlatform\Elasticsearch\State\CollectionProvider;
189+
use ApiPlatform\Elasticsearch\State\ItemProvider;
190+
use ApiPlatform\Elasticsearch\State\Options;
178191
use ApiPlatform\Metadata\ApiProperty;
179192
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+
)]
182202
class Tweet
183203
{
184204
#[ApiProperty(identifier: true)]
@@ -199,43 +219,8 @@ Keep in mind that it is your responsibility to populate your Elasticsearch index
199219
a custom [state processors](state-processors.md#creating-a-custom-state-processor) or any other mechanism that suits your
200220
project (such as an [ETL](https://en.wikipedia.org/wiki/Extract,_transform,_load)).
201221

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-
204222
You're done! The API is now ready to use.
205223

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-
239224
## Filtering
240225

241226
See how to use Elasticsearch filters and how to create Elasticsearch custom filters in [the Filters chapter](filters.md).

0 commit comments

Comments
 (0)