use of org.elasticsearch.common.xcontent.XContentBuilder in project elasticsearch by elastic.
the class RestGetSettingsAction method prepareRequest.
@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
final String[] names = request.paramAsStringArrayOrEmptyIfAll("name");
final boolean renderDefaults = request.paramAsBoolean("include_defaults", false);
GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices(Strings.splitStringByCommaToArray(request.param("index"))).indicesOptions(IndicesOptions.fromRequest(request, IndicesOptions.strictExpandOpen())).humanReadable(request.hasParam("human")).names(names);
getSettingsRequest.local(request.paramAsBoolean("local", getSettingsRequest.local()));
return channel -> client.admin().indices().getSettings(getSettingsRequest, new RestBuilderListener<GetSettingsResponse>(channel) {
@Override
public RestResponse buildResponse(GetSettingsResponse getSettingsResponse, XContentBuilder builder) throws Exception {
builder.startObject();
for (ObjectObjectCursor<String, Settings> cursor : getSettingsResponse.getIndexToSettings()) {
if (cursor.value.isEmpty()) {
continue;
}
builder.startObject(cursor.key);
builder.startObject("settings");
cursor.value.toXContent(builder, request);
builder.endObject();
if (renderDefaults) {
builder.startObject("defaults");
settingsFilter.filter(indexScopedSettings.diff(cursor.value, settings)).toXContent(builder, request);
builder.endObject();
}
builder.endObject();
}
builder.endObject();
return new BytesRestResponse(OK, builder);
}
});
}
use of org.elasticsearch.common.xcontent.XContentBuilder in project elasticsearch by elastic.
the class RestTable method buildXContentBuilder.
public static RestResponse buildXContentBuilder(Table table, RestChannel channel) throws Exception {
RestRequest request = channel.request();
XContentBuilder builder = channel.newBuilder();
List<DisplayHeader> displayHeaders = buildDisplayHeaders(table, request);
builder.startArray();
List<Integer> rowOrder = getRowOrder(table, request);
for (Integer row : rowOrder) {
builder.startObject();
for (DisplayHeader header : displayHeaders) {
builder.field(header.display, renderValue(request, table.getAsMap().get(header.name).get(row).value));
}
builder.endObject();
}
builder.endArray();
return new BytesRestResponse(RestStatus.OK, builder);
}
use of org.elasticsearch.common.xcontent.XContentBuilder in project elasticsearch by elastic.
the class Suggest method toString.
@Override
public String toString() {
try {
XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint();
builder.startObject();
toXContent(builder, EMPTY_PARAMS);
builder.endObject();
return builder.string();
} catch (IOException e) {
return "{ \"error\" : \"" + e.getMessage() + "\"}";
}
}
use of org.elasticsearch.common.xcontent.XContentBuilder in project elasticsearch by elastic.
the class DirectCandidateGeneratorBuilder method toString.
@Override
public String toString() {
try {
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.prettyPrint();
toXContent(builder, EMPTY_PARAMS);
return builder.string();
} catch (Exception e) {
return "{ \"error\" : \"" + ExceptionsHelper.detailedMessage(e) + "\"}";
}
}
use of org.elasticsearch.common.xcontent.XContentBuilder in project elasticsearch by elastic.
the class IndexMetaDataTests method testIndexMetaDataSerialization.
public void testIndexMetaDataSerialization() throws IOException {
Integer numShard = randomFrom(1, 2, 4, 8, 16);
int numberOfReplicas = randomIntBetween(0, 10);
IndexMetaData metaData = IndexMetaData.builder("foo").settings(Settings.builder().put("index.version.created", 1).put("index.number_of_shards", numShard).put("index.number_of_replicas", numberOfReplicas).build()).creationDate(randomLong()).primaryTerm(0, 2).setRoutingNumShards(32).build();
final XContentBuilder builder = JsonXContent.contentBuilder();
builder.startObject();
metaData.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();
XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes());
final IndexMetaData fromXContentMeta = IndexMetaData.fromXContent(parser);
assertEquals(metaData, fromXContentMeta);
assertEquals(metaData.hashCode(), fromXContentMeta.hashCode());
assertEquals(metaData.getNumberOfReplicas(), fromXContentMeta.getNumberOfReplicas());
assertEquals(metaData.getNumberOfShards(), fromXContentMeta.getNumberOfShards());
assertEquals(metaData.getCreationVersion(), fromXContentMeta.getCreationVersion());
assertEquals(metaData.getRoutingNumShards(), fromXContentMeta.getRoutingNumShards());
assertEquals(metaData.getCreationDate(), fromXContentMeta.getCreationDate());
assertEquals(metaData.getRoutingFactor(), fromXContentMeta.getRoutingFactor());
assertEquals(metaData.primaryTerm(0), fromXContentMeta.primaryTerm(0));
final BytesStreamOutput out = new BytesStreamOutput();
metaData.writeTo(out);
IndexMetaData deserialized = IndexMetaData.readFrom(out.bytes().streamInput());
assertEquals(metaData, deserialized);
assertEquals(metaData.hashCode(), deserialized.hashCode());
assertEquals(metaData.getNumberOfReplicas(), deserialized.getNumberOfReplicas());
assertEquals(metaData.getNumberOfShards(), deserialized.getNumberOfShards());
assertEquals(metaData.getCreationVersion(), deserialized.getCreationVersion());
assertEquals(metaData.getRoutingNumShards(), deserialized.getRoutingNumShards());
assertEquals(metaData.getCreationDate(), deserialized.getCreationDate());
assertEquals(metaData.getRoutingFactor(), deserialized.getRoutingFactor());
assertEquals(metaData.primaryTerm(0), deserialized.primaryTerm(0));
}
Aggregations