use of org.elasticsearch.common.xcontent.ToXContent in project elasticsearch by elastic.
the class Mapping method toString.
@Override
public String toString() {
try {
XContentBuilder builder = XContentFactory.jsonBuilder().startObject();
toXContent(builder, new ToXContent.MapParams(emptyMap()));
return builder.endObject().string();
} catch (IOException bogus) {
throw new UncheckedIOException(bogus);
}
}
use of org.elasticsearch.common.xcontent.ToXContent in project elasticsearch by elastic.
the class RestClusterAllocationExplainAction method prepareRequest.
@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
ClusterAllocationExplainRequest req;
if (request.hasContentOrSourceParam() == false) {
// Empty request signals "explain the first unassigned shard you find"
req = new ClusterAllocationExplainRequest();
} else {
try (XContentParser parser = request.contentOrSourceParamParser()) {
req = ClusterAllocationExplainRequest.parse(parser);
}
}
req.includeYesDecisions(request.paramAsBoolean("include_yes_decisions", false));
req.includeDiskInfo(request.paramAsBoolean("include_disk_info", false));
return channel -> client.admin().cluster().allocationExplain(req, new RestBuilderListener<ClusterAllocationExplainResponse>(channel) {
@Override
public RestResponse buildResponse(ClusterAllocationExplainResponse response, XContentBuilder builder) throws IOException {
response.getExplanation().toXContent(builder, ToXContent.EMPTY_PARAMS);
return new BytesRestResponse(RestStatus.OK, builder);
}
});
}
use of org.elasticsearch.common.xcontent.ToXContent in project elasticsearch by elastic.
the class GetIndexTemplatesResponse method toXContent.
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
params = new ToXContent.DelegatingMapParams(singletonMap("reduce_mappings", "true"), params);
builder.startObject();
for (IndexTemplateMetaData indexTemplateMetaData : getIndexTemplates()) {
IndexTemplateMetaData.Builder.toXContent(indexTemplateMetaData, builder, params);
}
builder.endObject();
return builder;
}
use of org.elasticsearch.common.xcontent.ToXContent in project elasticsearch by elastic.
the class SuggestTests method testFromXContent.
public void testFromXContent() throws IOException {
ToXContent.Params params = new ToXContent.MapParams(Collections.singletonMap(RestSearchAction.TYPED_KEYS_PARAM, "true"));
Suggest suggest = createTestItem();
XContentType xContentType = randomFrom(XContentType.values());
boolean humanReadable = randomBoolean();
BytesReference originalBytes = toXContent(suggest, xContentType, params, humanReadable);
Suggest parsed;
try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
ensureFieldName(parser, parser.nextToken(), Suggest.NAME);
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
parsed = Suggest.fromXContent(parser);
assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
assertNull(parser.nextToken());
}
assertEquals(suggest.size(), parsed.size());
for (Suggestion suggestion : suggest) {
Suggestion<? extends Entry<? extends Option>> parsedSuggestion = parsed.getSuggestion(suggestion.getName());
assertNotNull(parsedSuggestion);
assertEquals(suggestion.getClass(), parsedSuggestion.getClass());
}
assertToXContentEquivalent(originalBytes, toXContent(parsed, xContentType, params, humanReadable), xContentType);
}
use of org.elasticsearch.common.xcontent.ToXContent in project elasticsearch by elastic.
the class SuggestionTests method testFromXContent.
@SuppressWarnings({ "rawtypes" })
public void testFromXContent() throws IOException {
ToXContent.Params params = new ToXContent.MapParams(Collections.singletonMap(RestSearchAction.TYPED_KEYS_PARAM, "true"));
for (Class<Suggestion<? extends Entry<? extends Option>>> type : SUGGESTION_TYPES) {
Suggestion suggestion = createTestItem(type);
XContentType xContentType = randomFrom(XContentType.values());
boolean humanReadable = randomBoolean();
BytesReference originalBytes = toXContent(suggestion, xContentType, params, humanReadable);
Suggestion parsed;
try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.nextToken(), parser::getTokenLocation);
parsed = Suggestion.fromXContent(parser);
assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
assertNull(parser.nextToken());
}
assertEquals(suggestion.getName(), parsed.getName());
assertEquals(suggestion.getEntries().size(), parsed.getEntries().size());
// We don't parse size via xContent, instead we set it to -1 on the client side
assertEquals(-1, parsed.getSize());
assertToXContentEquivalent(originalBytes, toXContent(parsed, xContentType, params, humanReadable), xContentType);
}
}
Aggregations