Put mapping APIedit

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.

$params = [
    'index' => 'twitter',
    'body' => [
        'properties' => [
            'email' => [
                'type' => 'keyword',
            ],
        ],
    ],
];
$response = $client->indices()->putMapping($params);
var putMappingResponse = client.Map<Tweet>(m => m
        .Index("twitter")
        .Properties(p =>
            p.Keyword(k => k.Name(t => t.Email))
        )
    );
resp = client.indices.put_mapping(
    index="twitter", body={"properties": {"email": {"type": "keyword"}}},
)
print(resp)
response = client.indices.put_mapping(
  index: 'twitter',
  body: {
    properties: {
      email: {
        type: 'keyword'
      }
    }
  }
)
puts response
res, err := es.Indices.PutMapping(
	[]string{"twitter"},
	strings.NewReader(`{
	  "properties": {
	    "email": {
	      "type": "keyword"
	    }
	  }
	}`),
)
fmt.Println(res, err)
const response = await client.indices.putMapping({
  index: 'twitter',
  body: {
    properties: {
      email: {
        type: 'keyword'
      }
    }
  }
})
console.log(response)
PUT /twitter/_mapping
{
  "properties": {
    "email": {
      "type": "keyword"
    }
  }
}

Requestedit

PUT /<target>/_mapping

PUT /_mapping

Path parametersedit

<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 parametersedit

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.

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 bodyedit

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.

Examplesedit

Example with single targetedit

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

$params = [
    'index' => 'publications',
];
$response = $client->indices()->create($params);
var putMappingResponse = client.Indices.Create("publications");
resp = client.indices.create(index="publications")
print(resp)
response = client.indices.create(
  index: 'publications'
)
puts response
res, err := es.Indices.Create("publications")
fmt.Println(res, err)
const response = await client.indices.create({
  index: 'publications'
})
console.log(response)
PUT /publications

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

$params = [
    'index' => 'publications',
    'body' => [
        'properties' => [
            'title' => [
                'type' => 'text',
            ],
        ],
    ],
];
$response = $client->indices()->putMapping($params);
var putMappingResponse = client.Map<object>(m => m
    .Index("publications")
    .Properties(p =>
        p.Text(k => k.Name("title"))
    )
);
resp = client.indices.put_mapping(
    index="publications", body={"properties": {"title": {"type": "text"}}},
)
print(resp)
response = client.indices.put_mapping(
  index: 'publications',
  body: {
    properties: {
      title: {
        type: 'text'
      }
    }
  }
)
puts response
res, err := es.Indices.PutMapping(
	[]string{"publications"},
	strings.NewReader(`{
	  "properties": {
	    "title": {
	      "type": "text"
	    }
	  }
	}`),
)
fmt.Println(res, err)
const response = await client.indices.putMapping({
  index: 'publications',
  body: {
    properties: {
      title: {
        type: 'text'
      }
    }
  }
})
console.log(response)
PUT /publications/_mapping
{
  "properties": {
    "title":  { "type": "text"}
  }
}

Multiple targetsedit

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 twitter-1 and twitter-2 indices at the same time:

$params = [
    'index' => 'twitter-1',
];
$response = $client->indices()->create($params);
$params = [
    'index' => 'twitter-2',
];
$response = $client->indices()->create($params);
$params = [
    'index' => 'twitter-1,twitter-2',
    'body' => [
        'properties' => [
            'user_name' => [
                'type' => 'text',
            ],
        ],
    ],
];
$response = $client->indices()->putMapping($params);
var createIndex1Response = client.Indices.Create("twitter-1");

var createIndex2Response = client.Indices.Create("twitter-2");

var putMappingResponse = client.Map<Tweet>(m => m
    .Index("twitter-1,twitter-2")
    .Properties(p =>
        p.Text(k => k.Name(t => t.UserName))
    )
);
resp = client.indices.create(index="twitter-1")
print(resp)

resp = client.indices.create(index="twitter-2")
print(resp)

resp = client.indices.put_mapping(
    index=["twitter-1", "twitter-2"],
    body={"properties": {"user_name": {"type": "text"}}},
)
print(resp)
response = client.indices.create(
  index: 'twitter-1'
)
puts response

response = client.indices.create(
  index: 'twitter-2'
)
puts response

response = client.indices.create(
  index: 'twitter-1,twitter-2',
  body: {
    properties: {
      user_name: {
        type: 'text'
      }
    }
  }
)
puts response
{
	res, err := es.Indices.Create("twitter-1")
	fmt.Println(res, err)
}

{
	res, err := es.Indices.Create(
		"twitter-2",
	)
	fmt.Println(res, err)
}

{
	res, err := es.Indices.PutMapping(
		[]string{"twitter-1,twitter-2"},
		strings.NewReader(`{
	  "properties": {
	    "user_name": {
	      "type": "text"
	    }
	  }
	}`),
	)
	fmt.Println(res, err)
}
const response0 = await client.indices.create({
  index: 'twitter-1'
})
console.log(response0)

const response1 = await client.indices.create({
  index: 'twitter-2'
})
console.log(response1)

const response2 = await client.indices.putMapping({
  index: 'twitter-1,twitter-2',
  body: {
    properties: {
      user_name: {
        type: 'text'
      }
    }
  }
})
console.log(response2)
# Create the two indices
PUT /twitter-1
PUT /twitter-2

# Update both mappings
PUT /twitter-1,twitter-2/_mapping 
{
  "properties": {
    "user_name": {
      "type": "text"
    }
  }
}

Note that the indices specified (twitter-1,twitter-2) follows multiple index names and wildcard format.

Add new properties to an existing object fieldedit

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.

response = client.indices.create(
  index: 'my-index-000001',
  body: {
    mappings: {
      properties: {
        name: {
          properties: {
            first: {
              type: 'text'
            }
          }
        }
      }
    }
  }
)
puts response
res, err := es.Indices.Create(
	"my-index-000001",
	es.Indices.Create.WithBody(strings.NewReader(`{
	  "mappings": {
	    "properties": {
	      "name": {
	        "properties": {
	          "first": {
	            "type": "text"
	          }
	        }
	      }
	    }
	  }
	}`)),
)
fmt.Println(res, err)
PUT /my-index-000001
{
  "mappings": {
    "properties": {
      "name": {
        "properties": {
          "first": {
            "type": "text"
          }
        }
      }
    }
  }
}

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

response = client.indices.put_mapping(
  index: 'my-index-000001',
  body: {
    properties: {
      name: {
        properties: {
          last: {
            type: 'text'
          }
        }
      }
    }
  }
)
puts response
res, err := es.Indices.PutMapping(
	[]string{"my-index-000001"},
	strings.NewReader(`{
	  "properties": {
	    "name": {
	      "properties": {
	        "last": {
	          "type": "text"
	        }
	      }
	    }
	  }
	}`),
)
fmt.Println(res, err)
PUT /my-index-000001/_mapping
{
  "properties": {
    "name": {
      "properties": {
        "last": {
          "type": "text"
        }
      }
    }
  }
}

Use the get mapping API to verify your changes.

response = client.indices.get_mapping(
  index: 'my-index-000001'
)
puts response
res, err := es.Indices.GetMapping(es.Indices.GetMapping.WithIndex("my-index-000001"))
fmt.Println(res, err)
GET /my-index-000001/_mapping

The API returns the following response:

{
  "my-index-000001" : {
    "mappings" : {
      "properties" : {
        "name" : {
          "properties" : {
            "first" : {
              "type" : "text"
            },
            "last" : {
              "type" : "text"
            }
          }
        }
      }
    }
  }
}

Add multi-fields to an existing fieldedit

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.

response = client.indices.create(
  index: 'my-index-000001',
  body: {
    mappings: {
      properties: {
        city: {
          type: 'text'
        }
      }
    }
  }
)
puts response
res, err := es.Indices.Create(
	"my-index-000001",
	es.Indices.Create.WithBody(strings.NewReader(`{
	  "mappings": {
	    "properties": {
	      "city": {
	        "type": "text"
	      }
	    }
	  }
	}`)),
)
fmt.Println(res, err)
PUT /my-index-000001
{
  "mappings": {
    "properties": {
      "city": {
        "type": "text"
      }
    }
  }
}

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.

response = client.indices.put_mapping(
  index: 'my-index-000001',
  body: {
    properties: {
      city: {
        type: 'text',
        fields: {
          raw: {
            type: 'keyword'
          }
        }
      }
    }
  }
)
puts response
res, err := es.Indices.PutMapping(
	[]string{"my-index-000001"},
	strings.NewReader(`{
	  "properties": {
	    "city": {
	      "type": "text",
	      "fields": {
	        "raw": {
	          "type": "keyword"
	        }
	      }
	    }
	  }
	}`),
)
fmt.Println(res, err)
PUT /my-index-000001/_mapping
{
  "properties": {
    "city": {
      "type": "text",
      "fields": {
        "raw": {
          "type": "keyword"
        }
      }
    }
  }
}

Use the get mapping API to verify your changes.

response = client.indices.get_mapping(
  index: 'my-index-000001'
)
puts response
res, err := es.Indices.GetMapping(es.Indices.GetMapping.WithIndex("my-index-000001"))
fmt.Println(res, err)
GET /my-index-000001/_mapping

The API returns the following response:

{
  "my-index-000001" : {
    "mappings" : {
      "properties" : {
        "city" : {
          "type" : "text",
          "fields" : {
            "raw" : {
              "type" : "keyword"
            }
          }
        }
      }
    }
  }
}

Change supported mapping parameters for an existing fieldedit

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.

response = client.indices.create(
  index: 'my-index-000001',
  body: {
    mappings: {
      properties: {
        user_id: {
          type: 'keyword',
          ignore_above: 20
        }
      }
    }
  }
)
puts response
res, err := es.Indices.Create(
	"my-index-000001",
	es.Indices.Create.WithBody(strings.NewReader(`{
	  "mappings": {
	    "properties": {
	      "user_id": {
	        "type": "keyword",
	        "ignore_above": 20
	      }
	    }
	  }
	}`)),
)
fmt.Println(res, err)
PUT /my-index-000001
{
  "mappings": {
    "properties": {
      "user_id": {
        "type": "keyword",
        "ignore_above": 20
      }
    }
  }
}

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

response = client.indices.put_mapping(
  index: 'my-index-000001',
  body: {
    properties: {
      user_id: {
        type: 'keyword',
        ignore_above: 100
      }
    }
  }
)
puts response
res, err := es.Indices.PutMapping(
	[]string{"my-index-000001"},
	strings.NewReader(`{
	  "properties": {
	    "user_id": {
	      "type": "keyword",
	      "ignore_above": 100
	    }
	  }
	}`),
)
fmt.Println(res, err)
PUT /my-index-000001/_mapping
{
  "properties": {
    "user_id": {
      "type": "keyword",
      "ignore_above": 100
    }
  }
}

Use the get mapping API to verify your changes.

response = client.indices.get_mapping(
  index: 'my-index-000001'
)
puts response
res, err := es.Indices.GetMapping(es.Indices.GetMapping.WithIndex("my-index-000001"))
fmt.Println(res, err)
GET /my-index-000001/_mapping

The API returns the following response:

{
  "my-index-000001" : {
    "mappings" : {
      "properties" : {
        "user_id" : {
          "type" : "keyword",
          "ignore_above" : 100
        }
      }
    }
  }
}

Change the mapping of an existing fieldedit

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 the users index with the user_id field with the long field type.

$params = [
    'index' => 'users',
    'body' => [
        'mappings' => [
            'properties' => [
                'user_id' => [
                    'type' => 'long',
                ],
            ],
        ],
    ],
];
$response = $client->indices()->create($params);
var createIndexResponse = client.Indices.Create("users", m => m
    .Map(m => m
        .Properties(pp => pp
            .Number(t => t
                .Name("user_id")
                .Type(NumberType.Long)
            )
        )
    )
);
resp = client.indices.create(
    index="users",
    body={"mappings": {"properties": {"user_id": {"type": "long"}}}},
)
print(resp)
response = client.indices.create(
  index: 'users',
  body: {
    mappings: {
      properties: {
        user_id: {
          type: 'long'
        }
      }
    }
  }
)
puts response
res, err := es.Indices.Create(
	"users",
	es.Indices.Create.WithBody(strings.NewReader(`{
	  "mappings": {
	    "properties": {
	      "user_id": {
	        "type": "long"
	      }
	    }
	  }
	}`)),
)
fmt.Println(res, err)
const response = await client.indices.create({
  index: 'users',
  body: {
    mappings: {
      properties: {
        user_id: {
          type: 'long'
        }
      }
    }
  }
})
console.log(response)
PUT /users
{
  "mappings" : {
    "properties": {
      "user_id": {
        "type": "long"
      }
    }
  }
}

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

response = client.index(
  index: 'users',
  refresh: 'wait_for',
  body: {
    user_id: 12_345
  }
)
puts response

response = client.index(
  index: 'users',
  refresh: 'wait_for',
  body: {
    user_id: 12_346
  }
)
puts response
{
	res, err := es.Index(
		"users",
		strings.NewReader(`{
	  "user_id": 12345
	}`),
		es.Index.WithRefresh("wait_for"),
		es.Index.WithPretty(),
	)
	fmt.Println(res, err)
}

{
	res, err := es.Index(
		"users",
		strings.NewReader(`{
	  "user_id": 12346
	}`),
		es.Index.WithRefresh("wait_for"),
		es.Index.WithPretty(),
	)
	fmt.Println(res, err)
}
POST /users/_doc?refresh=wait_for
{
  "user_id" : 12345
}

POST /users/_doc?refresh=wait_for
{
  "user_id" : 12346
}

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

$params = [
    'index' => 'new_users',
    'body' => [
        'mappings' => [
            'properties' => [
                'user_id' => [
                    'type' => 'keyword',
                ],
            ],
        ],
    ],
];
$response = $client->indices()->create($params);
var createIndexResponse = client.Indices.Create("new_users", m => m
    .Map(m => m
        .Properties(pp => pp
            .Keyword(t => t
                .Name("user_id")
            )
        )
    )
);
resp = client.indices.create(
    index="new_users",
    body={"mappings": {"properties": {"user_id": {"type": "keyword"}}}},
)
print(resp)
response = client.indices.create(
  index: 'new_users',
  body: {
    mappings: {
      properties: {
        user_id: {
          type: 'keyword'
        }
      }
    }
  }
)
puts response
res, err := es.Indices.Create(
	"new_users",
	es.Indices.Create.WithBody(strings.NewReader(`{
	  "mappings": {
	    "properties": {
	      "user_id": {
	        "type": "keyword"
	      }
	    }
	  }
	}`)),
)
fmt.Println(res, err)
const response = await client.indices.create({
  index: 'new_users',
  body: {
    mappings: {
      properties: {
        user_id: {
          type: 'keyword'
        }
      }
    }
  }
})
console.log(response)
PUT /new_users
{
  "mappings" : {
    "properties": {
      "user_id": {
        "type": "keyword"
      }
    }
  }
}

Use the reindex API to copy documents from the users index to the new_users index.

$params = [
    'body' => [
        'source' => [
            'index' => 'users',
        ],
        'dest' => [
            'index' => 'new_users',
        ],
    ],
];
$response = $client->reindex($params);
var reindexOnServerResponse = client.ReindexOnServer(r => r
    .Source(s => s.Index("users"))
    .Destination(d => d.Index("new_users"))
);
resp = client.reindex(
    body={"source": {"index": "users"}, "dest": {"index": "new_users"}},
)
print(resp)
response = client.reindex(
  body: {
    source: {
      index: 'users'
    },
    dest: {
      index: 'new_users'
    }
  }
)
puts response
res, err := es.Reindex(
	strings.NewReader(`{
	  "source": {
	    "index": "users"
	  },
	  "dest": {
	    "index": "new_users"
	  }
	}`))
fmt.Println(res, err)
const response = await client.reindex({
  body: {
    source: {
      index: 'users'
    },
    dest: {
      index: 'new_users'
    }
  }
})
console.log(response)
POST /_reindex
{
  "source": {
    "index": "users"
  },
  "dest": {
    "index": "new_users"
  }
}

The API returns the following response:

{
  "took": 147,
  "timed_out": false,
  "total": 2,
  "updated": 0,
  "created": 2,
  "deleted": 0,
  "batches": 1,
  "version_conflicts": 0,
  "noops": 0,
  "retries": {
    "bulk": 0,
    "search": 0
  },
  "throttled_millis": 0,
  "requests_per_second": -1.0,
  "throttled_until_millis": 0,
  "failures" : [ ]
}

Rename a fieldedit

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.

response = client.indices.create(
  index: 'my-index-000001',
  body: {
    mappings: {
      properties: {
        user_identifier: {
          type: 'keyword'
        }
      }
    }
  }
)
puts response
res, err := es.Indices.Create(
	"my-index-000001",
	es.Indices.Create.WithBody(strings.NewReader(`{
	  "mappings": {
	    "properties": {
	      "user_identifier": {
	        "type": "keyword"
	      }
	    }
	  }
	}`)),
)
fmt.Println(res, err)
PUT /my-index-000001
{
  "mappings": {
    "properties": {
      "user_identifier": {
        "type": "keyword"
      }
    }
  }
}

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

response = client.indices.put_mapping(
  index: 'my-index-000001',
  body: {
    properties: {
      user_id: {
        type: 'alias',
        path: 'user_identifier'
      }
    }
  }
)
puts response
res, err := es.Indices.PutMapping(
	[]string{"my-index-000001"},
	strings.NewReader(`{
	  "properties": {
	    "user_id": {
	      "type": "alias",
	      "path": "user_identifier"
	    }
	  }
	}`),
)
fmt.Println(res, err)
PUT /my-index-000001/_mapping
{
  "properties": {
    "user_id": {
      "type": "alias",
      "path": "user_identifier"
    }
  }
}

Use the get mapping API to verify your changes.

response = client.indices.get_mapping(
  index: 'my-index-000001'
)
puts response
res, err := es.Indices.GetMapping(es.Indices.GetMapping.WithIndex("my-index-000001"))
fmt.Println(res, err)
GET /my-index-000001/_mapping

The API returns the following response:

{
  "my-index-000001" : {
    "mappings" : {
      "properties" : {
        "user_id" : {
          "type" : "alias",
          "path" : "user_identifier"
        },
        "user_identifier" : {
          "type" : "keyword"
        }
      }
    }
  }
}