Skip to content

Commit 29f3b04

Browse files
author
Christoph Büscher
committed
Add typless client side GetIndexRequest calls and response class (#37778)
The HLRC client currently uses `org.elasticsearch.action.admin.indices.get.GetIndexRequest` and `org.elasticsearch.action.admin.indices.get.GetIndexResponse` in its get index calls. Both request and response are designed for the typed APIs, including some return types e.g. for `getMappings()` which in the maps it returns still use a level including the type name. In order to change this without breaking existing users of the HLRC API, this PR introduces two new request and response objects in the `org.elasticsearch.client.indices` client package. These are used by the IndicesClient#get and IndicesClient#exists calls now by default and support the type-less API. The old request and response objects are still kept for use in similarly named, but deprecated methods. The newly introduced client side classes are simplified versions of the server side request/response classes since they don't need to support wire serialization, and only the response needs fromXContent parsing (but no xContent-serialization, since this is the responsibility of the server-side class). Also changing the return type of `GetIndexResponse#getMapping` to `Map<String, MappingMetaData> getMappings()`, while it previously was returning another map keyed by the type-name. Similar getters return simple Maps instead of the ImmutableOpenMaps that the server side response objects return.
1 parent a123582 commit 29f3b04

File tree

16 files changed

+994
-145
lines changed

16 files changed

+994
-145
lines changed

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

Lines changed: 104 additions & 23 deletions
Large diffs are not rendered by default.

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

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
import org.elasticsearch.action.admin.indices.flush.FlushRequest;
3434
import org.elasticsearch.action.admin.indices.flush.SyncedFlushRequest;
3535
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest;
36-
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
37-
import org.elasticsearch.client.indices.GetFieldMappingsRequest;
3836
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
3937
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
4038
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest;
@@ -46,6 +44,8 @@
4644
import org.elasticsearch.action.support.ActiveShardCount;
4745
import org.elasticsearch.client.indices.CreateIndexRequest;
4846
import org.elasticsearch.client.indices.FreezeIndexRequest;
47+
import org.elasticsearch.client.indices.GetFieldMappingsRequest;
48+
import org.elasticsearch.client.indices.GetIndexRequest;
4949
import org.elasticsearch.client.indices.GetIndexTemplatesRequest;
5050
import org.elasticsearch.client.indices.GetMappingsRequest;
5151
import org.elasticsearch.client.indices.IndexTemplatesExistRequest;
@@ -54,7 +54,6 @@
5454
import org.elasticsearch.client.indices.UnfreezeIndexRequest;
5555
import org.elasticsearch.client.indices.rollover.RolloverRequest;
5656
import org.elasticsearch.common.Strings;
57-
import org.elasticsearch.rest.BaseRestHandler;
5857

5958
import java.io.IOException;
6059
import java.util.Locale;
@@ -152,6 +151,10 @@ static Request putMapping(PutMappingRequest putMappingRequest) throws IOExceptio
152151
return request;
153152
}
154153

154+
/**
155+
* converter for the legacy server-side {@link org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest} that still supports
156+
* types
157+
*/
155158
@Deprecated
156159
static Request putMapping(org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest putMappingRequest) throws IOException {
157160
// The concreteIndex is an internal concept, not applicable to requests made over the REST API.
@@ -396,6 +399,28 @@ static Request getSettings(GetSettingsRequest getSettingsRequest) {
396399
return request;
397400
}
398401

402+
/**
403+
* converter for the legacy server-side {@link org.elasticsearch.action.admin.indices.get.GetIndexRequest} that
404+
* still supports types
405+
*/
406+
@Deprecated
407+
static Request getIndex(org.elasticsearch.action.admin.indices.get.GetIndexRequest getIndexRequest) {
408+
String[] indices = getIndexRequest.indices() == null ? Strings.EMPTY_ARRAY : getIndexRequest.indices();
409+
410+
String endpoint = RequestConverters.endpoint(indices);
411+
Request request = new Request(HttpGet.METHOD_NAME, endpoint);
412+
413+
RequestConverters.Params params = new RequestConverters.Params(request);
414+
params.withIndicesOptions(getIndexRequest.indicesOptions());
415+
params.withLocal(getIndexRequest.local());
416+
params.withIncludeDefaults(getIndexRequest.includeDefaults());
417+
params.withHuman(getIndexRequest.humanReadable());
418+
params.withMasterTimeout(getIndexRequest.masterNodeTimeout());
419+
params.putParam(INCLUDE_TYPE_NAME_PARAMETER, Boolean.TRUE.toString());
420+
421+
return request;
422+
}
423+
399424
static Request getIndex(GetIndexRequest getIndexRequest) {
400425
String[] indices = getIndexRequest.indices() == null ? Strings.EMPTY_ARRAY : getIndexRequest.indices();
401426

@@ -408,12 +433,33 @@ static Request getIndex(GetIndexRequest getIndexRequest) {
408433
params.withIncludeDefaults(getIndexRequest.includeDefaults());
409434
params.withHuman(getIndexRequest.humanReadable());
410435
params.withMasterTimeout(getIndexRequest.masterNodeTimeout());
411-
// Force "include_type_name" parameter since responses need to be compatible when coming from 7.0 nodes
412-
params.putParam(BaseRestHandler.INCLUDE_TYPE_NAME_PARAMETER, Boolean.TRUE.toString());
436+
params.putParam(INCLUDE_TYPE_NAME_PARAMETER, Boolean.FALSE.toString());
413437

414438
return request;
415439
}
416440

441+
/**
442+
* converter for the legacy server-side {@link org.elasticsearch.action.admin.indices.get.GetIndexRequest} that
443+
* still supports types
444+
*/
445+
@Deprecated
446+
static Request indicesExist(org.elasticsearch.action.admin.indices.get.GetIndexRequest getIndexRequest) {
447+
// this can be called with no indices as argument by transport client, not via REST though
448+
if (getIndexRequest.indices() == null || getIndexRequest.indices().length == 0) {
449+
throw new IllegalArgumentException("indices are mandatory");
450+
}
451+
String endpoint = RequestConverters.endpoint(getIndexRequest.indices(), "");
452+
Request request = new Request(HttpHead.METHOD_NAME, endpoint);
453+
454+
RequestConverters.Params params = new RequestConverters.Params(request);
455+
params.withLocal(getIndexRequest.local());
456+
params.withHuman(getIndexRequest.humanReadable());
457+
params.withIndicesOptions(getIndexRequest.indicesOptions());
458+
params.withIncludeDefaults(getIndexRequest.includeDefaults());
459+
params.putParam(INCLUDE_TYPE_NAME_PARAMETER, Boolean.TRUE.toString());
460+
return request;
461+
}
462+
417463
static Request indicesExist(GetIndexRequest getIndexRequest) {
418464
// this can be called with no indices as argument by transport client, not via REST though
419465
if (getIndexRequest.indices() == null || getIndexRequest.indices().length == 0) {
@@ -427,6 +473,7 @@ static Request indicesExist(GetIndexRequest getIndexRequest) {
427473
params.withHuman(getIndexRequest.humanReadable());
428474
params.withIndicesOptions(getIndexRequest.indicesOptions());
429475
params.withIncludeDefaults(getIndexRequest.includeDefaults());
476+
params.putParam(INCLUDE_TYPE_NAME_PARAMETER, Boolean.FALSE.toString());
430477
return request;
431478
}
432479

@@ -445,18 +492,18 @@ static Request indexPutSettings(UpdateSettingsRequest updateSettingsRequest) thr
445492
}
446493

447494
/**
448-
* @deprecated This uses the old form of PutIndexTemplateRequest which uses types.
495+
* @deprecated This uses the old form of PutIndexTemplateRequest which uses types.
449496
* Use (@link {@link #putTemplate(PutIndexTemplateRequest)} instead
450497
*/
451498
@Deprecated
452-
static Request putTemplate(org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest putIndexTemplateRequest)
499+
static Request putTemplate(org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest putIndexTemplateRequest)
453500
throws IOException {
454501
String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_template")
455502
.addPathPart(putIndexTemplateRequest.name()).build();
456503
Request request = new Request(HttpPut.METHOD_NAME, endpoint);
457504
RequestConverters.Params params = new RequestConverters.Params(request);
458505
params.withMasterTimeout(putIndexTemplateRequest.masterNodeTimeout());
459-
params.putParam(BaseRestHandler.INCLUDE_TYPE_NAME_PARAMETER, Boolean.TRUE.toString());
506+
params.putParam(INCLUDE_TYPE_NAME_PARAMETER, Boolean.TRUE.toString());
460507
if (putIndexTemplateRequest.create()) {
461508
params.putParam("create", Boolean.TRUE.toString());
462509
}
@@ -474,7 +521,7 @@ static Request putTemplate(PutIndexTemplateRequest putIndexTemplateRequest) thro
474521
RequestConverters.Params params = new RequestConverters.Params(request);
475522
params.withMasterTimeout(putIndexTemplateRequest.masterNodeTimeout());
476523
if (putIndexTemplateRequest.mappings() != null) {
477-
params.putParam(BaseRestHandler.INCLUDE_TYPE_NAME_PARAMETER, Boolean.FALSE.toString());
524+
params.putParam(INCLUDE_TYPE_NAME_PARAMETER, Boolean.FALSE.toString());
478525
}
479526
if (putIndexTemplateRequest.create()) {
480527
params.putParam("create", Boolean.TRUE.toString());
@@ -515,11 +562,11 @@ static Request getAlias(GetAliasesRequest getAliasesRequest) {
515562
static Request getTemplatesWithDocumentTypes(GetIndexTemplatesRequest getIndexTemplatesRequest) {
516563
return getTemplates(getIndexTemplatesRequest, true);
517564
}
518-
565+
519566
static Request getTemplates(GetIndexTemplatesRequest getIndexTemplatesRequest) {
520567
return getTemplates(getIndexTemplatesRequest, false);
521568
}
522-
569+
523570
private static Request getTemplates(GetIndexTemplatesRequest getIndexTemplatesRequest, boolean includeTypeName) {
524571
final String endpoint = new RequestConverters.EndpointBuilder()
525572
.addPathPartAsIs("_template")
@@ -529,9 +576,9 @@ private static Request getTemplates(GetIndexTemplatesRequest getIndexTemplatesRe
529576
final RequestConverters.Params params = new RequestConverters.Params(request);
530577
params.withLocal(getIndexTemplatesRequest.isLocal());
531578
params.withMasterTimeout(getIndexTemplatesRequest.getMasterNodeTimeout());
532-
params.putParam(BaseRestHandler.INCLUDE_TYPE_NAME_PARAMETER, Boolean.toString(includeTypeName));
579+
params.putParam(INCLUDE_TYPE_NAME_PARAMETER, Boolean.toString(includeTypeName));
533580
return request;
534-
}
581+
}
535582

536583
static Request templatesExist(IndexTemplatesExistRequest indexTemplatesExistRequest) {
537584
final String endpoint = new RequestConverters.EndpointBuilder()
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client.indices;
21+
22+
import org.elasticsearch.action.support.IndicesOptions;
23+
import org.elasticsearch.client.TimedRequest;
24+
import org.elasticsearch.common.util.ArrayUtils;
25+
26+
/**
27+
* A request to retrieve information about an index.
28+
*/
29+
public class GetIndexRequest extends TimedRequest {
30+
31+
public enum Feature {
32+
ALIASES,
33+
MAPPINGS,
34+
SETTINGS;
35+
}
36+
37+
static final Feature[] DEFAULT_FEATURES = new Feature[] { Feature.ALIASES, Feature.MAPPINGS, Feature.SETTINGS };
38+
private Feature[] features = DEFAULT_FEATURES;
39+
private boolean humanReadable = false;
40+
private transient boolean includeDefaults = false;
41+
42+
private final String[] indices;
43+
private IndicesOptions indicesOptions = IndicesOptions.fromOptions(false, false, true, true);
44+
private boolean local = false;
45+
46+
public GetIndexRequest(String... indices) {
47+
this.indices = indices;
48+
}
49+
50+
/**
51+
* The indices into which the mappings will be put.
52+
*/
53+
public String[] indices() {
54+
return indices;
55+
}
56+
57+
public IndicesOptions indicesOptions() {
58+
return indicesOptions;
59+
}
60+
61+
public GetIndexRequest indicesOptions(IndicesOptions indicesOptions) {
62+
this.indicesOptions = indicesOptions;
63+
return this;
64+
}
65+
66+
public final GetIndexRequest local(boolean local) {
67+
this.local = local;
68+
return this;
69+
}
70+
71+
/**
72+
* Return local information, do not retrieve the state from master node (default: false).
73+
* @return <code>true</code> if local information is to be returned;
74+
* <code>false</code> if information is to be retrieved from master node (default).
75+
*/
76+
public final boolean local() {
77+
return local;
78+
}
79+
80+
public GetIndexRequest features(Feature... features) {
81+
if (features == null) {
82+
throw new IllegalArgumentException("features cannot be null");
83+
} else {
84+
this.features = features;
85+
}
86+
return this;
87+
}
88+
89+
public GetIndexRequest addFeatures(Feature... features) {
90+
if (this.features == DEFAULT_FEATURES) {
91+
return features(features);
92+
} else {
93+
return features(ArrayUtils.concat(features(), features, Feature.class));
94+
}
95+
}
96+
97+
public Feature[] features() {
98+
return features;
99+
}
100+
101+
public GetIndexRequest humanReadable(boolean humanReadable) {
102+
this.humanReadable = humanReadable;
103+
return this;
104+
}
105+
106+
public boolean humanReadable() {
107+
return humanReadable;
108+
}
109+
110+
/**
111+
* Sets the value of "include_defaults".
112+
*
113+
* @param includeDefaults value of "include_defaults" to be set.
114+
* @return this request
115+
*/
116+
public GetIndexRequest includeDefaults(boolean includeDefaults) {
117+
this.includeDefaults = includeDefaults;
118+
return this;
119+
}
120+
121+
/**
122+
* Whether to return all default settings for each of the indices.
123+
*
124+
* @return <code>true</code> if defaults settings for each of the indices need to returned;
125+
* <code>false</code> otherwise.
126+
*/
127+
public boolean includeDefaults() {
128+
return includeDefaults;
129+
}
130+
131+
132+
}

0 commit comments

Comments
 (0)