Skip to content

Commit 588a90a

Browse files
authored
docs(elasticsearch): document field name mapping (#2349)
1 parent 545a21b commit 588a90a

1 file changed

Lines changed: 35 additions & 2 deletions

File tree

‎core/elasticsearch.md‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,7 @@ class Tweet
327327
}
328328
```
329329

330-
API Platform will automatically disable write operations and snake_case document fields will
331-
automatically be converted to camelCase object properties during serialization.
330+
API Platform will automatically disable write operations.
332331

333332
Keep in mind that it is your responsibility to populate your Elasticsearch index. To do so, you can
334333
use [Logstash](https://www.elastic.co/products/logstash), a custom
@@ -337,6 +336,40 @@ that suits your project (such as an [ETL](https://en.wikipedia.org/wiki/Extract,
337336

338337
You're done! The API is now ready to use.
339338

339+
### Mapping Field Names
340+
341+
By default, API Platform does not convert Elasticsearch document field names: a `_source` field is
342+
mapped to the object property of the same name (unless [`#[SerializedName]`](serialization.md) is
343+
used). Use camelCase field names in your mappings, as described above.
344+
345+
If your documents use snake_case field names, configure a name converter so that they are mapped to
346+
camelCase properties. There are two options.
347+
348+
To convert names for every format, set the global `name_converter` in the API Platform
349+
configuration. This also changes the serialization of all other formats:
350+
351+
```yaml
352+
# api/config/packages/api_platform.yaml
353+
api_platform:
354+
name_converter: "serializer.name_converter.camel_case_to_snake_case"
355+
```
356+
357+
To convert names for Elasticsearch only, override the
358+
`api_platform.elasticsearch.name_converter.inner_fields` service and pass it the converter:
359+
360+
```yaml
361+
# api/config/services.yaml
362+
services:
363+
api_platform.elasticsearch.name_converter.inner_fields:
364+
class: ApiPlatform\Elasticsearch\Serializer\NameConverter\InnerFieldsNameConverter
365+
arguments:
366+
- "@serializer.name_converter.camel_case_to_snake_case"
367+
```
368+
369+
> [!WARNING] The same name converter builds the field names used by the Elasticsearch filters and by
370+
> sorting. Once a converter is set, a filter or sort on the `firstName` property targets the
371+
> `first_name` field of the index.
372+
340373
## Filtering
341374

342375
See how to use Elasticsearch filters and how to create Elasticsearch custom filters in the

0 commit comments

Comments
 (0)