当前位置: 首页>>代码示例>>Java>>正文


Java XContentBuilder.copyCurrentStructure方法代码示例

本文整理汇总了Java中org.elasticsearch.common.xcontent.XContentBuilder.copyCurrentStructure方法的典型用法代码示例。如果您正苦于以下问题:Java XContentBuilder.copyCurrentStructure方法的具体用法?Java XContentBuilder.copyCurrentStructure怎么用?Java XContentBuilder.copyCurrentStructure使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.elasticsearch.common.xcontent.XContentBuilder的用法示例。


在下文中一共展示了XContentBuilder.copyCurrentStructure方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: fromXContent

import org.elasticsearch.common.xcontent.XContentBuilder; //导入方法依赖的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;
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:45,代码来源:DecayFunctionParser.java

示例2: testSimpleGetFieldMappingsWithPretty

import org.elasticsearch.common.xcontent.XContentBuilder; //导入方法依赖的package包/类
public void testSimpleGetFieldMappingsWithPretty() throws Exception {
    assertAcked(prepareCreate("index").addMapping("type", getMappingForType("type")));
    Map<String, String> params = new HashMap<>();
    params.put("pretty", "true");
    GetFieldMappingsResponse response = client().admin().indices().prepareGetFieldMappings("index").setTypes("type").setFields("field1", "obj.subfield").get();
    XContentBuilder responseBuilder = XContentFactory.jsonBuilder().prettyPrint();
    responseBuilder.startObject();
    response.toXContent(responseBuilder, new ToXContent.MapParams(params));
    responseBuilder.endObject();
    String responseStrings = responseBuilder.string();


    XContentBuilder prettyJsonBuilder = XContentFactory.jsonBuilder().prettyPrint();
    prettyJsonBuilder.copyCurrentStructure(createParser(JsonXContent.jsonXContent, responseStrings));
    assertThat(responseStrings, equalTo(prettyJsonBuilder.string()));

    params.put("pretty", "false");

    response = client().admin().indices().prepareGetFieldMappings("index").setTypes("type").setFields("field1", "obj.subfield").get();
    responseBuilder = XContentFactory.jsonBuilder().prettyPrint().lfAtEnd();
    responseBuilder.startObject();
    response.toXContent(responseBuilder, new ToXContent.MapParams(params));
    responseBuilder.endObject();
    responseStrings = responseBuilder.string();

    prettyJsonBuilder = XContentFactory.jsonBuilder().prettyPrint();
    prettyJsonBuilder.copyCurrentStructure(createParser(JsonXContent.jsonXContent, responseStrings));
    assertThat(responseStrings, not(equalTo(prettyJsonBuilder.string())));

}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:31,代码来源:SimpleGetFieldMappingsIT.java

示例3: addComplexField

import org.elasticsearch.common.xcontent.XContentBuilder; //导入方法依赖的package包/类
public static void addComplexField(XContentBuilder builder, String fieldName,
    XContentType contentType, byte[] data) throws IOException {
  XContentParser parser = null;
  try {
    // Elasticsearch will accept JSON directly but we need to validate that
    // the incoming event is JSON first. Sadly, the elasticsearch JSON parser
    // is a stream parser so we need to instantiate it, parse the event to
    // validate it, then instantiate it again to provide the JSON to
    // elasticsearch.
    // If validation fails then the incoming event is submitted to
    // elasticsearch as plain text.
    parser = XContentFactory.xContent(contentType).createParser(data);
    while (parser.nextToken() != null) {};

    // If the JSON is valid then include it
    parser = XContentFactory.xContent(contentType).createParser(data);
    // Add the field name, but not the value.
    builder.field(fieldName);
    // This will add the whole parsed content as the value of the field.
    builder.copyCurrentStructure(parser);
  } catch (JsonParseException ex) {
    // If we get an exception here the most likely cause is nested JSON that
    // can't be figured out in the body. At this point just push it through
    // as is
    addSimpleField(builder, fieldName, data);
  } finally {
    if (parser != null) {
      parser.close();
    }
  }
}
 
开发者ID:moueimei,项目名称:flume-release-1.7.0,代码行数:32,代码来源:ContentBuilderUtil.java

示例4: parse

import org.elasticsearch.common.xcontent.XContentBuilder; //导入方法依赖的package包/类
/**
 * Parses bodies of the kind
 *
 * <pre>
 * <code>
 * {
 *      "fieldname1" : {
 *          "origin" = "someValue",
 *          "scale" = "someValue"
 *      }
 *
 * }
 * </code>
 * </pre>
 *
 * */
@Override
public ScoreFunction parse(QueryParseContext parseContext, XContentParser parser) throws IOException, QueryParsingException {
    String currentFieldName;
    XContentParser.Token token;
    AbstractDistanceScoreFunction scoreFunction;
    String multiValueMode = "MIN";
    XContentBuilder variableContent = XContentFactory.jsonBuilder();
    String fieldName = null;
    while ((token = parser.nextToken()) == XContentParser.Token.FIELD_NAME) {
        currentFieldName = parser.currentName();
        token = parser.nextToken();
        if (token == XContentParser.Token.START_OBJECT) {
            variableContent.copyCurrentStructure(parser);
            fieldName = currentFieldName;
        } else if (parseContext.parseFieldMatcher().match(currentFieldName, MULTI_VALUE_MODE)) {
            multiValueMode = parser.text();
        } else {
            throw new ElasticsearchParseException("malformed score function score parameters.");
        }
    }
    if (fieldName == null) {
        throw new ElasticsearchParseException("malformed score function score parameters.");
    }
    XContentParser variableParser = XContentFactory.xContent(variableContent.string()).createParser(variableContent.string());
    scoreFunction = parseVariable(fieldName, variableParser, parseContext, MultiValueMode.fromString(multiValueMode.toUpperCase(Locale.ROOT)));
    return scoreFunction;
}
 
开发者ID:baidu,项目名称:Elasticsearch,代码行数:44,代码来源:DecayFunctionParser.java

示例5: parsePercolatorDocument

import org.elasticsearch.common.xcontent.XContentBuilder; //导入方法依赖的package包/类
Query parsePercolatorDocument(String id, BytesReference source) {
    String type = null;
    BytesReference querySource = null;
    try (XContentParser sourceParser = XContentHelper.createParser(source)) {
        String currentFieldName = null;
        XContentParser.Token token = sourceParser.nextToken(); // move the START_OBJECT
        if (token != XContentParser.Token.START_OBJECT) {
            throw new ElasticsearchException("failed to parse query [" + id + "], not starting with OBJECT");
        }
        while ((token = sourceParser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (token == XContentParser.Token.FIELD_NAME) {
                currentFieldName = sourceParser.currentName();
            } else if (token == XContentParser.Token.START_OBJECT) {
                if ("query".equals(currentFieldName)) {
                    if (type != null) {
                        return parseQuery(type, sourceParser);
                    } else {
                        XContentBuilder builder = XContentFactory.contentBuilder(sourceParser.contentType());
                        builder.copyCurrentStructure(sourceParser);
                        querySource = builder.bytes();
                        builder.close();
                    }
                } else {
                    sourceParser.skipChildren();
                }
            } else if (token == XContentParser.Token.START_ARRAY) {
                sourceParser.skipChildren();
            } else if (token.isValue()) {
                if ("type".equals(currentFieldName)) {
                    type = sourceParser.text();
                }
            }
        }
        try (XContentParser queryParser = XContentHelper.createParser(querySource)) {
            return parseQuery(type, queryParser);
        }
    } catch (Exception e) {
        throw new PercolatorException(shardId().index(), "failed to parse query [" + id + "]", e);
    }
}
 
开发者ID:baidu,项目名称:Elasticsearch,代码行数:41,代码来源:PercolatorQueriesRegistry.java


注:本文中的org.elasticsearch.common.xcontent.XContentBuilder.copyCurrentStructure方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。