Skip to content

Commit 2f5300e

Browse files
author
Christoph Büscher
authored
Deprecate types in get_source and exist_source (#36426)
This change adds a new untyped endpoint `{index}/_source/{id}` for both the GET and the HEAD methods to get the source of a document or check for its existance. It also adds deprecation warnings to RestGetSourceAction that emit a warning when the old deprecated "type" parameter is still used. Also updating documentation and tests where appropriate. Relates to #35190
1 parent f0f2b26 commit 2f5300e

File tree

25 files changed

+393
-44
lines changed

25 files changed

+393
-44
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,14 @@ private static Request getStyleRequest(String method, GetRequest getRequest) {
268268
}
269269

270270
static Request sourceExists(GetRequest getRequest) {
271-
Request request = new Request(HttpHead.METHOD_NAME, endpoint(getRequest.index(), getRequest.type(), getRequest.id(), "_source"));
272-
271+
String optionalType = getRequest.type();
272+
String endpoint;
273+
if (optionalType.equals(MapperService.SINGLE_MAPPING_NAME)) {
274+
endpoint = endpoint(getRequest.index(), "_source", getRequest.id());
275+
} else {
276+
endpoint = endpoint(getRequest.index(), optionalType, getRequest.id(), "_source");
277+
}
278+
Request request = new Request(HttpHead.METHOD_NAME, endpoint);
273279
Params parameters = new Params(request);
274280
parameters.withPreference(getRequest.preference());
275281
parameters.withRouting(getRequest.routing());

client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
import org.elasticsearch.common.xcontent.XContentType;
7474
import org.elasticsearch.common.xcontent.json.JsonXContent;
7575
import org.elasticsearch.index.VersionType;
76+
import org.elasticsearch.index.mapper.MapperService;
7677
import org.elasticsearch.index.query.QueryBuilders;
7778
import org.elasticsearch.index.query.TermQueryBuilder;
7879
import org.elasticsearch.index.rankeval.PrecisionAtK;
@@ -115,6 +116,7 @@
115116
import java.util.Locale;
116117
import java.util.Map;
117118
import java.util.StringJoiner;
119+
import java.util.function.BiFunction;
118120
import java.util.function.Consumer;
119121
import java.util.function.Function;
120122
import java.util.function.Supplier;
@@ -156,6 +158,58 @@ public void testGetWithType() {
156158
getAndExistsWithTypeTest(RequestConverters::get, HttpGet.METHOD_NAME);
157159
}
158160

161+
public void testSourceExists() throws IOException {
162+
doTestSourceExists((index, id) -> new GetRequest(index, id));
163+
}
164+
165+
public void testSourceExistsWithType() throws IOException {
166+
String type = frequently() ? randomAlphaOfLengthBetween(3, 10) : MapperService.SINGLE_MAPPING_NAME;
167+
doTestSourceExists((index, id) -> new GetRequest(index, type, id));
168+
}
169+
170+
private static void doTestSourceExists(BiFunction<String, String, GetRequest> requestFunction) throws IOException {
171+
String index = randomAlphaOfLengthBetween(3, 10);
172+
String id = randomAlphaOfLengthBetween(3, 10);
173+
final GetRequest getRequest = requestFunction.apply(index, id);
174+
175+
Map<String, String> expectedParams = new HashMap<>();
176+
if (randomBoolean()) {
177+
String preference = randomAlphaOfLengthBetween(3, 10);
178+
getRequest.preference(preference);
179+
expectedParams.put("preference", preference);
180+
}
181+
if (randomBoolean()) {
182+
String routing = randomAlphaOfLengthBetween(3, 10);
183+
getRequest.routing(routing);
184+
expectedParams.put("routing", routing);
185+
}
186+
if (randomBoolean()) {
187+
boolean realtime = randomBoolean();
188+
getRequest.realtime(realtime);
189+
if (realtime == false) {
190+
expectedParams.put("realtime", "false");
191+
}
192+
}
193+
if (randomBoolean()) {
194+
boolean refresh = randomBoolean();
195+
getRequest.refresh(refresh);
196+
if (refresh) {
197+
expectedParams.put("refresh", "true");
198+
}
199+
}
200+
Request request = RequestConverters.sourceExists(getRequest);
201+
assertEquals(HttpHead.METHOD_NAME, request.getMethod());
202+
String type = getRequest.type();
203+
if (type.equals(MapperService.SINGLE_MAPPING_NAME)) {
204+
assertEquals("/" + index + "/_source/" + id, request.getEndpoint());
205+
} else {
206+
assertEquals("/" + index + "/" + type + "/" + id + "/_source", request.getEndpoint());
207+
}
208+
209+
assertEquals(expectedParams, request.getParameters());
210+
assertNull(request.getEntity());
211+
}
212+
159213
public void testMultiGet() throws IOException {
160214
Map<String, String> expectedParams = new HashMap<>();
161215
MultiGetRequest multiGetRequest = new MultiGetRequest();

client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/CRUDDocumentationIT.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1265,7 +1265,6 @@ public void testGet() throws Exception {
12651265
assertEquals(3, getResponse.getSourceAsMap().size());
12661266
//tag::get-response
12671267
String index = getResponse.getIndex();
1268-
String type = getResponse.getType();
12691268
String id = getResponse.getId();
12701269
if (getResponse.isExists()) {
12711270
long version = getResponse.getVersion();

docs/reference/docs/get.asciidoc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
[[docs-get]]
22
== Get API
33

4-
The get API allows to get a typed JSON document from the index based on
4+
The get API allows to get a JSON document from the index based on
55
its id. The following example gets a JSON document from an index called
6-
twitter, under a type called `_doc`, with id valued 0:
6+
twitter with id valued 0:
77

88
[source,js]
99
--------------------------------------------------
@@ -34,7 +34,7 @@ The result of the above get operation is:
3434
--------------------------------------------------
3535
// TESTRESPONSE[s/"_seq_no" : \d+/"_seq_no" : $body._seq_no/ s/"_primary_term" : 1/"_primary_term" : $body._primary_term/]
3636

37-
The above result includes the `_index`, `_type`, `_id` and `_version`
37+
The above result includes the `_index`, `_id` and `_version`
3838
of the document we wish to retrieve, including the actual `_source`
3939
of the document if it could be found (as indicated by the `found`
4040
field in the response).
@@ -223,13 +223,13 @@ will fail.
223223
[[_source]]
224224
=== Getting the +_source+ directly
225225

226-
Use the `/{index}/{type}/{id}/_source` endpoint to get
226+
Use the `/{index}/_source/{id}` endpoint to get
227227
just the `_source` field of the document,
228228
without any additional content around it. For example:
229229

230230
[source,js]
231231
--------------------------------------------------
232-
GET twitter/_doc/1/_source
232+
GET twitter/_source/1
233233
--------------------------------------------------
234234
// CONSOLE
235235
// TEST[continued]
@@ -238,7 +238,7 @@ You can also use the same source filtering parameters to control which parts of
238238

239239
[source,js]
240240
--------------------------------------------------
241-
GET twitter/_doc/1/_source?_source_includes=*.id&_source_excludes=entities'
241+
GET twitter/_source/1/?_source_includes=*.id&_source_excludes=entities'
242242
--------------------------------------------------
243243
// CONSOLE
244244
// TEST[continued]
@@ -248,7 +248,7 @@ An existing document will not have a _source if it is disabled in the <<mapping-
248248

249249
[source,js]
250250
--------------------------------------------------
251-
HEAD twitter/_doc/1/_source
251+
HEAD twitter/_source/1
252252
--------------------------------------------------
253253
// CONSOLE
254254
// TEST[continued]

modules/transport-netty4/src/test/java/org/elasticsearch/rest/Netty4HeadBodyIsEmptyIT.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ public void testTemplateExists() throws IOException {
149149

150150
public void testGetSourceAction() throws IOException {
151151
createTestDoc();
152-
headTestCase("/test/test/1/_source", emptyMap(), greaterThan(0));
153-
headTestCase("/test/test/2/_source", emptyMap(), NOT_FOUND.getStatus(), greaterThan(0));
152+
headTestCase("/test/_source/1", emptyMap(), greaterThan(0));
153+
headTestCase("/test/_source/2", emptyMap(), NOT_FOUND.getStatus(), greaterThan(0));
154154

155155
try (XContentBuilder builder = jsonBuilder()) {
156156
builder.startObject();
@@ -175,7 +175,7 @@ public void testGetSourceAction() throws IOException {
175175
request.setJsonEntity(Strings.toString(builder));
176176
client().performRequest(request);
177177
createTestDoc("test-no-source", "test-no-source");
178-
headTestCase("/test-no-source/test-no-source/1/_source", emptyMap(), NOT_FOUND.getStatus(), greaterThan(0));
178+
headTestCase("/test-no-source/_source/1", emptyMap(), NOT_FOUND.getStatus(), greaterThan(0));
179179
}
180180
}
181181

rest-api-spec/src/main/resources/rest-api-spec/api/exists_source.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"documentation": "http://www.elastic.co/guide/en/elasticsearch/reference/master/docs-get.html",
44
"methods": ["HEAD"],
55
"url": {
6-
"path": "/{index}/{type}/{id}/_source",
7-
"paths": ["/{index}/{type}/{id}/_source"],
6+
"path": "/{index}/_source/{id}",
7+
"paths": ["/{index}/_source/{id}", "/{index}/{type}/{id}/_source"],
88
"parts": {
99
"id": {
1010
"type" : "string",
@@ -18,8 +18,8 @@
1818
},
1919
"type": {
2020
"type" : "string",
21-
"required" : true,
22-
"description" : "The type of the document; use `_all` to fetch the first document matching the ID across all types"
21+
"required" : false,
22+
"description" : "The type of the document; deprecated and optional starting with 7.0"
2323
}
2424
},
2525
"params": {

rest-api-spec/src/main/resources/rest-api-spec/api/get_source.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"documentation": "http://www.elastic.co/guide/en/elasticsearch/reference/master/docs-get.html",
44
"methods": ["GET"],
55
"url": {
6-
"path": "/{index}/{type}/{id}/_source",
7-
"paths": ["/{index}/{type}/{id}/_source"],
6+
"path": "/{index}/_source/{id}",
7+
"paths": ["/{index}/_source/{id}", "/{index}/{type}/{id}/_source"],
88
"parts": {
99
"id": {
1010
"type" : "string",
@@ -18,8 +18,8 @@
1818
},
1919
"type": {
2020
"type" : "string",
21-
"required" : true,
22-
"description" : "The type of the document; use `_all` to fetch the first document matching the ID across all types"
21+
"required" : false,
22+
"description" : "The type of the document; deprecated and optional starting with 7.0"
2323
}
2424
},
2525
"params": {
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
---
22
"Basic":
3+
4+
- skip:
5+
version: " - 6.99.99"
6+
reason: types are required in requests before 7.0.0
7+
38
- do:
49
index:
510
index: test_1
6-
type: test
711
id: 1
812
body: { "foo": "bar" }
913

1014
- do:
1115
get_source:
1216
index: test_1
13-
type: test
1417
id: 1
1518

1619
- match: { '': { foo: bar } }
1720

1821
- do:
1922
get_source:
2023
index: test_1
21-
type: _all
2224
id: 1
2325

2426
- match: { '': { foo: bar } }
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
"Basic with types":
3+
4+
- do:
5+
index:
6+
index: test_1
7+
type: test
8+
id: 1
9+
body: { "foo": "bar" }
10+
11+
- do:
12+
get_source:
13+
index: test_1
14+
type: test
15+
id: 1
16+
17+
- match: { '': { foo: bar } }

rest-api-spec/src/main/resources/rest-api-spec/test/get_source/15_default_values.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
---
22
"Default values":
3+
4+
5+
- skip:
6+
version: " - 6.99.99"
7+
reason: types are required in requests before 7.0.0
8+
39
- do:
410
index:
511
index: test_1
@@ -10,7 +16,6 @@
1016
- do:
1117
get_source:
1218
index: test_1
13-
type: _all
1419
id: 1
1520

1621
- match: { '': { foo: bar } }
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
"Default values":
3+
- do:
4+
index:
5+
index: test_1
6+
type: test
7+
id: 1
8+
body: { "foo": "bar" }
9+
10+
- do:
11+
get_source:
12+
index: test_1
13+
type: test
14+
id: 1
15+
16+
- match: { '': { foo: bar } }

rest-api-spec/src/main/resources/rest-api-spec/test/get_source/40_routing.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
---
22
"Routing":
33

4+
5+
- skip:
6+
version: " - 6.99.99"
7+
reason: types are required in requests before 7.0.0
8+
49
- do:
510
indices.create:
611
index: test_1
@@ -26,7 +31,6 @@
2631
- do:
2732
get_source:
2833
index: test_1
29-
type: test
3034
id: 1
3135
routing: 5
3236

@@ -36,6 +40,5 @@
3640
catch: missing
3741
get_source:
3842
index: test_1
39-
type: test
4043
id: 1
4144

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
"Routing":
3+
4+
5+
- do:
6+
indices.create:
7+
index: test_1
8+
body:
9+
settings:
10+
index:
11+
number_of_shards: 5
12+
number_of_routing_shards: 5
13+
number_of_replicas: 0
14+
15+
- do:
16+
cluster.health:
17+
wait_for_status: green
18+
19+
- do:
20+
index:
21+
index: test_1
22+
type: test
23+
id: 1
24+
routing: 5
25+
body: { foo: bar }
26+
27+
- do:
28+
get_source:
29+
index: test_1
30+
type: test
31+
id: 1
32+
routing: 5
33+
34+
- match: { '': {foo: bar}}
35+
36+
- do:
37+
catch: missing
38+
get_source:
39+
index: test_1
40+
type: test
41+
id: 1
42+

0 commit comments

Comments
 (0)