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

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

Introduction

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

Prototype

public SchemaField newField(String fieldName, String fieldType, Map<String, ?> options) 

Source Link

Document

Returns a SchemaField if the given fieldName does not already exist in this schema, and does not match any dynamic fields in this schema.

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  v a 2s.co m

    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);
}

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

License:Open Source License

SchemaField getSchemaField(final IndexSchema schema, final FieldEntry entry) {
    Map<String, Boolean> options = new HashMap<String, Boolean>();
    return schema.newField(this.getTargetFieldname(entry), fieldType, options);
}