Skip to content

Commit 052306a

Browse files
committed
[DOCS] Add examples to the mapping docs (#45520)
1 parent c1178ee commit 052306a

File tree

2 files changed

+147
-34
lines changed

2 files changed

+147
-34
lines changed

docs/reference/indices/put-mapping.asciidoc

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,23 @@ PUT /twitter-1,twitter-2/_mapping <1>
5757
[float]
5858
==== Updating field mappings
5959

60-
In general, the mapping for existing fields cannot be updated. There are some
61-
exceptions to this rule. For instance:
60+
// tag::put-field-mapping-exceptions[]
6261

63-
* new <<properties>> can be added to <<object>> fields.
64-
* new <<multi-fields,multi-fields>> can be added to existing fields.
65-
* the <<ignore-above>> parameter can be updated.
62+
You can't change the mapping of an existing field, with the following
63+
exceptions:
64+
65+
* You can add new <<properties,properties>> to an <<object,`object`>> field.
66+
* You can use the <<multi-fields,`field`>> mapping parameter to enable
67+
multi-fields.
68+
* You can change the value of the <<ignore-above,`ignore_above`>> mapping
69+
parameter.
70+
71+
Changing the mapping of an existing field could invalidate data that's already
72+
indexed. If you need to change the mapping of a field, create a new index with
73+
the correct mappings and <<docs-reindex,reindex>> your data into that index. If
74+
you only want to rename a field, consider adding an <<alias, `alias`>> field.
75+
76+
// end::put-field-mapping-exceptions[]
6677

6778
For example:
6879

docs/reference/mapping.asciidoc

Lines changed: 131 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -118,49 +118,151 @@ You know more about your data than Elasticsearch can guess, so while dynamic
118118
mapping can be useful to get started, at some point you will want to specify
119119
your own explicit mappings.
120120

121-
You can create field mappings when you
122-
<<indices-create-index,create an index>>, and you can add
123-
fields to an existing index with the <<indices-put-mapping,PUT mapping API>>.
121+
You can create field mappings when you <<create-mapping,create an index>> and
122+
<<add-field-mapping,add fields to an existing index>>.
124123

125124
[float]
126-
== Updating existing field mappings
125+
[[create-mapping]]
126+
== Create an index with an explicit mapping
127127

128-
Other than where documented, *existing field mappings cannot be
129-
updated*. Changing the mapping would mean invalidating already indexed
130-
documents. Instead, you should create a new index with the correct mappings
131-
and <<docs-reindex,reindex>> your data into that index. If you only wish
132-
to rename a field and not change its mappings, it may make sense to introduce
133-
an <<alias, `alias`>> field.
128+
You can use the <<indices-create-index,create index>> API to create a new index
129+
with an explicit mapping.
130+
131+
[source,js]
132+
----
133+
PUT /my-index
134+
{
135+
"mappings": {
136+
"properties": {
137+
"age": { "type": "integer" }, <1>
138+
"email": { "type": "keyword" }, <2>
139+
"name": { "type": "text" } <3>
140+
}
141+
}
142+
}
143+
----
144+
// CONSOLE
145+
146+
<1> Creates `age`, an <<number,`integer`>> field
147+
<2> Creates `email`, a <<keyword,`keyword`>> field
148+
<3> Creates `name`, a <<text,`text`>> field
134149

135150
[float]
136-
== Example mapping
151+
[[add-field-mapping]]
152+
== Add a field to an existing mapping
137153

138-
A mapping can be specified when creating an index, as follows:
154+
You can use the <<indices-put-mapping, put mapping>> API to add one or more new
155+
fields to an existing index.
156+
157+
The following example adds `employee-id`, a `keyword` field with an
158+
<<mapping-index,`index`>> mapping parameter value of `false`. This means values
159+
for the `employee-id` field are stored but not indexed or available for search.
139160

140161
[source,js]
141-
---------------------------------------
142-
PUT my_index <1>
162+
----
163+
PUT /my-index/_mapping
143164
{
144-
"mappings": {
145-
"properties": { <2>
146-
"title": { "type": "text" }, <3>
147-
"name": { "type": "text" }, <4>
148-
"age": { "type": "integer" }, <5>
149-
"created": {
150-
"type": "date", <6>
151-
"format": "strict_date_optional_time||epoch_millis"
165+
"properties": {
166+
"employee-id": {
167+
"type": "keyword",
168+
"index": false
169+
}
170+
}
171+
}
172+
----
173+
// CONSOLE
174+
// TEST[continued]
175+
176+
[float]
177+
[[update-mapping]]
178+
=== Update the mapping of a field
179+
180+
include::{docdir}/indices/put-mapping.asciidoc[tag=put-field-mapping-exceptions]
181+
182+
[float]
183+
[[view-mapping]]
184+
== View the mapping of an index
185+
186+
You can use the <<indices-get-mapping, get mapping>> API to view the mapping of
187+
an existing index.
188+
189+
[source,js]
190+
----
191+
GET /my-index/_mapping
192+
----
193+
// CONSOLE
194+
// TEST[continued]
195+
196+
The API returns the following response:
197+
198+
[source,js]
199+
----
200+
{
201+
"my-index" : {
202+
"mappings" : {
203+
"properties" : {
204+
"age" : {
205+
"type" : "integer"
206+
},
207+
"email" : {
208+
"type" : "keyword"
209+
},
210+
"employee-id" : {
211+
"type" : "keyword",
212+
"index" : false
213+
},
214+
"name" : {
215+
"type" : "text"
216+
}
152217
}
153218
}
154219
}
155220
}
156-
---------------------------------------
221+
----
222+
// TESTRESPONSE
223+
224+
225+
[float]
226+
[[view-field-mapping]]
227+
== View the mapping of specific fields
228+
229+
If you only want to view the mapping of one or more specific fields, you can use
230+
the <<indices-get-field-mapping, get field mapping>> API.
231+
232+
This is useful if you don't need the complete mapping of an index or your index
233+
contains a large number of fields.
234+
235+
The following request retrieves the mapping for the `employee-id` field.
236+
237+
[source,js]
238+
----
239+
GET /my-index/_mapping/field/employee-id
240+
----
157241
// CONSOLE
158-
<1> Create an index called `my_index`.
159-
<2> Specify the fields or _properties_ in the mapping.
160-
<3> Specify that the `title` field contains `text` values.
161-
<4> Specify that the `name` field contains `text` values.
162-
<5> Specify that the `age` field contains `integer` values.
163-
<6> Specify that the `created` field contains `date` values in two possible formats.
242+
// TEST[continued]
243+
244+
The API returns the following response:
245+
246+
[source,js]
247+
----
248+
{
249+
"my-index" : {
250+
"mappings" : {
251+
"employee-id" : {
252+
"full_name" : "employee-id",
253+
"mapping" : {
254+
"employee-id" : {
255+
"type" : "keyword",
256+
"index" : false
257+
}
258+
}
259+
}
260+
}
261+
}
262+
}
263+
264+
----
265+
// TESTRESPONSE
164266

165267
--
166268

0 commit comments

Comments
 (0)