Put mapping API

Adds new fields to an existing data stream or index. You can also use the put mapping API to change the search settings of existing fields.

For data streams, these changes are applied to all backing indices by default.

  1. PUT /my-index-000001/_mapping
  2. {
  3. "properties": {
  4. "email": {
  5. "type": "keyword"
  6. }
  7. }
  8. }

Before 7.0.0, the mappings definition used to include a type name. Although specifying types in requests is now deprecated, a type can still be provided if the request parameter include_type_name is set. For more details, please see Removal of mapping types.

Request

PUT /<target>/_mapping

PUT /_mapping

Path parameters

<target>

(Optional, string) Comma-separated list of data streams, indices, and index aliases used to limit the request. Wildcard expressions (*) are supported.

To target all data streams and indices in a cluster, omit this parameter or use _all or *.

Query parameters

allow_no_indices

(Optional, boolean) If true, the request does not return an error if a wildcard expression or _all value retrieves only missing or closed indices.

This parameter also applies to index aliases that point to a missing or closed index.

Defaults to false.

expand_wildcards

(Optional, string) Controls what kind of indices that wildcard expressions can expand to. Multiple values are accepted when separated by a comma, as in open,hidden. Valid values are:

  • all

    Expand to open and closed indices, including hidden indices.

    open

    Expand only to open indices.

    closed

    Expand only to closed indices.

    hidden

    Expansion of wildcards will include hidden indices. Must be combined with open, closed, or both.

    none

    Wildcard expressions are not accepted.

Defaults to open.

include_type_name

[7.0.0] Deprecated in 7.0.0. Mapping types have been deprecated. See Removal of mapping types. (Optional, boolean) If true, a mapping type is expected in the body of mappings. Defaults to false.

ignore_unavailable

(Optional, boolean) If true, missing or closed indices are not included in the response. Defaults to false.

master_timeout

(Optional, time units) Specifies the period of time to wait for a connection to the master node. If no response is received before the timeout expires, the request fails and returns an error. Defaults to 30s.

timeout

(Optional, time units) Specifies the period of time to wait for a response. If no response is received before the timeout expires, the request fails and returns an error. Defaults to 30s.

write_index_only

(Optional, boolean) If true, the mappings are applied only to the current write index for the target. Defaults to false.

Request body

properties

(Required, mapping object) Mapping for a field. For new fields, this mapping can include:

For existing fields, see Change the mapping of an existing field.

Examples

Example with single target

The put mapping API requires an existing data stream or index. The following create index API request creates the publications index with no mapping.

  1. PUT /publications

The following put mapping API request adds title, a new text field, to the publications index.

  1. PUT /publications/_mapping
  2. {
  3. "properties": {
  4. "title": { "type": "text"}
  5. }
  6. }

Multiple targets

The PUT mapping API can be applied to multiple data streams or indices with a single request. For example, you can update mappings for the my-index-000001 and my-index-000002 indices at the same time:

  1. # Create the two indices
  2. PUT /my-index-000001
  3. PUT /my-index-000002
  4. # Update both mappings
  5. PUT /my-index-000001,my-index-000002/_mapping
  6. {
  7. "properties": {
  8. "user": {
  9. "properties": {
  10. "name": {
  11. "type": "keyword"
  12. }
  13. }
  14. }
  15. }
  16. }

Add new properties to an existing object field

You can use the put mapping API to add new properties to an existing object field. To see how this works, try the following example.

Use the create index API to create an index with the name object field and an inner first text field.

  1. PUT /my-index-000001
  2. {
  3. "mappings": {
  4. "properties": {
  5. "name": {
  6. "properties": {
  7. "first": {
  8. "type": "text"
  9. }
  10. }
  11. }
  12. }
  13. }
  14. }

Use the put mapping API to add a new inner last text field to the name field.

  1. PUT /my-index-000001/_mapping
  2. {
  3. "properties": {
  4. "name": {
  5. "properties": {
  6. "last": {
  7. "type": "text"
  8. }
  9. }
  10. }
  11. }
  12. }

Add multi-fields to an existing field

Multi-fields let you index the same field in different ways. You can use the put mapping API to update the fields mapping parameter and enable multi-fields for an existing field.

To see how this works, try the following example.

Use the create index API to create an index with the city text field.

  1. PUT /my-index-000001
  2. {
  3. "mappings": {
  4. "properties": {
  5. "city": {
  6. "type": "text"
  7. }
  8. }
  9. }
  10. }

While text fields work well for full-text search, keyword fields are not analyzed and may work better for sorting or aggregations.

Use the put mapping API to enable a multi-field for the city field. This request adds the city.raw keyword multi-field, which can be used for sorting.

  1. PUT /my-index-000001/_mapping
  2. {
  3. "properties": {
  4. "city": {
  5. "type": "text",
  6. "fields": {
  7. "raw": {
  8. "type": "keyword"
  9. }
  10. }
  11. }
  12. }
  13. }

Change supported mapping parameters for an existing field

The documentation for each mapping parameter indicates whether you can update it for an existing field using the put mapping API. For example, you can use the put mapping API to update the ignore_above parameter.

To see how this works, try the following example.

Use the create index API to create an index containing a user_id keyword field. The user_id field has an ignore_above parameter value of 20.

  1. PUT /my-index-000001
  2. {
  3. "mappings": {
  4. "properties": {
  5. "user_id": {
  6. "type": "keyword",
  7. "ignore_above": 20
  8. }
  9. }
  10. }
  11. }

Use the put mapping API to change the ignore_above parameter value to 100.

  1. PUT /my-index-000001/_mapping
  2. {
  3. "properties": {
  4. "user_id": {
  5. "type": "keyword",
  6. "ignore_above": 100
  7. }
  8. }
  9. }

Change the mapping of an existing field

Except for supported mapping parameters, you can’t change the mapping or field type of an existing field. Changing an existing field could invalidate data that’s already indexed.

If you need to change the mapping of a field in a data stream’s backing indices, see Change mappings and settings for a data stream.

If you need to change the mapping of a field in other indices, create a new index with the correct mapping and reindex your data into that index.

To see how you can change the mapping of an existing field in an index, try the following example.

Use the create index API to create an index with the user_id field with the long field type.

  1. PUT /my-index-000001
  2. {
  3. "mappings" : {
  4. "properties": {
  5. "user_id": {
  6. "type": "long"
  7. }
  8. }
  9. }
  10. }

Use the index API to index several documents with user_id field values.

  1. POST /my-index-000001/_doc?refresh=wait_for
  2. {
  3. "user_id" : 12345
  4. }
  5. POST /my-index-000001/_doc?refresh=wait_for
  6. {
  7. "user_id" : 12346
  8. }

To change the user_id field to the keyword field type, use the create index API to create a new index with the correct mapping.

  1. PUT /my-new-index-000001
  2. {
  3. "mappings" : {
  4. "properties": {
  5. "user_id": {
  6. "type": "keyword"
  7. }
  8. }
  9. }
  10. }

Use the reindex API to copy documents from the old index to the new one.

  1. POST /_reindex
  2. {
  3. "source": {
  4. "index": "my-index-000001"
  5. },
  6. "dest": {
  7. "index": "my-new-index-000001"
  8. }
  9. }

Rename a field

Renaming a field would invalidate data already indexed under the old field name. Instead, add an alias field to create an alternate field name.

For example, use the create index API to create an index with the user_identifier field.

  1. PUT /my-index-000001
  2. {
  3. "mappings": {
  4. "properties": {
  5. "user_identifier": {
  6. "type": "keyword"
  7. }
  8. }
  9. }
  10. }

Use the put mapping API to add the user_id field alias for the existing user_identifier field.

  1. PUT /my-index-000001/_mapping
  2. {
  3. "properties": {
  4. "user_id": {
  5. "type": "alias",
  6. "path": "user_identifier"
  7. }
  8. }
  9. }