}
private IndexRequest parseObject(String line) throws ObjectImportException {
XContentParser parser = null;
try {
IndexRequest indexRequest = new IndexRequest();
parser = XContentFactory.xContent(line.getBytes()).createParser(line.getBytes());
Token token;
XContentBuilder sourceBuilder = XContentFactory.contentBuilder(XContentType.JSON);
long ttl = 0;
while ((token = parser.nextToken()) != Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
String fieldName = parser.currentName();
token = parser.nextToken();
if (fieldName.equals(IdFieldMapper.NAME) && token == Token.VALUE_STRING) {
indexRequest.id(parser.text());
} else if (fieldName.equals(IndexFieldMapper.NAME) && token == Token.VALUE_STRING) {
indexRequest.index(parser.text());
} else if (fieldName.equals(TypeFieldMapper.NAME) && token == Token.VALUE_STRING) {
indexRequest.type(parser.text());
} else if (fieldName.equals(RoutingFieldMapper.NAME) && token == Token.VALUE_STRING) {
indexRequest.routing(parser.text());
} else if (fieldName.equals(TimestampFieldMapper.NAME) && token == Token.VALUE_NUMBER) {
indexRequest.timestamp(String.valueOf(parser.longValue()));
} else if (fieldName.equals(TTLFieldMapper.NAME) && token == Token.VALUE_NUMBER) {
ttl = parser.longValue();
} else if (fieldName.equals("_version") && token == Token.VALUE_NUMBER) {
indexRequest.version(parser.longValue());
indexRequest.versionType(VersionType.EXTERNAL);
} else if (fieldName.equals(SourceFieldMapper.NAME) && token == Token.START_OBJECT) {
sourceBuilder.copyCurrentStructure(parser);
}
} else if (token == null) {
break;
}
}
if (ttl > 0) {
String ts = indexRequest.timestamp();
long start;
if (ts != null) {
start = Long.valueOf(ts);
} else {
start = new Date().getTime();
}
ttl = ttl - start;
if (ttl > 0) {
indexRequest.ttl(ttl);
} else {
// object is invalid, do not import
return null;
}
}
indexRequest.source(sourceBuilder);
return indexRequest;
} catch (ElasticSearchParseException e) {
throw new ObjectImportException(e);
} catch (IOException e) {
throw new ObjectImportException(e);