本文整理汇总了Java中org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder方法的典型用法代码示例。如果您正苦于以下问题:Java XContentFactory.jsonBuilder方法的具体用法?Java XContentFactory.jsonBuilder怎么用?Java XContentFactory.jsonBuilder使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.elasticsearch.common.xcontent.XContentFactory
的用法示例。
在下文中一共展示了XContentFactory.jsonBuilder方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getBodyAsString
import org.elasticsearch.common.xcontent.XContentFactory; //导入方法依赖的package包/类
/**
* Returns the body as a string
*/
public String getBodyAsString() {
if (bodyAsString == null && body != null) {
//content-type null means that text was returned
if (bodyContentType == null || bodyContentType == XContentType.JSON || bodyContentType == XContentType.YAML) {
bodyAsString = new String(body, StandardCharsets.UTF_8);
} else {
//if the body is in a binary format and gets requested as a string (e.g. to log a test failure), we convert it to json
try (XContentBuilder jsonBuilder = XContentFactory.jsonBuilder()) {
try (XContentParser parser = bodyContentType.xContent().createParser(NamedXContentRegistry.EMPTY, body)) {
jsonBuilder.copyCurrentStructure(parser);
}
bodyAsString = jsonBuilder.string();
} catch (IOException e) {
throw new UncheckedIOException("unable to convert response body to a string format", e);
}
}
}
return bodyAsString;
}
示例2: setInline
import org.elasticsearch.common.xcontent.XContentFactory; //导入方法依赖的package包/类
/**
* Since inline scripts can accept code rather than just an id, they must also be able
* to handle template parsing, hence the need for custom parsing code. Templates can
* consist of either an {@link String} or a JSON object. If a JSON object is discovered
* then the content type option must also be saved as a compiler option.
*/
private void setInline(XContentParser parser) {
try {
if (type != null) {
throwOnlyOneOfType();
}
type = ScriptType.INLINE;
if (parser.currentToken() == Token.START_OBJECT) {
//this is really for search templates, that need to be converted to json format
XContentBuilder builder = XContentFactory.jsonBuilder();
idOrCode = builder.copyCurrentStructure(parser).string();
options.put(CONTENT_TYPE_OPTION, XContentType.JSON.mediaType());
} else {
idOrCode = parser.text();
}
} catch (IOException exception) {
throw new UncheckedIOException(exception);
}
}
示例3: testParseDist
import org.elasticsearch.common.xcontent.XContentFactory; //导入方法依赖的package包/类
/**
* test correct parsing of `dist` parameter, this should create builder with pre/post set to same value
*/
public void testParseDist() throws IOException {
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
builder.startObject(SpanNotQueryBuilder.NAME);
builder.field("exclude");
spanTermQuery("description", "jumped").toXContent(builder, null);
builder.field("include");
spanNearQuery(QueryBuilders.spanTermQuery("description", "quick"), 1)
.addClause(QueryBuilders.spanTermQuery("description", "fox")).toXContent(builder, null);
builder.field("dist", 3);
builder.endObject();
builder.endObject();
SpanNotQueryBuilder query = (SpanNotQueryBuilder)parseQuery(builder.string());
assertThat(query.pre(), equalTo(3));
assertThat(query.post(), equalTo(3));
assertNotNull(query.includeQuery());
assertNotNull(query.excludeQuery());
}
示例4: createScript
import org.elasticsearch.common.xcontent.XContentFactory; //导入方法依赖的package包/类
private Script createScript() throws IOException {
final Map<String, Object> params = randomBoolean() ? Collections.emptyMap() : Collections.singletonMap("key", "value");
ScriptType scriptType = randomFrom(ScriptType.values());
String script;
if (scriptType == ScriptType.INLINE) {
try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
builder.startObject();
builder.field("field", randomAsciiOfLengthBetween(1, 5));
builder.endObject();
script = builder.string();
}
} else {
script = randomAsciiOfLengthBetween(1, 5);
}
return new Script(
scriptType,
scriptType == ScriptType.STORED ? null : randomFrom("_lang1", "_lang2", "_lang3"),
script,
scriptType == ScriptType.INLINE ?
Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType()) : null, params
);
}
示例5: encodeSettings
import org.elasticsearch.common.xcontent.XContentFactory; //导入方法依赖的package包/类
public static BytesReference encodeSettings(Settings settings) throws IOException {
BytesStreamOutput bso = new BytesStreamOutput();
XContentBuilder builder = XContentFactory.jsonBuilder(bso);
builder.startObject();
for (Map.Entry<String, String> entry : settings.getAsMap().entrySet()) {
builder.field(entry.getKey(), entry.getValue());
}
builder.endObject();
builder.flush();
return bso.bytes();
}
示例6: mapping
import org.elasticsearch.common.xcontent.XContentFactory; //导入方法依赖的package包/类
/**
* Gets the mappings for the given schema and table as a formatted json string.
*/
public String mapping(String schema, String table) throws IOException {
if(USE_EXTERNAL_ES5){
return "";
}
GetMappingsResponse response = getElasticInternalClient().admin().indices().prepareGetMappings(schema).setTypes(table).execute().actionGet();
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.prettyPrint().lfAtEnd();
builder.startObject();
ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappingsByIndex = response.getMappings();
if (mappingsByIndex.isEmpty()) {
return "";
}
for (ObjectObjectCursor<String, ImmutableOpenMap<String, MappingMetaData>> indexEntry : mappingsByIndex) {
if (indexEntry.value.isEmpty()) {
continue;
}
builder.startObject(indexEntry.key, XContentBuilder.FieldCaseConversion.NONE);
builder.startObject(new XContentBuilderString("mappings"));
for (ObjectObjectCursor<String, MappingMetaData> typeEntry : indexEntry.value) {
builder.field(typeEntry.key);
builder.map(typeEntry.value.sourceAsMap());
}
builder.endObject();
builder.endObject();
}
builder.endObject();
return builder.string();
}
示例7: DocWriter
import org.elasticsearch.common.xcontent.XContentFactory; //导入方法依赖的package包/类
public DocWriter(OutputStream outputStream,
Set<CollectExpression<Row, ?>> collectExpressions,
Map<String, Object> overwrites) throws IOException {
this.outputStream = outputStream;
this.collectExpressions = collectExpressions;
this.overwrites = overwrites;
builder = XContentFactory.jsonBuilder(outputStream);
}
示例8: filter
import org.elasticsearch.common.xcontent.XContentFactory; //导入方法依赖的package包/类
public AliasAction filter(QueryBuilder queryBuilder) {
if (queryBuilder == null) {
this.filter = null;
return this;
}
try {
XContentBuilder builder = XContentFactory.jsonBuilder();
queryBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.close();
this.filter = builder.string();
return this;
} catch (IOException e) {
throw new ElasticsearchGenerationException("Failed to build json for alias request", e);
}
}
示例9: toString
import org.elasticsearch.common.xcontent.XContentFactory; //导入方法依赖的package包/类
@Override
public String toString() {
try {
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.prettyPrint();
toXContent(builder, EMPTY_PARAMS);
return builder.string();
} catch (Exception e) {
throw new ElasticsearchException("Failed to build xcontent.", e);
}
}
示例10: fromXContent
import org.elasticsearch.common.xcontent.XContentFactory; //导入方法依赖的package包/类
/**
* Parses bodies of the kind
*
* <pre>
* <code>
* {
* "fieldname1" : {
* "origin" : "someValue",
* "scale" : "someValue"
* },
* "multi_value_mode" : "min"
* }
* </code>
* </pre>
*/
@Override
public DFB fromXContent(QueryParseContext context) throws IOException, ParsingException {
XContentParser parser = context.parser();
String currentFieldName;
XContentParser.Token token;
MultiValueMode multiValueMode = DecayFunctionBuilder.DEFAULT_MULTI_VALUE_MODE;
String fieldName = null;
BytesReference functionBytes = null;
while ((token = parser.nextToken()) == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
token = parser.nextToken();
if (token == XContentParser.Token.START_OBJECT) {
fieldName = currentFieldName;
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.copyCurrentStructure(parser);
functionBytes = builder.bytes();
} else if (MULTI_VALUE_MODE.match(currentFieldName)) {
multiValueMode = MultiValueMode.fromString(parser.text());
} else {
throw new ParsingException(parser.getTokenLocation(), "malformed score function score parameters.");
}
}
if (fieldName == null || functionBytes == null) {
throw new ParsingException(parser.getTokenLocation(), "malformed score function score parameters.");
}
DFB functionBuilder = createFromBytes.apply(fieldName, functionBytes);
functionBuilder.setMultiValueMode(multiValueMode);
return functionBuilder;
}
示例11: DecayFunctionBuilder
import org.elasticsearch.common.xcontent.XContentFactory; //导入方法依赖的package包/类
/**
* Convenience constructor that converts its parameters into json to parse on the data nodes.
*/
protected DecayFunctionBuilder(String fieldName, Object origin, Object scale, Object offset, double decay) {
if (fieldName == null) {
throw new IllegalArgumentException("decay function: field name must not be null");
}
if (scale == null) {
throw new IllegalArgumentException("decay function: scale must not be null");
}
if (decay <= 0 || decay >= 1.0) {
throw new IllegalStateException("decay function: decay must be in range 0..1!");
}
this.fieldName = fieldName;
try {
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
if (origin != null) {
builder.field(ORIGIN, origin);
}
builder.field(SCALE, scale);
if (offset != null) {
builder.field(OFFSET, offset);
}
builder.field(DECAY, decay);
builder.endObject();
this.functionBytes = builder.bytes();
} catch (IOException e) {
throw new IllegalArgumentException("unable to build inner function object",e);
}
}
示例12: toString
import org.elasticsearch.common.xcontent.XContentFactory; //导入方法依赖的package包/类
@Override
public final String toString() {
try {
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.prettyPrint();
toXContent(builder, EMPTY_PARAMS);
return builder.string();
} catch (Exception e) {
return "{ \"error\" : \"" + ExceptionsHelper.detailedMessage(e) + "\"}";
}
}
示例13: aliases
import org.elasticsearch.common.xcontent.XContentFactory; //导入方法依赖的package包/类
/**
* Sets the aliases that will be associated with the index when it gets created
*/
@SuppressWarnings("unchecked")
public CreateIndexRequest aliases(Map source) {
try {
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.map(source);
return aliases(builder.bytes());
} catch (IOException e) {
throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e);
}
}
示例14: parseFromBuilder
import org.elasticsearch.common.xcontent.XContentFactory; //导入方法依赖的package包/类
protected SignificanceHeuristic parseFromBuilder(ParseFieldRegistry<SignificanceHeuristicParser> significanceHeuristicParserRegistry,
SignificanceHeuristic significanceHeuristic) throws IOException {
SignificantTermsAggregationBuilder stBuilder = significantTerms("testagg");
stBuilder.significanceHeuristic(significanceHeuristic).field("text").minDocCount(200);
XContentBuilder stXContentBuilder = XContentFactory.jsonBuilder();
stBuilder.internalXContent(stXContentBuilder, null);
XContentParser stParser = createParser(JsonXContent.jsonXContent, stXContentBuilder.string());
return parseSignificanceHeuristic(significanceHeuristicParserRegistry, stParser);
}
示例15: toString
import org.elasticsearch.common.xcontent.XContentFactory; //导入方法依赖的package包/类
public final String toString(Params params) {
try {
XContentBuilder builder = XContentFactory.jsonBuilder();
if (params.paramAsBoolean("pretty", true)) {
builder.prettyPrint();
}
toXContent(builder, params);
return builder.string();
} catch (Exception e) {
// So we have a stack trace logged somewhere
return "{ \"error\" : \"" + ExceptionsHelper.detailedMessage(e) + "\"}";
}
}