當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。