Java Code Examples for org.elasticsearch.common.xcontent.XContentBuilder#copyCurrentStructure()

The following examples show how to use org.elasticsearch.common.xcontent.XContentBuilder#copyCurrentStructure() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StoredFeature.java    From elasticsearch-learning-to-rank with Apache License 2.0 6 votes vote down vote up
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    builder.startObject();
    builder.field(NAME.getPreferredName(), name);
    builder.field(PARAMS.getPreferredName(), queryParams);
    builder.field(TEMPLATE_LANGUAGE.getPreferredName(), templateLanguage);
    if (templateAsString) {
        builder.field(TEMPLATE.getPreferredName(), template);
    } else {
        builder.field(TEMPLATE.getPreferredName());
        // it's ok to use NamedXContentRegistry.EMPTY because we don't really parse we copy the structure...
        XContentParser parser = XContentFactory.xContent(template).createParser(NamedXContentRegistry.EMPTY,
                LoggingDeprecationHandler.INSTANCE, template);
        builder.copyCurrentStructure(parser);
    }
    builder.endObject();
    return builder;
}
 
Example 2
Source File: StoredLtrModel.java    From elasticsearch-learning-to-rank with Apache License 2.0 6 votes vote down vote up
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    builder.startObject();
    builder.field(NAME.getPreferredName(), name);
    builder.field(FEATURE_SET.getPreferredName());
    featureSet.toXContent(builder, params);
    builder.startObject(MODEL.getPreferredName());
    builder.field(LtrModelDefinition.MODEL_TYPE.getPreferredName(), rankingModelType);
    builder.field(LtrModelDefinition.MODEL_DEFINITION.getPreferredName());
    if (modelAsString) {
        builder.value(rankingModel);
    } else {
        try (XContentParser parser = JsonXContent.jsonXContent.createParser(EMPTY,
                LoggingDeprecationHandler.INSTANCE, rankingModel)
        ) {
            builder.copyCurrentStructure(parser);
        }
    }
    builder.field(LtrModelDefinition.FEATURE_NORMALIZERS.getPreferredName());
    this.parsedFtrNorms.toXContent(builder, params);
    builder.endObject();
    builder.endObject();
    return builder;
}
 
Example 3
Source File: XmlFilter.java    From elasticsearch-xml with Apache License 2.0 6 votes vote down vote up
@Override
public BytesReference content() {
    if (isXml(request)) {
        XContentParser parser = null;
        try {
            BytesReference b = request.content();
            parser = XmlXContentFactory.xContent(XmlXContentType.XML).createParser(b);
            parser.nextToken();
            XContentBuilder builder = XContentFactory.jsonBuilder();
            builder.copyCurrentStructure(parser);
            return builder.bytes();
        } catch (Throwable e) {
            logger.error(e.getMessage(), e);
        } finally {
            if (parser != null) {
                parser.close();
            }
        }
    }
    return request.content();
}
 
Example 4
Source File: ContentBuilderUtil.java    From mt-flume with Apache License 2.0 6 votes vote down vote up
public static void addComplexField(XContentBuilder builder, String fieldName,
    XContentType contentType, byte[] data) throws IOException {
  XContentParser parser = null;
  try {
    XContentBuilder tmp = jsonBuilder();
    parser = XContentFactory.xContent(contentType).createParser(data);
    parser.nextToken();
    tmp.copyCurrentStructure(parser);
    builder.field(fieldName, tmp);
  } 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, we have already added the field so don't do it again
    addSimpleField(builder, fieldName, data);
  } finally {
    if (parser != null) {
      parser.close();
    }
  }
}
 
Example 5
Source File: DecayFunctionParser.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 6
Source File: PercolatorQueriesRegistry.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 7
Source File: ElasticsearchResource.java    From newsleak with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Creates a new elasticsearch index and adds a mapping for the document type.
 * Previously existing indexes will be removed.
 *
 * @throws Exception
 *             the exception
 */
private void createIndex() throws Exception {

	boolean exists = client.admin().indices().prepareExists(mIndex).execute().actionGet().isExists();

	// remove preexisting index
	if (exists) {
		logger.log(Level.INFO, "Preexisting index " + mIndex + " will be removed.");
		DeleteIndexResponse deleteResponse = client.admin().indices().delete(new DeleteIndexRequest(mIndex))
				.actionGet();
		if (deleteResponse.isAcknowledged()) {
			logger.log(Level.INFO, "Preexisting index " + mIndex + " successfully removed.");
			exists = false;
		}
	}

	// create schema mapping from file
	logger.log(Level.INFO, "Index " + mIndex + " will be created.");
	String docMapping = new String(Files.readAllBytes(Paths.get(documentMappingFile)));

	XContentBuilder builder = XContentFactory.jsonBuilder();
	XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(docMapping.getBytes());
	parser.close();
	builder.copyCurrentStructure(parser);

	CreateIndexRequestBuilder createIndexRequestBuilder = client.admin().indices().prepareCreate(mIndex);
	createIndexRequestBuilder.addMapping(DOCUMENT_TYPE, builder);
	createIndexRequestBuilder.execute().actionGet();

}
 
Example 8
Source File: ElasticsearchIntegrationTest.java    From openshift-elasticsearch-plugin with Apache License 2.0 5 votes vote down vote up
protected static BytesReference readXContent(final Reader reader, final XContentType contentType) throws IOException {
    XContentParser parser = null;
    try {
        parser = XContentFactory.xContent(contentType).createParser(NamedXContentRegistry.EMPTY, reader);
        parser.nextToken();
        final XContentBuilder builder = XContentFactory.jsonBuilder();
        builder.copyCurrentStructure(parser);
        return builder.bytes();
    } finally {
        if (parser != null) {
            parser.close();
        }
    }
}
 
Example 9
Source File: ContentBuilderUtil.java    From ElasticsearchSink2 with Apache License 2.0 5 votes vote down vote up
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();
    }
  }
}
 
Example 10
Source File: ContentBuilderUtil.java    From ingestion with Apache License 2.0 5 votes vote down vote up
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();
    }
  }
}