Skip to content

Commit a3abcdc

Browse files
committed
Consistent APIs: Get field mapping API includes 'mappings'
The get field mapping API now includes a mappings element after the index in its JSON Added more consistent endpoint /{index}/_mapping/{type}/field/{fields} and added endpoint /_mapping/{type}/field/{fields} which are also used in tests Added rest spec tests for wildcards and _all Relates #4071 NOTE: This is not yet complete for 1.0. We need to return an empty JSON document instead of a 404 if the field of an existing index and type is not found. However this is not possible with the current data structure being returned. Needs to be finished for 1.0.
1 parent 349a8be commit a3abcdc

File tree

5 files changed

+112
-26
lines changed

5 files changed

+112
-26
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"methods": ["GET"],
55
"url": {
66
"path": "/_mapping/field/{field}",
7-
"paths": ["/_mapping/field/{field}", "/{index}/_mapping/field/{field}", "/{index}/{type}/_mapping/field/{field}"],
7+
"paths": ["/_mapping/field/{field}", "/{index}/_mapping/field/{field}", "/_mapping/{type}/field/{field}", "/{index}/_mapping/{type}/field/{field}"],
88
"parts": {
99
"index": {
1010
"type" : "list",

rest-api-spec/test/indices.get_field_mapping/10_basic.yaml

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ setup:
1717
indices.get_field_mapping:
1818
field: text
1919

20-
- match: {test_index.test_type.text.mapping.text.type: string}
20+
- match: {test_index.mappings.test_type.text.mapping.text.type: string}
2121

2222
---
2323
"Get field mapping by index only":
@@ -26,7 +26,7 @@ setup:
2626
index: test_index
2727
field: text
2828

29-
- match: {test_index.test_type.text.mapping.text.type: string}
29+
- match: {test_index.mappings.test_type.text.mapping.text.type: string}
3030

3131
---
3232
"Get field mapping by type & field":
@@ -37,7 +37,7 @@ setup:
3737
type: test_type
3838
field: text
3939

40-
- match: {test_index.test_type.text.mapping.text.type: string}
40+
- match: {test_index.mappings.test_type.text.mapping.text.type: string}
4141

4242
---
4343
"Get field mapping by type & field, with another field that doesn't exist":
@@ -48,8 +48,8 @@ setup:
4848
type: test_type
4949
field: [ text , text1 ]
5050

51-
- match: {test_index.test_type.text.mapping.text.type: string}
52-
- is_false: test_index.test_type.text1
51+
- match: {test_index.mappings.test_type.text.mapping.text.type: string}
52+
- is_false: test_index.mappings.test_type.text1
5353

5454
---
5555
"Get field mapping with include_defaults":
@@ -61,5 +61,16 @@ setup:
6161
field: text
6262
include_defaults: true
6363

64-
- match: {test_index.test_type.text.mapping.text.type: string}
65-
- match: {test_index.test_type.text.mapping.text.analyzer: default}
64+
- match: {test_index.mappings.test_type.text.mapping.text.type: string}
65+
- match: {test_index.mappings.test_type.text.mapping.text.analyzer: default}
66+
67+
---
68+
"Get field mapping should work without index specifying type and field":
69+
70+
- do:
71+
indices.get_field_mapping:
72+
type: test_type
73+
field: text
74+
75+
- match: {test_index.mappings.test_type.text.mapping.text.type: string}
76+

rest-api-spec/test/indices.get_field_mapping/50_field_wildcards.yaml

Lines changed: 88 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,50 +23,122 @@ setup:
2323
type: string
2424
index_name: t3
2525

26+
- do:
27+
indices.create:
28+
index: test_index_2
29+
body:
30+
mappings:
31+
test_type_2:
32+
properties:
33+
t1:
34+
type: string
35+
t2:
36+
type: string
37+
obj:
38+
path: just_name
39+
properties:
40+
t1:
41+
type: string
42+
i_t1:
43+
type: string
44+
index_name: t1
45+
i_t3:
46+
type: string
47+
index_name: t3
48+
2649
---
2750
"Get field mapping with * for fields":
2851

2952
- do:
3053
indices.get_field_mapping:
3154
field: "*"
3255

33-
- match: {test_index.test_type.t1.full_name: t1 }
34-
- match: {test_index.test_type.t2.full_name: t2 }
35-
- match: {test_index.test_type.obj\.t1.full_name: obj.t1 }
36-
- match: {test_index.test_type.obj\.i_t1.full_name: obj.i_t1 }
37-
- match: {test_index.test_type.obj\.i_t3.full_name: obj.i_t3 }
56+
- match: {test_index.mappings.test_type.t1.full_name: t1 }
57+
- match: {test_index.mappings.test_type.t2.full_name: t2 }
58+
- match: {test_index.mappings.test_type.obj\.t1.full_name: obj.t1 }
59+
- match: {test_index.mappings.test_type.obj\.i_t1.full_name: obj.i_t1 }
60+
- match: {test_index.mappings.test_type.obj\.i_t3.full_name: obj.i_t3 }
3861

3962
---
4063
"Get field mapping with t* for fields":
4164

4265
- do:
4366
indices.get_field_mapping:
67+
index: test_index
4468
field: "t*"
4569

4670
# i_t1 matches the pattern using it's index name, but t1 already means a full name
4771
# of a field and thus takes precedence.
48-
- match: {test_index.test_type.t1.full_name: t1 }
49-
- match: {test_index.test_type.t2.full_name: t2 }
50-
- match: {test_index.test_type.t3.full_name: obj.i_t3 }
51-
- length: {test_index.test_type: 3}
72+
- match: {test_index.mappings.test_type.t1.full_name: t1 }
73+
- match: {test_index.mappings.test_type.t2.full_name: t2 }
74+
- match: {test_index.mappings.test_type.t3.full_name: obj.i_t3 }
75+
- length: {test_index.mappings.test_type: 3}
5276

5377
---
5478
"Get field mapping with *t1 for fields":
5579

5680
- do:
5781
indices.get_field_mapping:
82+
index: test_index
5883
field: "*t1"
59-
- match: {test_index.test_type.t1.full_name: t1 }
60-
- match: {test_index.test_type.obj\.t1.full_name: obj.t1 }
61-
- match: {test_index.test_type.obj\.i_t1.full_name: obj.i_t1 }
62-
- length: {test_index.test_type: 3}
84+
- match: {test_index.mappings.test_type.t1.full_name: t1 }
85+
- match: {test_index.mappings.test_type.obj\.t1.full_name: obj.t1 }
86+
- match: {test_index.mappings.test_type.obj\.i_t1.full_name: obj.i_t1 }
87+
- length: {test_index.mappings.test_type: 3}
6388

6489
---
6590
"Get field mapping with wildcarded relative names":
6691

6792
- do:
6893
indices.get_field_mapping:
94+
index: test_index
95+
field: "i_*"
96+
- match: {test_index.mappings.test_type.i_t1.full_name: obj.i_t1 }
97+
- match: {test_index.mappings.test_type.i_t3.full_name: obj.i_t3 }
98+
- length: {test_index.mappings.test_type: 2}
99+
100+
---
101+
"Get field mapping should work using '_all' for indices and types":
102+
103+
- do:
104+
indices.get_field_mapping:
105+
index: _all
106+
type: _all
107+
field: "i_*"
108+
- match: {test_index.mappings.test_type.i_t1.full_name: obj.i_t1 }
109+
- match: {test_index.mappings.test_type.i_t3.full_name: obj.i_t3 }
110+
- length: {test_index.mappings.test_type: 2}
111+
- match: {test_index_2.mappings.test_type_2.i_t1.full_name: obj.i_t1 }
112+
- match: {test_index_2.mappings.test_type_2.i_t3.full_name: obj.i_t3 }
113+
- length: {test_index_2.mappings.test_type_2: 2}
114+
115+
---
116+
"Get field mapping should work using '*' for indices and types":
117+
118+
- do:
119+
indices.get_field_mapping:
120+
index: '*'
121+
type: '*'
122+
field: "i_*"
123+
- match: {test_index.mappings.test_type.i_t1.full_name: obj.i_t1 }
124+
- match: {test_index.mappings.test_type.i_t3.full_name: obj.i_t3 }
125+
- length: {test_index.mappings.test_type: 2}
126+
- match: {test_index_2.mappings.test_type_2.i_t1.full_name: obj.i_t1 }
127+
- match: {test_index_2.mappings.test_type_2.i_t3.full_name: obj.i_t3 }
128+
- length: {test_index_2.mappings.test_type_2: 2}
129+
130+
---
131+
"Get field mapping should work using comma_separated values for indices and types":
132+
133+
- do:
134+
indices.get_field_mapping:
135+
index: 'test_index,test_index_2'
136+
type: 'test_type,test_type_2'
69137
field: "i_*"
70-
- match: {test_index.test_type.i_t1.full_name: obj.i_t1 }
71-
- match: {test_index.test_type.i_t3.full_name: obj.i_t3 }
72-
- length: {test_index.test_type: 2}
138+
- match: {test_index.mappings.test_type.i_t1.full_name: obj.i_t1 }
139+
- match: {test_index.mappings.test_type.i_t3.full_name: obj.i_t3 }
140+
- length: {test_index.mappings.test_type: 2}
141+
- match: {test_index_2.mappings.test_type_2.i_t1.full_name: obj.i_t1 }
142+
- match: {test_index_2.mappings.test_type_2.i_t3.full_name: obj.i_t3 }
143+
- length: {test_index_2.mappings.test_type_2: 2}
144+

src/main/java/org/elasticsearch/action/admin/indices/mapping/get/GetFieldMappingsResponse.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public FieldMappingMetaData fieldMappings(String index, String type, String fiel
7171
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
7272
for (Map.Entry<String, ImmutableMap<String, ImmutableMap<String, FieldMappingMetaData>>> indexEntry : mappings.entrySet()) {
7373
builder.startObject(indexEntry.getKey(), XContentBuilder.FieldCaseConversion.NONE);
74+
builder.startObject("mappings");
7475
for (Map.Entry<String, ImmutableMap<String, FieldMappingMetaData>> typeEntry : indexEntry.getValue().entrySet()) {
7576
builder.startObject(typeEntry.getKey(), XContentBuilder.FieldCaseConversion.NONE);
7677
for (Map.Entry<String, FieldMappingMetaData> fieldEntry : typeEntry.getValue().entrySet()) {
@@ -81,6 +82,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
8182
builder.endObject();
8283
}
8384
builder.endObject();
85+
builder.endObject();
8486
}
8587
return builder;
8688
}

src/main/java/org/elasticsearch/rest/action/admin/indices/mapping/get/RestGetFieldMappingAction.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,18 @@ public class RestGetFieldMappingAction extends BaseRestHandler {
4949
public RestGetFieldMappingAction(Settings settings, Client client, RestController controller) {
5050
super(settings, client);
5151
controller.registerHandler(GET, "/_mapping/field/{fields}", this);
52+
controller.registerHandler(GET, "/_mapping/{type}/field/{fields}", this);
5253
controller.registerHandler(GET, "/{index}/_mapping/field/{fields}", this);
5354
controller.registerHandler(GET, "/{index}/{type}/_mapping/field/{fields}", this);
55+
controller.registerHandler(GET, "/{index}/_mapping/{type}/field/{fields}", this);
5456
}
5557

5658
@Override
5759
public void handleRequest(final RestRequest request, final RestChannel channel) {
5860
final String[] indices = Strings.splitStringByCommaToArray(request.param("index"));
59-
final String[] types = Strings.splitStringByCommaToArray(request.param("type"));
61+
final String[] types = request.paramAsStringArrayOrEmptyIfAll("type");
6062
boolean local = request.paramAsBooleanOptional("local", false);
6163
final String[] fields = Strings.splitStringByCommaToArray(request.param("fields"));
62-
6364
GetFieldMappingsRequest getMappingsRequest = new GetFieldMappingsRequest();
6465
getMappingsRequest.indices(indices).types(types).local(local).fields(fields).includeDefaults(request.paramAsBoolean("include_defaults", false));
6566
getMappingsRequest.indicesOptions(IndicesOptions.fromRequest(request, getMappingsRequest.indicesOptions()));

0 commit comments

Comments
 (0)