com.frank.search.solr.core.schema.SolrSchemaWriter.java Source code

Java tutorial

Introduction

Here is the source code for com.frank.search.solr.core.schema.SolrSchemaWriter.java

Source

/*
 * Copyright 2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.frank.search.solr.core.schema;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.frank.search.solr.core.SolrExceptionTranslator;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.common.SolrException;
import org.springframework.dao.InvalidDataAccessResourceUsageException;
import org.springframework.dao.support.PersistenceExceptionTranslator;
import com.frank.search.solr.server.SolrClientFactory;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author Christoph Strobl
 * @since 1.3
 */
public class SolrSchemaWriter {

    private static final PersistenceExceptionTranslator EXCEPTION_TRANSLATOR = new SolrExceptionTranslator();
    private SolrClientFactory factory;

    public SolrSchemaWriter(SolrClientFactory factory) {
        this.factory = factory;
    }

    public void writeSchema(SchemaDefinition schemaDefinition) {

        if (isSchemaPresent(schemaDefinition.getCollectionName())) {
            updateSchema(schemaDefinition);
            return;
        }

        createSchema(schemaDefinition);
    }

    protected void createSchema(SchemaDefinition schemaDefinition) {
        throw new UnsupportedOperationException("The solr rest API does not allow schema creation.");
    }

    protected void updateSchema(SchemaDefinition schemaDefinition) {

        SchemaDefinition existing = loadExistingSchema(schemaDefinition.getCollectionName());

        List<SchemaDefinition.FieldDefinition> fieldsToBeCreated = new ArrayList<SchemaDefinition.FieldDefinition>();
        for (SchemaDefinition.FieldDefinition fieldDefinition : schemaDefinition.getFields()) {
            if (!existing.containsField(fieldDefinition.getName())) {
                fieldsToBeCreated.add(fieldDefinition);
            }
        }

        writeFieldDefinitions(fieldsToBeCreated, schemaDefinition.getCollectionName());
    }

    private void writeFieldDefinitions(Collection<SchemaDefinition.FieldDefinition> definitions,
            String collectionName) {
        if (!CollectionUtils.isEmpty(definitions)) {
            try {
                SolrSchemaRequest.create().fields(definitions).build()
                        .process(factory.getSolrClient(collectionName));
            } catch (SolrServerException e) {
                throw EXCEPTION_TRANSLATOR.translateExceptionIfPossible(new RuntimeException(e));
            } catch (IOException e) {
                throw new InvalidDataAccessResourceUsageException("Failed to write schema field definitions.", e);
            }
        }
    }

    boolean isSchemaPresent(String collectionName) {
        return !retrieveSchemaVersion(collectionName).isNaN();
    }

    SchemaDefinition loadExistingSchema(String collectionName) {

        try {
            SolrJsonResponse response = SolrSchemaRequest.schema().process(factory.getSolrClient(collectionName));
            if (response != null && response.getNode("schema") != null) {
                ObjectMapper mapper = new ObjectMapper();
                mapper.enable(MapperFeature.AUTO_DETECT_CREATORS);
                mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
                mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
                return mapper.readValue(response.getNode("schema").toString(), SchemaDefinition.class);
            }
            return null;
        } catch (SolrServerException e) {
            throw EXCEPTION_TRANSLATOR.translateExceptionIfPossible(new RuntimeException(e));
        } catch (IOException e) {
            throw new InvalidDataAccessResourceUsageException("Failed to load schema definition.", e);
        } catch (SolrException e) {
            throw EXCEPTION_TRANSLATOR.translateExceptionIfPossible(new RuntimeException(e));
        }
    }

    Double retrieveSchemaVersion(String collectionName) {
        try {
            SolrJsonResponse response = SolrSchemaRequest.version().process(factory.getSolrClient(collectionName));
            JsonNode node = response.getNode("version");
            return node != null ? node.asDouble() : Double.NaN;
        } catch (SolrServerException e) {
            EXCEPTION_TRANSLATOR.translateExceptionIfPossible(new RuntimeException(e));
        } catch (IOException e) {
            EXCEPTION_TRANSLATOR.translateExceptionIfPossible(new RuntimeException(e));
        } catch (SolrException e) {
            EXCEPTION_TRANSLATOR.translateExceptionIfPossible(new RuntimeException(e));
        }
        return Double.NaN;
    }

}