Example usage for org.apache.solr.schema IndexSchema addField

List of usage examples for org.apache.solr.schema IndexSchema addField

Introduction

In this page you can find the example usage for org.apache.solr.schema IndexSchema addField.

Prototype

public IndexSchema addField(SchemaField newField) 

Source Link

Usage

From source file:com.sindicetech.siren.solr.handler.mapper.FieldMappersHandler.java

License:Open Source License

private void addSchemaField(FieldMapper mapper, FieldEntry entry) {
    if (!core.getLatestSchema().isMutable()) {
        final String message = "This IndexSchema is not mutable.";
        throw new SolrException(BAD_REQUEST, message);
    }/*from   w w  w .j  a  va 2  s .c  om*/

    for (;;) {
        final IndexSchema oldSchema = core.getLatestSchema();
        if (oldSchema.getFieldTypeNoEx(mapper.getTargetFieldname(entry)) != null) {
            return; // the field already exists in the schema
        }

        try {
            SchemaField field = mapper.getSchemaField(core.getLatestSchema(), entry);
            IndexSchema newSchema = oldSchema.addField(field);
            if (newSchema != null) {
                core.setLatestSchema(newSchema);
                logger.debug("Successfully added field '{}' to the schema.", field.getName());
                return; // success - exit from the retry loop
            } else {
                throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Failed to add fields.");
            }
        } catch (ManagedIndexSchema.FieldExistsException e) {
            logger.debug("The field to be added already exists in the schema - retrying.");
            // No action: at least one field to be added already exists in the schema, so retry
            // We should never get here, since oldSchema.getFieldTypeNoEx(field) will exclude already existing fields
        } catch (ManagedIndexSchema.SchemaChangedInZkException e) {
            logger.debug("Schema changed while processing request - retrying.");
        }
    }
}