Skip to content

Commit 5c760d1

Browse files
committed
don't case the index name in cluster health, use string constants in xcontent generation
1 parent 3a52c2f commit 5c760d1

File tree

2 files changed

+70
-29
lines changed

2 files changed

+70
-29
lines changed

modules/elasticsearch/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ public XContentBuilder startObject(String name) throws IOException {
103103
return this;
104104
}
105105

106+
public XContentBuilder startObject(String name, FieldCaseConversion conversion) throws IOException {
107+
field(name, conversion);
108+
startObject();
109+
return this;
110+
}
111+
106112
public XContentBuilder startObject(XContentBuilderString name) throws IOException {
107113
field(name);
108114
startObject();
@@ -204,6 +210,22 @@ public XContentBuilder field(String name) throws IOException {
204210
return this;
205211
}
206212

213+
public XContentBuilder field(String name, FieldCaseConversion conversion) throws IOException {
214+
if (conversion == FieldCaseConversion.UNDERSCORE) {
215+
if (cachedStringBuilder == null) {
216+
cachedStringBuilder = new StringBuilder();
217+
}
218+
name = Strings.toUnderscoreCase(name, cachedStringBuilder);
219+
} else if (conversion == FieldCaseConversion.CAMELCASE) {
220+
if (cachedStringBuilder == null) {
221+
cachedStringBuilder = new StringBuilder();
222+
}
223+
name = Strings.toCamelCase(name, cachedStringBuilder);
224+
}
225+
generator.writeFieldName(name);
226+
return this;
227+
}
228+
207229
public XContentBuilder field(String name, char[] value, int offset, int length) throws IOException {
208230
field(name);
209231
if (value == null) {

modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/admin/cluster/health/RestClusterHealthAction.java

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.elasticsearch.common.inject.Inject;
2626
import org.elasticsearch.common.settings.Settings;
2727
import org.elasticsearch.common.xcontent.XContentBuilder;
28+
import org.elasticsearch.common.xcontent.XContentBuilderString;
2829
import org.elasticsearch.rest.*;
2930
import org.elasticsearch.rest.action.support.RestActions;
3031
import org.elasticsearch.rest.action.support.RestXContentBuilder;
@@ -84,18 +85,18 @@ public class RestClusterHealthAction extends BaseRestHandler {
8485
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
8586
builder.startObject();
8687

87-
builder.field("cluster_name", response.clusterName());
88-
builder.field("status", response.status().name().toLowerCase());
89-
builder.field("timed_out", response.timedOut());
90-
builder.field("number_of_nodes", response.numberOfNodes());
91-
builder.field("active_primary_shards", response.activePrimaryShards());
92-
builder.field("active_shards", response.activeShards());
93-
builder.field("relocating_shards", response.relocatingShards());
94-
builder.field("initializing_shards", response.initializingShards());
95-
builder.field("unassigned_shards", response.unassignedShards());
88+
builder.field(Fields.CLUSTER_NAME, response.clusterName());
89+
builder.field(Fields.STATUS, response.status().name().toLowerCase());
90+
builder.field(Fields.TIMED_OUT, response.timedOut());
91+
builder.field(Fields.NUMBER_OF_NODES, response.numberOfNodes());
92+
builder.field(Fields.ACTIVE_PRIMARY_SHARDS, response.activePrimaryShards());
93+
builder.field(Fields.ACTIVE_SHARDS, response.activeShards());
94+
builder.field(Fields.RELOCATING_SHARDS, response.relocatingShards());
95+
builder.field(Fields.INITIALIZING_SHARDS, response.initializingShards());
96+
builder.field(Fields.UNASSIGNED_SHARDS, response.unassignedShards());
9697

9798
if (!response.validationFailures().isEmpty()) {
98-
builder.startArray("validation_failures");
99+
builder.startArray(Fields.VALIDATION_FAILURES);
99100
for (String validationFailure : response.validationFailures()) {
100101
builder.value(validationFailure);
101102
}
@@ -106,7 +107,7 @@ public class RestClusterHealthAction extends BaseRestHandler {
106107
builder.startObject(indexHealth.index());
107108

108109
if (!indexHealth.validationFailures().isEmpty()) {
109-
builder.startArray("validation_failures");
110+
builder.startArray(Fields.VALIDATION_FAILURES);
110111
for (String validationFailure : indexHealth.validationFailures()) {
111112
builder.value(validationFailure);
112113
}
@@ -120,39 +121,39 @@ public class RestClusterHealthAction extends BaseRestHandler {
120121
}
121122

122123
if (fLevel > 0) {
123-
builder.startObject("indices");
124+
builder.startObject(Fields.INDICES);
124125
for (ClusterIndexHealth indexHealth : response) {
125-
builder.startObject(indexHealth.index());
126+
builder.startObject(indexHealth.index(), XContentBuilder.FieldCaseConversion.NONE);
126127

127-
builder.field("status", indexHealth.status().name().toLowerCase());
128-
builder.field("number_of_shards", indexHealth.numberOfShards());
129-
builder.field("number_of_replicas", indexHealth.numberOfReplicas());
130-
builder.field("active_primary_shards", indexHealth.activePrimaryShards());
131-
builder.field("active_shards", indexHealth.activeShards());
132-
builder.field("relocating_shards", indexHealth.relocatingShards());
133-
builder.field("initializing_shards", indexHealth.initializingShards());
134-
builder.field("unassigned_shards", indexHealth.unassignedShards());
128+
builder.field(Fields.STATUS, indexHealth.status().name().toLowerCase());
129+
builder.field(Fields.NUMBER_OF_SHARDS, indexHealth.numberOfShards());
130+
builder.field(Fields.NUMBER_OF_REPLICAS, indexHealth.numberOfReplicas());
131+
builder.field(Fields.ACTIVE_PRIMARY_SHARDS, indexHealth.activePrimaryShards());
132+
builder.field(Fields.ACTIVE_SHARDS, indexHealth.activeShards());
133+
builder.field(Fields.RELOCATING_SHARDS, indexHealth.relocatingShards());
134+
builder.field(Fields.INITIALIZING_SHARDS, indexHealth.initializingShards());
135+
builder.field(Fields.UNASSIGNED_SHARDS, indexHealth.unassignedShards());
135136

136137
if (!indexHealth.validationFailures().isEmpty()) {
137-
builder.startArray("validation_failures");
138+
builder.startArray(Fields.VALIDATION_FAILURES);
138139
for (String validationFailure : indexHealth.validationFailures()) {
139140
builder.value(validationFailure);
140141
}
141142
builder.endArray();
142143
}
143144

144145
if (fLevel > 1) {
145-
builder.startObject("shards");
146+
builder.startObject(Fields.SHARDS);
146147

147148
for (ClusterShardHealth shardHealth : indexHealth) {
148149
builder.startObject(Integer.toString(shardHealth.id()));
149150

150-
builder.field("status", shardHealth.status().name().toLowerCase());
151-
builder.field("primary_active", shardHealth.primaryActive());
152-
builder.field("active_shards", shardHealth.activeShards());
153-
builder.field("relocating_shards", shardHealth.relocatingShards());
154-
builder.field("initializing_shards", shardHealth.initializingShards());
155-
builder.field("unassigned_shards", shardHealth.unassignedShards());
151+
builder.field(Fields.STATUS, shardHealth.status().name().toLowerCase());
152+
builder.field(Fields.PRIMARY_ACTIVE, shardHealth.primaryActive());
153+
builder.field(Fields.ACTIVE_SHARDS, shardHealth.activeShards());
154+
builder.field(Fields.RELOCATING_SHARDS, shardHealth.relocatingShards());
155+
builder.field(Fields.INITIALIZING_SHARDS, shardHealth.initializingShards());
156+
builder.field(Fields.UNASSIGNED_SHARDS, shardHealth.unassignedShards());
156157

157158
builder.endObject();
158159
}
@@ -182,4 +183,22 @@ public class RestClusterHealthAction extends BaseRestHandler {
182183
}
183184
});
184185
}
186+
187+
static final class Fields {
188+
static final XContentBuilderString CLUSTER_NAME = new XContentBuilderString("cluster_name");
189+
static final XContentBuilderString STATUS = new XContentBuilderString("status");
190+
static final XContentBuilderString TIMED_OUT = new XContentBuilderString("timed_out");
191+
static final XContentBuilderString NUMBER_OF_SHARDS = new XContentBuilderString("number_of_shards");
192+
static final XContentBuilderString NUMBER_OF_REPLICAS = new XContentBuilderString("number_of_replicas");
193+
static final XContentBuilderString NUMBER_OF_NODES = new XContentBuilderString("number_of_nodes");
194+
static final XContentBuilderString ACTIVE_PRIMARY_SHARDS = new XContentBuilderString("active_primary_shards");
195+
static final XContentBuilderString ACTIVE_SHARDS = new XContentBuilderString("active_shards");
196+
static final XContentBuilderString RELOCATING_SHARDS = new XContentBuilderString("relocating_shards");
197+
static final XContentBuilderString INITIALIZING_SHARDS = new XContentBuilderString("initializing_shards");
198+
static final XContentBuilderString UNASSIGNED_SHARDS = new XContentBuilderString("unassigned_shards");
199+
static final XContentBuilderString VALIDATION_FAILURES = new XContentBuilderString("validation_failures");
200+
static final XContentBuilderString INDICES = new XContentBuilderString("indices");
201+
static final XContentBuilderString SHARDS = new XContentBuilderString("shards");
202+
static final XContentBuilderString PRIMARY_ACTIVE = new XContentBuilderString("primary_active");
203+
}
185204
}

0 commit comments

Comments
 (0)