Example usage for org.apache.solr.schema ManagedIndexSchemaFactory ManagedIndexSchemaFactory

List of usage examples for org.apache.solr.schema ManagedIndexSchemaFactory ManagedIndexSchemaFactory

Introduction

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

Prototype

ManagedIndexSchemaFactory

Source Link

Usage

From source file:fr.gael.dhus.system.init.SolrInitializer.java

License:Open Source License

/**
 * Opens and Parses `schema.xml` and adds every fields returned by
 * {@link MetadataTypeService#getSolrFields()}.
 *
 * @param path_to_coredir path to the core's instanceDir.
 * @param path_to_schema path to the schema.xml to edit.
 *
 * @throws ParserConfigurationException An error occured while parsing schema.xml
 * @throws IOException An error occured while accessing schema.xml
 * @throws SAXException An error occured while parsing schema.xml
 *///from   www .j av a2  s. com
public void createSchema(Path path_to_coredir, String path_to_schema)
        throws ParserConfigurationException, IOException, SAXException {
    SolrConfig sc = new SolrConfig(path_to_coredir, SOLR_CONFIG_NAME, null);
    ManagedIndexSchemaFactory misf = new ManagedIndexSchemaFactory();
    NamedList named_list = new NamedList();
    named_list.add("mutable", Boolean.TRUE);
    misf.init(named_list);
    ManagedIndexSchema schema = misf.create(path_to_schema, sc);

    Map<String, SolrField> solrfm = metadataTypeService.getSolrFields();

    List<SchemaField> schemafl = new ArrayList<>(solrfm.size() + 1);

    for (SolrField solrf : solrfm.values()) {
        Map<String, Object> options = new HashMap<>();
        int metadata_properties = DEFAULT;

        if (solrf.isStored() != null) {
            if (solrf.isStored()) {
                options.put("stored", true);
            } else {
                options.put("stored", false);
                metadata_properties &= ~STORED;
            }
        }

        if (solrf.isIndexed() != null) {
            if (solrf.isIndexed()) {
                options.put("indexed", true);
            } else {
                options.put("indexed", false);
                metadata_properties &= ~INDEXED;
            }
        }

        if (solrf.isRequired() != null) {
            if (solrf.isRequired()) {
                options.put("required", true);
                metadata_properties |= REQUIRED;
            } else {
                options.put("required", false);
            }
        }

        if (solrf.isMultiValued() != null) {
            if (solrf.isMultiValued()) {
                options.put("multiValued", true);
                metadata_properties |= MULTIVALUED;
            } else {
                options.put("multiValued", false);
            }
        }

        if (LOGGER.isDebugEnabled()) {
            StringBuilder sb = new StringBuilder("solr field: ").append(solrf.getName());
            sb.append("  type=").append(solrf.getType());
            sb.append("  stored=").append((metadata_properties & STORED) > 0);
            sb.append("  indexed=").append((metadata_properties & INDEXED) > 0);
            sb.append("  required=").append((metadata_properties & REQUIRED) > 0);
            sb.append("  multiValued=").append((metadata_properties & MULTIVALUED) > 0);
            LOGGER.debug(sb.toString());
        }

        SchemaField schemaf;
        if ((schemaf = schema.getFieldOrNull(solrf.getName())) != null) {
            // If already defined with a different type: fatal error!
            if (!schemaf.getType().getTypeName().equals(solrf.getType())) {
                String msg = new StringBuilder().append("Conflicting solr field '").append(solrf.getName())
                        .append("' defined twice with different types: ")
                        .append(schemaf.getType().getTypeName()).append(" and ").append(solrf.getType())
                        .toString();
                throw new RuntimeException(msg);
            }

            int schema_properties = schemaf.getProperties();
            // If already defined with a different properties: fatal error!
            if ((schema_properties & ALLOPTS) != metadata_properties) {
                String msg = new StringBuilder().append("Conflicting solr field '").append(solrf.getName())
                        .append("' defined twice with different properties").toString();
                throw new RuntimeException(msg);
            }

            LOGGER.info(String.format("solr field '%s' already in schema", solrf.getName()));
            continue;
        }

        LOGGER.info(String.format("Adding solr field '%s' in schema", solrf.getName()));

        schemaf = schema.newField(solrf.getName(), solrf.getType(), options);

        schemafl.add(schemaf);
    }

    // Adds new fields and saves the schema.xml
    schema.addFields(schemafl);
}