本文整理匯總了Java中org.elasticsearch.common.xcontent.XContentFactory.xContent方法的典型用法代碼示例。如果您正苦於以下問題:Java XContentFactory.xContent方法的具體用法?Java XContentFactory.xContent怎麽用?Java XContentFactory.xContent使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.elasticsearch.common.xcontent.XContentFactory
的用法示例。
在下文中一共展示了XContentFactory.xContent方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: restContentBuilder
import org.elasticsearch.common.xcontent.XContentFactory; //導入方法依賴的package包/類
public static XContentBuilder restContentBuilder(RestRequest request)
throws IOException {
XContentType contentType = XContentType
.fromRestContentType(request.header("Content-Type"));
if (contentType == null) {
// try and guess it from the body, if exists
if (request.hasContent()) {
contentType = XContentFactory.xContentType(request.content());
}
}
if (contentType == null) {
// default to JSON
contentType = XContentType.JSON;
}
BytesStreamOutput out = new BytesStreamOutput();
XContentBuilder builder = new XContentBuilder(
XContentFactory.xContent(contentType), out);
if (request.paramAsBoolean("pretty", false)) {
builder.prettyPrint();
}
String casing = request.param("case");
if (casing != null && "camelCase".equals(casing)) {
builder.fieldCaseConversion(
XContentBuilder.FieldCaseConversion.CAMELCASE);
} else {
builder.fieldCaseConversion(
XContentBuilder.FieldCaseConversion.NONE);
}
return builder;
}
示例2: newBuilder
import org.elasticsearch.common.xcontent.XContentFactory; //導入方法依賴的package包/類
/**
* Creates a new {@link XContentBuilder} for a response to be sent using this channel. The builder's type is determined by the following
* logic. If the request has a format parameter that will be used to attempt to map to an {@link XContentType}. If there is no format
* parameter, the HTTP Accept header is checked to see if it can be matched to a {@link XContentType}. If this first attempt to map
* fails, the request content type will be used if the value is not {@code null}; if the value is {@code null} the output format falls
* back to JSON.
*/
@Override
public XContentBuilder newBuilder(@Nullable XContentType requestContentType, boolean useFiltering) throws IOException {
// try to determine the response content type from the media type or the format query string parameter, with the format parameter
// taking precedence over the Accept header
XContentType responseContentType = XContentType.fromMediaTypeOrFormat(format);
if (responseContentType == null) {
if (requestContentType != null) {
// if there was a parsed content-type for the incoming request use that since no format was specified using the query
// string parameter or the HTTP Accept header
responseContentType = requestContentType;
} else {
// default to JSON output when all else fails
responseContentType = XContentType.JSON;
}
}
Set<String> includes = Collections.emptySet();
Set<String> excludes = Collections.emptySet();
if (useFiltering) {
Set<String> filters = Strings.splitStringByCommaToSet(filterPath);
includes = filters.stream().filter(INCLUDE_FILTER).collect(toSet());
excludes = filters.stream().filter(EXCLUDE_FILTER).map(f -> f.substring(1)).collect(toSet());
}
XContentBuilder builder = new XContentBuilder(XContentFactory.xContent(responseContentType), bytesOutput(), includes, excludes);
if (pretty) {
builder.prettyPrint().lfAtEnd();
}
builder.humanReadable(human);
return builder;
}
示例3: assertBinary
import org.elasticsearch.common.xcontent.XContentFactory; //導入方法依賴的package包/類
protected void assertBinary(XContentBuilder expected, XContentBuilder builder) {
assertNotNull(builder);
assertNotNull(expected);
try {
XContent xContent = XContentFactory.xContent(builder.contentType());
XContentParser jsonParser = createParser(xContent, expected.bytes());
XContentParser testParser = createParser(xContent, builder.bytes());
while (true) {
XContentParser.Token token1 = jsonParser.nextToken();
XContentParser.Token token2 = testParser.nextToken();
if (token1 == null) {
assertThat(token2, nullValue());
return;
}
assertThat(token1, equalTo(token2));
switch (token1) {
case FIELD_NAME:
assertThat(jsonParser.currentName(), equalTo(testParser.currentName()));
break;
case VALUE_STRING:
assertThat(jsonParser.text(), equalTo(testParser.text()));
break;
case VALUE_NUMBER:
assertThat(jsonParser.numberType(), equalTo(testParser.numberType()));
assertThat(jsonParser.numberValue(), equalTo(testParser.numberValue()));
break;
}
}
} catch (Exception e) {
fail("Fail to verify the result of the XContentBuilder: " + e.getMessage());
}
}
示例4: newBuilder
import org.elasticsearch.common.xcontent.XContentFactory; //導入方法依賴的package包/類
public XContentBuilder newBuilder(@Nullable BytesReference autoDetectSource, boolean useFiltering) throws IOException {
XContentType contentType = XContentType.fromRestContentType(request.param("format", request.header("Content-Type")));
if (contentType == null) {
// try and guess it from the auto detect source
if (autoDetectSource != null) {
contentType = XContentFactory.xContentType(autoDetectSource);
}
}
if (contentType == null) {
// default to JSON
contentType = XContentType.JSON;
}
String[] filters = useFiltering ? request.paramAsStringArrayOrEmptyIfAll("filter_path") : null;
XContentBuilder builder = new XContentBuilder(XContentFactory.xContent(contentType), bytesOutput(), filters);
if (request.paramAsBoolean("pretty", false)) {
builder.prettyPrint().lfAtEnd();
}
builder.humanReadable(request.paramAsBoolean("human", builder.humanReadable()));
String casing = request.param("case");
if (casing != null) {
String msg = "Parameter 'case' has been deprecated, all responses will use underscore casing in the future";
DEPRECATION_LOGGER.deprecated(msg);
}
if (casing != null && "camelCase".equals(casing)) {
builder.fieldCaseConversion(XContentBuilder.FieldCaseConversion.CAMELCASE);
} else {
// we expect all REST interfaces to write results in underscore casing, so
// no need for double casing
builder.fieldCaseConversion(XContentBuilder.FieldCaseConversion.NONE);
}
return builder;
}