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

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

Introduction

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

Prototype

public boolean isMutable() 

Source Link

Usage

From source file:com.sindicetech.siren.solr.facet.SirenFacetProcessor.java

License:Open Source License

@Override
public void processAdd(AddUpdateCommand cmd) throws IOException {
    SolrCore core = cmd.getReq().getCore();
    IndexSchema schema = core.getLatestSchema();

    if (!schema.isMutable()) {
        throw new SolrException(BAD_REQUEST,
                String.format("This IndexSchema, of core %s, is not mutable.", core.getName()));
    }//from ww w  .j  a  va  2  s .c  om

    SolrInputDocument doc = cmd.getSolrInputDocument();

    extractor.setSchema(schema);
    List<SirenFacetEntry> entries = extractor.extractFacets(doc);

    // update schema
    // use Sets so that we add a fieldname only once even if it is generated multiple times (for
    // multiple paths)
    Set<SchemaField> newFields = new HashSet<SchemaField>();
    for (SirenFacetEntry entry : entries) {
        // skip entry if the field is already in the schema
        if (schema.getFieldOrNull(entry.toFieldName()) != null) {
            continue;
        }

        TypeMapping typeMapping = getTypeMappingValueClass(entry.datatype.xsdDatatype);

        // skip facet values that are too large altogether - they don't make sense for faceting 
        if (entry.value instanceof String && ((String) entry.value)
                .length() > (typeMapping.maxFieldSize != null ? typeMapping.maxFieldSize
                        : DEFAULT_MAX_FACET_VALUE_LENGTH)) {
            continue;
        }

        String fieldTypeName = getTypeMappingValueClass(entry.datatype.xsdDatatype).fieldType;

        Map<String, Boolean> options = new HashMap<String, Boolean>();
        // see FieldProperties.propertyNames[]
        options.put("indexed", false);
        options.put("stored", false);
        options.put("docValues", true);
        options.put("multiValued", true);

        newFields.add(schema.newField(entry.toFieldName(), fieldTypeName, options));
    }

    if (!newFields.isEmpty()) {
        IndexSchema newSchema = schema.addFields(newFields);
        cmd.getReq().getCore().setLatestSchema(newSchema);
        cmd.getReq().updateSchemaToLatest();
        logger.debug("Successfully added field(s) to the schema.");
    }

    // update document
    for (SirenFacetEntry entry : entries) {
        TypeMapping typeMapping = getTypeMappingValueClass(entry.datatype.xsdDatatype);

        // skip facet values that are too large altogether - they don't make sense for faceting 
        if (entry.value instanceof String && ((String) entry.value)
                .length() > (typeMapping.maxFieldSize != null ? typeMapping.maxFieldSize
                        : DEFAULT_MAX_FACET_VALUE_LENGTH)) {
            continue;
        }

        doc.addField(entry.toFieldName(), entry.value);
    }

    // call the next one in chain
    super.processAdd(cmd);
}