Java Code Examples for org.elasticsearch.common.xcontent.XContentGenerator#writeEndObject()

The following examples show how to use org.elasticsearch.common.xcontent.XContentGenerator#writeEndObject() . 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: DocumentContentBuilder.java    From molgenis with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Create Elasticsearch document source content from entity
 *
 * @param entity the entity to convert to document source content
 * @return Elasticsearch document source content
 */
Document createDocument(Entity entity) {
  int maxIndexingDepth = entity.getEntityType().getIndexingDepth();
  XContentBuilder contentBuilder;
  try {
    contentBuilder = XContentFactory.contentBuilder(JSON);
    XContentGenerator generator = contentBuilder.generator();
    generator.writeStartObject();
    createRec(entity, generator, 0, maxIndexingDepth);
    generator.writeEndObject();
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
  String documentId = toElasticsearchId(entity.getIdValue());
  return Document.create(documentId, contentBuilder);
}
 
Example 2
Source File: XmlXContentGenerator.java    From elasticsearch-xml with Apache License 2.0 5 votes vote down vote up
public static void copyCurrentStructure(XContentGenerator generator, XContentParser parser) throws IOException {
    XContentParser.Token t = parser.currentToken();

    // Let's handle field-name separately first
    if (t == XContentParser.Token.FIELD_NAME) {
        generator.writeFieldName(parser.currentName());
        t = parser.nextToken();
        // fall-through to copy the associated value
    }

    switch (t) {
        case START_ARRAY:
            generator.writeStartArray();
            while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
                copyCurrentStructure(generator, parser);
            }
            generator.writeEndArray();
            break;
        case START_OBJECT:
            generator.writeStartObject();
            while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
                copyCurrentStructure(generator, parser);
            }
            generator.writeEndObject();
            break;
        default: // others are simple:
            copyCurrentEvent(generator, parser);
    }
}
 
Example 3
Source File: DocumentContentBuilder.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void createRecReferenceAttribute(
    XContentGenerator generator, int depth, int maxDepth, Entity xrefEntity) throws IOException {
  if (depth < maxDepth) {
    generator.writeStartObject();
    createRec(xrefEntity, generator, depth + 1, maxDepth);
    generator.writeEndObject();
  } else {
    Attribute xrefIdAttr = xrefEntity.getEntityType().getLabelAttribute();
    createRec(xrefEntity, xrefIdAttr, generator, depth + 1, maxDepth);
  }
}
 
Example 4
Source File: JsonXContentGenerator.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Low level implementation detail of {@link XContentGenerator#copyCurrentStructure(XContentParser)}.
 */
private static void copyCurrentStructure(XContentGenerator destination, XContentParser parser) throws IOException {
    XContentParser.Token token = parser.currentToken();

    // Let's handle field-name separately first
    if (token == XContentParser.Token.FIELD_NAME) {
        destination.writeFieldName(parser.currentName());
        token = parser.nextToken();
        // fall-through to copy the associated value
    }

    switch (token) {
        case START_ARRAY:
            destination.writeStartArray();
            while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
                copyCurrentStructure(destination, parser);
            }
            destination.writeEndArray();
            break;
        case START_OBJECT:
            destination.writeStartObject();
            while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
                copyCurrentStructure(destination, parser);
            }
            destination.writeEndObject();
            break;
        default: // others are simple:
            destination.copyCurrentEvent(parser);
    }
}
 
Example 5
Source File: XmlXContentGenerator.java    From elasticsearch-xml with Apache License 2.0 4 votes vote down vote up
public static void copyCurrentEvent(XContentGenerator generator, XContentParser parser) throws IOException {
    switch (parser.currentToken()) {
        case START_OBJECT:
            generator.writeStartObject();
            break;
        case END_OBJECT:
            generator.writeEndObject();
            break;
        case START_ARRAY:
            generator.writeStartArray();
            break;
        case END_ARRAY:
            generator.writeEndArray();
            break;
        case FIELD_NAME:
            generator.writeFieldName(parser.currentName());
            break;
        case VALUE_STRING:
            if (parser.hasTextCharacters()) {
                generator.writeString(parser.textCharacters(), parser.textOffset(), parser.textLength());
            } else {
                generator.writeString(parser.text());
            }
            break;
        case VALUE_NUMBER:
            switch (parser.numberType()) {
                case INT:
                    generator.writeNumber(parser.intValue());
                    break;
                case LONG:
                    generator.writeNumber(parser.longValue());
                    break;
                case FLOAT:
                    generator.writeNumber(parser.floatValue());
                    break;
                case DOUBLE:
                    generator.writeNumber(parser.doubleValue());
                    break;
            }
            break;
        case VALUE_BOOLEAN:
            generator.writeBoolean(parser.booleanValue());
            break;
        case VALUE_NULL:
            generator.writeNull();
            break;
        case VALUE_EMBEDDED_OBJECT:
            generator.writeBinary(parser.binaryValue());
    }
}