org.elasticsearch.common.xcontent.XContentGenerator Java Examples

The following examples show how to use org.elasticsearch.common.xcontent.XContentGenerator. 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: DocumentContentBuilder.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void writeBoolean(XContentGenerator generator, Boolean boolValue) throws IOException {
  if (boolValue != null) {
    generator.writeBoolean(boolValue);
  } else {
    generator.writeNull();
  }
}
 
Example #3
Source File: DocumentContentBuilder.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void writeDouble(XContentGenerator generator, Double doubleValue) throws IOException {
  if (doubleValue != null) {
    generator.writeNumber(doubleValue);
  } else {
    generator.writeNull();
  }
}
 
Example #4
Source File: DocumentContentBuilder.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void writeInteger(XContentGenerator generator, Integer intValue) throws IOException {
  if (intValue != null) {
    generator.writeNumber(intValue);
  } else {
    generator.writeNull();
  }
}
 
Example #5
Source File: DocumentContentBuilder.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void writeLong(XContentGenerator generator, Long longValue) throws IOException {
  if (longValue != null) {
    generator.writeNumber(longValue);
  } else {
    generator.writeNull();
  }
}
 
Example #6
Source File: DocumentContentBuilder.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void writeDateTime(XContentGenerator generator, Instant dateTimeValue)
    throws IOException {
  if (dateTimeValue != null) {
    generator.writeString(dateTimeValue.toString());
  } else {
    generator.writeNull();
  }
}
 
Example #7
Source File: DocumentContentBuilder.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void writeDate(XContentGenerator generator, LocalDate dateValue) throws IOException {
  if (dateValue != null) {
    generator.writeString(dateValue.toString());
  } else {
    generator.writeNull();
  }
}
 
Example #8
Source File: DocumentContentBuilder.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void writeString(XContentGenerator generator, String strValue) throws IOException {
  if (strValue != null) {
    generator.writeString(strValue);
  } else {
    generator.writeNull();
  }
}
 
Example #9
Source File: DocumentContentBuilder.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void writeReference(
    XContentGenerator generator, int depth, int maxDepth, Entity xrefEntity) throws IOException {
  if (xrefEntity != null) {
    createRecReferenceAttribute(generator, depth, maxDepth, xrefEntity);
  } else {
    generator.writeNull();
  }
}
 
Example #10
Source File: DocumentContentBuilder.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void writeMultiReference(
    XContentGenerator generator, int depth, int maxDepth, Iterable<Entity> mrefEntities)
    throws IOException {
  if (!Iterables.isEmpty(mrefEntities)) {
    generator.writeStartArray();
    for (Entity mrefEntity : mrefEntities) {
      createRecReferenceAttribute(generator, depth, maxDepth, mrefEntity);
    }
    generator.writeEndArray();
  } else {
    generator.writeNull();
  }
}
 
Example #11
Source File: DocumentContentBuilder.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void createRec(Entity entity, XContentGenerator generator, int depth, int maxDepth)
    throws IOException {
  for (Attribute attr : entity.getEntityType().getAtomicAttributes()) {
    generator.writeFieldName(documentIdGenerator.generateId(attr));
    createRec(entity, attr, generator, depth, maxDepth);
  }
}
 
Example #12
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 #13
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 #14
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 #15
Source File: SmileXContent.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
public XContentGenerator createGenerator(OutputStream os, Set<String> includes, Set<String> excludes) throws IOException {
    return new SmileXContentGenerator(SMILE_FACTORY.createGenerator(os, JsonEncoding.UTF8), os, includes, excludes);
}
 
Example #16
Source File: YamlXContent.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
public XContentGenerator createGenerator(OutputStream os, Set<String> includes, Set<String> excludes) throws IOException {
    return new YamlXContentGenerator(YAML_FACTORY.createGenerator(os, JsonEncoding.UTF8), os, includes, excludes);
}
 
Example #17
Source File: JsonXContent.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
public XContentGenerator createGenerator(OutputStream os, Set<String> includes, Set<String> excludes) throws IOException {
    return new JsonXContentGenerator(JSON_FACTORY.createGenerator(os, JsonEncoding.UTF8), os, includes, excludes);
}
 
Example #18
Source File: XmlXContent.java    From elasticsearch-xml with Apache License 2.0 4 votes vote down vote up
@Override
public XContentGenerator createGenerator(OutputStream os) throws IOException {
    return new XmlXContentGenerator(xmlFactory.createGenerator(os, JsonEncoding.UTF8));
}
 
Example #19
Source File: DocumentContentBuilder.java    From molgenis with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void createRec(
    Entity entity, Attribute attr, XContentGenerator generator, int depth, int maxDepth)
    throws IOException {
  String attrName = attr.getName();
  AttributeType attrType = attr.getDataType();

  switch (attrType) {
    case BOOL:
      Boolean boolValue = entity.getBoolean(attrName);
      writeBoolean(generator, boolValue);
      break;
    case DECIMAL:
      Double doubleValue = entity.getDouble(attrName);
      writeDouble(generator, doubleValue);
      break;
    case INT:
      Integer intValue = entity.getInt(attrName);
      writeInteger(generator, intValue);
      break;
    case LONG:
      Long longValue = entity.getLong(attrName);
      writeLong(generator, longValue);
      break;
    case EMAIL:
    case ENUM:
    case HTML:
    case HYPERLINK:
    case SCRIPT:
    case STRING:
    case TEXT:
      String strValue = entity.getString(attrName);
      writeString(generator, strValue);
      break;
    case DATE:
      LocalDate date = entity.getLocalDate(attrName);
      writeDate(generator, date);
      break;
    case DATE_TIME:
      Instant dateTime = entity.getInstant(attrName);
      writeDateTime(generator, dateTime);
      break;
    case CATEGORICAL:
    case XREF:
    case FILE:
      Entity xrefEntity = entity.getEntity(attrName);
      writeReference(generator, depth, maxDepth, xrefEntity);
      break;
    case CATEGORICAL_MREF:
    case MREF:
    case ONE_TO_MANY:
      Iterable<Entity> mrefEntities = entity.getEntities(attrName);
      writeMultiReference(generator, depth, maxDepth, mrefEntities);
      break;
    case COMPOUND:
      throw new IllegalAttributeTypeException(attrType);
    default:
      throw new UnexpectedEnumException(attrType);
  }
}
 
Example #20
Source File: CsvXContent.java    From elasticsearch-rest-command with The Unlicense 4 votes vote down vote up
@Override
public XContentGenerator createGenerator(Writer writer) throws IOException {
	return new CsvXContentGenerator(csvFactory.createGenerator(writer));
}
 
Example #21
Source File: CsvXContent.java    From elasticsearch-rest-command with The Unlicense 4 votes vote down vote up
@Override
public XContentGenerator createGenerator(OutputStream os)
		throws IOException {
	 return new CsvXContentGenerator(csvFactory.createGenerator(os, JsonEncoding.UTF8));
}
 
Example #22
Source File: XmlXContentBuilder.java    From elasticsearch-xml with Apache License 2.0 4 votes vote down vote up
public XContentGenerator generator() {
    return this.generator;
}
 
Example #23
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());
    }
}
 
Example #24
Source File: XmlXContent.java    From elasticsearch-xml with Apache License 2.0 4 votes vote down vote up
@Override
public XContentGenerator createGenerator(OutputStream os, String[] filters) throws IOException {
    // ignore filters (for now)
    return new XmlXContentGenerator(xmlFactory.createGenerator(os, JsonEncoding.UTF8));
}