Skip to content

Commit fcea5e0

Browse files
committed
add PUT options for _mapping
Single type can now be added with `[PUT|POST] {index|_all|*|regex|blank}/[_mapping|_mappings]/type` and `[PUT|POST] {index|_all|*|regex|blank}/type/[_mapping|_mappings]` see roadmap at issue elastic#4071
1 parent 6be2066 commit fcea5e0

File tree

5 files changed

+264
-6
lines changed

5 files changed

+264
-6
lines changed

docs/reference/indices/put-mapping.asciidoc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,25 @@ $ curl -XPUT 'http://localhost:9200/kimchy,elasticsearch/tweet/_mapping' -d '
5959
}
6060
'
6161
--------------------------------------------------
62+
63+
All options:
64+
65+
[source,js]
66+
--------------------------------------------------
67+
68+
[PUT|POST] /{index}/_mapping/{type}
69+
70+
[PUT|POST] /{index}/{type}/_mapping
71+
72+
--------------------------------------------------
73+
74+
75+
where
76+
77+
[horizontal]
78+
`{index}`:: `blank | * | _all | glob pattern | name1, name2, …`
79+
80+
`{type}`:: Name of the type to add. Must be the name of the type defined in the body.
81+
82+
83+
Instead of `_mapping` you can also use the plural `_mappings`.

rest-api-spec/api/indices.put_mapping.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
"methods": ["PUT", "POST"],
55
"url": {
66
"path": "/{index}/{type}/_mapping",
7-
"paths": ["/{index}/{type}/_mapping"],
7+
"paths": ["/{index}/{type}/_mapping", "/{type}/_mapping", "/{index}/_mapping/{type}", "/_mapping/{type}", "/{index}/{type}/_mappings", "/{type}/_mappings", "/{index}/_mappings/{type}", "/_mappings/{type}"],
88
"parts": {
99
"index": {
1010
"type" : "list",
11-
"required" : true,
11+
"required" : false,
1212
"description" : "A comma-separated list of index names; use `_all` to perform the operation on all indices"
1313
},
1414
"type": {
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
---
2+
"Test Create and update mapping with all options (PUT {index|_all|*|prefix*}/type/_mapping)":
3+
4+
# create two indices
5+
- do:
6+
indices.create:
7+
index: test_index1
8+
- do:
9+
indices.create:
10+
index: test_index2
11+
12+
# put mapping per index
13+
- do:
14+
indices.put_mapping:
15+
index: test_index1
16+
type: test_type
17+
body:
18+
test_type:
19+
properties:
20+
text:
21+
type: string
22+
analyzer: whitespace
23+
- do:
24+
indices.put_mapping:
25+
index: test_index2
26+
type: test_type
27+
body:
28+
test_type:
29+
properties:
30+
text:
31+
type: string
32+
analyzer: whitespace
33+
34+
35+
- do:
36+
indices.get_mapping:
37+
index: test_index1
38+
39+
- match: {test_index1.test_type.properties.text.type: string}
40+
- match: {test_index1.test_type.properties.text.analyzer: whitespace}
41+
42+
- do:
43+
indices.get_mapping:
44+
index: test_index2
45+
46+
- match: {test_index2.test_type.properties.text.type: string}
47+
- match: {test_index2.test_type.properties.text.analyzer: whitespace}
48+
49+
50+
51+
# put mapping in _all index
52+
- do:
53+
indices.put_mapping:
54+
index: _all
55+
type: test_type1
56+
body:
57+
test_type1:
58+
properties:
59+
text:
60+
type: string
61+
analyzer: whitespace
62+
63+
- do:
64+
indices.get_mapping:
65+
index: test_index1
66+
67+
- match: {test_index1.test_type1.properties.text.type: string}
68+
- match: {test_index1.test_type1.properties.text.analyzer: whitespace}
69+
70+
- do:
71+
indices.get_mapping:
72+
index: test_index2
73+
74+
- match: {test_index2.test_type1.properties.text.type: string}
75+
- match: {test_index2.test_type1.properties.text.analyzer: whitespace}
76+
77+
78+
# put mapping in * index
79+
- do:
80+
indices.put_mapping:
81+
index: "*"
82+
type: test_type2
83+
body:
84+
test_type2:
85+
properties:
86+
text:
87+
type: string
88+
analyzer: whitespace
89+
90+
- do:
91+
indices.get_mapping:
92+
index: test_index1
93+
94+
- match: {test_index1.test_type2.properties.text.type: string}
95+
- match: {test_index1.test_type2.properties.text.analyzer: whitespace}
96+
97+
- do:
98+
indices.get_mapping:
99+
index: test_index2
100+
101+
- match: {test_index2.test_type2.properties.text.type: string}
102+
- match: {test_index2.test_type2.properties.text.analyzer: whitespace}
103+
104+
# put mapping prefix* index
105+
- do:
106+
indices.put_mapping:
107+
index: "test_index*"
108+
type: test_type3
109+
body:
110+
test_type3:
111+
properties:
112+
text:
113+
type: string
114+
analyzer: whitespace
115+
116+
- do:
117+
indices.get_mapping:
118+
index: test_index1
119+
120+
- match: {test_index1.test_type3.properties.text.type: string}
121+
- match: {test_index1.test_type3.properties.text.analyzer: whitespace}
122+
123+
- do:
124+
indices.get_mapping:
125+
index: test_index2
126+
127+
- match: {test_index2.test_type3.properties.text.type: string}
128+
- match: {test_index2.test_type3.properties.text.analyzer: whitespace}
129+
130+
131+
132+
# put mapping in list of indices
133+
- do:
134+
indices.put_mapping:
135+
index: [test_index1, test_index2]
136+
type: test_type4
137+
body:
138+
test_type4:
139+
properties:
140+
text:
141+
type: string
142+
analyzer: whitespace
143+
144+
- do:
145+
indices.get_mapping:
146+
index: test_index1
147+
148+
- match: {test_index1.test_type4.properties.text.type: string}
149+
- match: {test_index1.test_type4.properties.text.analyzer: whitespace}
150+
151+
- do:
152+
indices.get_mapping:
153+
index: test_index2
154+
155+
- match: {test_index2.test_type4.properties.text.type: string}
156+
- match: {test_index2.test_type4.properties.text.analyzer: whitespace}
157+
158+
159+
# put mapping with blank index
160+
- do:
161+
indices.put_mapping:
162+
type: test_type5
163+
body:
164+
test_type5:
165+
properties:
166+
text:
167+
type: string
168+
analyzer: whitespace
169+
170+
- do:
171+
indices.get_mapping:
172+
index: test_index1
173+
174+
- match: {test_index1.test_type5.properties.text.type: string}
175+
- match: {test_index1.test_type5.properties.text.analyzer: whitespace}
176+
177+
- do:
178+
indices.get_mapping:
179+
index: test_index2
180+
181+
- match: {test_index2.test_type5.properties.text.type: string}
182+
- match: {test_index2.test_type5.properties.text.analyzer: whitespace}
183+
184+

src/main/java/org/elasticsearch/rest/action/admin/indices/mapping/put/RestPutMappingAction.java

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,60 @@
3737
*/
3838
public class RestPutMappingAction extends BaseRestHandler {
3939

40+
/*
41+
* This TestHandler is a workaround. With the current path registration
42+
* (PathTrie) registering /{type}/_mapping in order to leave the index
43+
* blank, the first parameter in the path will still map to "index". * In
44+
* order to register /{type}/_mapping as path, we instead register
45+
* /{index}/_mapping and interpret the parameter "index" as type if no type
46+
* is given in the uri parameters.
47+
*/
48+
protected final class RestHandlerForMissingIndex implements RestHandler {
49+
@Override
50+
public void handleRequest(RestRequest request, RestChannel channel) {
51+
PutMappingRequest putMappingRequest;
52+
if (request.param("type") != null) {
53+
putMappingRequest = putMappingRequest(Strings.splitStringByCommaToArray(request.param("index")));
54+
putMappingRequest.type(request.param("type"));
55+
} else {
56+
putMappingRequest = putMappingRequest(new String[0]);
57+
putMappingRequest.type(request.param("index"));
58+
}
59+
putMappingRequest.listenerThreaded(false);
60+
putMappingRequest.source(request.content().toUtf8());
61+
putMappingRequest.timeout(request.paramAsTime("timeout", putMappingRequest.timeout()));
62+
putMappingRequest.ignoreConflicts(request.paramAsBoolean("ignore_conflicts", putMappingRequest.ignoreConflicts()));
63+
putMappingRequest.masterNodeTimeout(request.paramAsTime("master_timeout", putMappingRequest.masterNodeTimeout()));
64+
putMappingRequest.indicesOptions(IndicesOptions.fromRequest(request, putMappingRequest.indicesOptions()));
65+
client.admin()
66+
.indices()
67+
.putMapping(putMappingRequest, new AcknowledgedRestResponseActionListener<PutMappingResponse>(request, channel, logger));
68+
}
69+
}
70+
4071
@Inject
4172
public RestPutMappingAction(Settings settings, Client client, RestController controller) {
4273
super(settings, client);
43-
controller.registerHandler(PUT, "/{index}/_mapping", this);
74+
controller.registerHandler(PUT, "/{index}/_mapping/", new RestHandlerForMissingIndex());
4475
controller.registerHandler(PUT, "/{index}/{type}/_mapping", this);
76+
controller.registerHandler(PUT, "/{index}/_mapping/{type}", this);
77+
controller.registerHandler(PUT, "/_mapping/{type}", this);
4578

46-
controller.registerHandler(POST, "/{index}/_mapping", this);
79+
controller.registerHandler(POST, "/{index}/_mapping/", new RestHandlerForMissingIndex());
4780
controller.registerHandler(POST, "/{index}/{type}/_mapping", this);
81+
controller.registerHandler(POST, "/{index}/_mapping/{type}", this);
82+
controller.registerHandler(POST, "/_mapping/{type}", this);
83+
84+
//register the same paths, but with plural form _mappings
85+
controller.registerHandler(PUT, "/{index}/_mappings/", new RestHandlerForMissingIndex());
86+
controller.registerHandler(PUT, "/{index}/{type}/_mappings", this);
87+
controller.registerHandler(PUT, "/{index}/_mappings/{type}", this);
88+
controller.registerHandler(PUT, "/_mappings/{type}", this);
89+
90+
controller.registerHandler(POST, "/{index}/_mappings/", new RestHandlerForMissingIndex());
91+
controller.registerHandler(POST, "/{index}/{type}/_mappings", this);
92+
controller.registerHandler(POST, "/{index}/_mappings/{type}", this);
93+
controller.registerHandler(POST, "/_mappings/{type}", this);
4894
}
4995

5096
@Override

src/test/java/org/elasticsearch/indices/IndicesOptionsTests.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -623,12 +623,18 @@ public void testPutMapping() throws Exception {
623623
assertThat(client().admin().indices().prepareGetMappings("foobar").get().mappings().get("foobar").get("type2"), notNullValue());
624624
assertThat(client().admin().indices().prepareGetMappings("bar").get().mappings().get("bar").get("type2"), notNullValue());
625625
assertThat(client().admin().indices().prepareGetMappings("barbaz").get().mappings().get("barbaz").get("type2"), notNullValue());
626+
verify(client().admin().indices().preparePutMapping().setType("type3").setSource("field", "type=string"), false);
627+
assertThat(client().admin().indices().prepareGetMappings("foo").get().mappings().get("foo").get("type3"), notNullValue());
628+
assertThat(client().admin().indices().prepareGetMappings("foobar").get().mappings().get("foobar").get("type3"), notNullValue());
629+
assertThat(client().admin().indices().prepareGetMappings("bar").get().mappings().get("bar").get("type3"), notNullValue());
630+
assertThat(client().admin().indices().prepareGetMappings("barbaz").get().mappings().get("barbaz").get("type3"), notNullValue());
631+
626632

627633
verify(client().admin().indices().preparePutMapping("c*").setType("type1").setSource("field", "type=string"), true);
628634

629635
assertAcked(client().admin().indices().prepareClose("barbaz").get());
630-
verify(client().admin().indices().preparePutMapping("barbaz").setType("type3").setSource("field", "type=string"), false);
631-
assertThat(client().admin().indices().prepareGetMappings("barbaz").get().mappings().get("barbaz").get("type3"), notNullValue());
636+
verify(client().admin().indices().preparePutMapping("barbaz").setType("type4").setSource("field", "type=string"), false);
637+
assertThat(client().admin().indices().prepareGetMappings("barbaz").get().mappings().get("barbaz").get("type4"), notNullValue());
632638
}
633639

634640
@Test

0 commit comments

Comments
 (0)