while (results != null && results.hasMore()) {
SearchResult sr = (SearchResult) results.next();
IndexRequest indexRequest = new IndexRequest(indexName);
XContentBuilder builder = jsonBuilder();
builder.startObject();
String dn = sr.getName();
logger.debug("Reading ldap object dn [{}]", dn);
Attributes ldapAttributes = sr.getAttributes();
NamingEnumeration<String> ldapAttributesIds = ldapAttributes.getIDs();
while (ldapAttributesIds.hasMoreElements()) {
String id = ldapAttributesIds.next();
logger.debug("\treading attribute id [{}]", id);
List<String> fieldValues = new ArrayList<String>();
Attribute attribute = ldapAttributes.get(id);
NamingEnumeration<?> values = attribute.getAll();
while (values.hasMoreElements()) {
Object value = values.next();
logger.debug("\t\tvalue: [{}]", value.toString());
fieldValues.add(value.toString());
}
String fieldName = resolveFieldName(id);
if(fieldValues.size() > 1){
builder.array(fieldName, fieldValues.toArray());
} else {
if (!"_id".equals(fieldName)) {
builder.field(fieldName, fieldValues.get(0));
} else {
indexRequest.id(fieldValues.get(0));
}
}
}
builder.endObject();
indexRequest.type(typeName).source(builder);
bulkRequest.add(indexRequest);
count++;
if((count % bulkSize) == 0){
BulkResponse bulkResponse = bulkRequest.execute().actionGet(bulkTimeout);