All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.stratio.cassandra.lucene.schema.SchemaBuilder Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2014 Stratio (http://stratio.com)
 *
 * 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.stratio.cassandra.lucene.schema;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.stratio.cassandra.lucene.IndexException;
import com.stratio.cassandra.lucene.common.JsonSerializer;
import com.stratio.cassandra.lucene.schema.analysis.AnalyzerBuilder;
import com.stratio.cassandra.lucene.schema.analysis.ClasspathAnalyzerBuilder;
import com.stratio.cassandra.lucene.schema.analysis.StandardAnalyzers;
import com.stratio.cassandra.lucene.schema.mapping.Mapper;
import com.stratio.cassandra.lucene.schema.mapping.builder.MapperBuilder;
import org.apache.lucene.analysis.Analyzer;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Class for building new {@link Schema}s both programmatically and from JSON.
 *
 * @author Andres de la Pena {@literal }
 */
public class SchemaBuilder {

    @JsonProperty("default_analyzer")
    private String defaultAnalyzerName;

    @JsonProperty("analyzers")
    private final Map analyzerBuilders;

    @JsonProperty("fields")
    private final Map> mapperBuilders;

    @JsonCreator
    SchemaBuilder(@JsonProperty("default_analyzer") String defaultAnalyzerName,
                  @JsonProperty("analyzers") Map analyzerBuilders,
                  @JsonProperty("fields") Map> mapperBuilders) {
        this.defaultAnalyzerName = defaultAnalyzerName;
        this.analyzerBuilders = analyzerBuilders != null ? analyzerBuilders : new LinkedHashMap<>();
        this.mapperBuilders = mapperBuilders != null ? mapperBuilders : new LinkedHashMap<>();
    }

    /**
     * Sets the name of the default {@link Analyzer}.
     *
     * @param name the name of the default {@link Analyzer}
     * @return this with the specified default analyzer
     */
    public SchemaBuilder defaultAnalyzer(String name) {
        defaultAnalyzerName = name;
        return this;
    }

    /**
     * Adds a new {@link Analyzer}.
     *
     * @param name the name of the {@link Analyzer} to be added
     * @param analyzer the builder of the {@link Analyzer} to be added
     * @return this with the specified analyzer
     */
    public SchemaBuilder analyzer(String name, AnalyzerBuilder analyzer) {
        analyzerBuilders.put(name, analyzer);
        return this;
    }

    /**
     * Adds a new {@link Mapper}.
     *
     * @param field the name of the {@link Mapper} to be added
     * @param mapper the builder of the {@link Mapper} to be added
     * @return this with the specified mapper
     */
    public SchemaBuilder mapper(String field, MapperBuilder mapper) {
        mapperBuilders.put(field, mapper);
        return this;
    }

    /**
     * Returns the {@link Schema} defined by this.
     *
     * @return a new schema
     */
    public Schema build() {

        Map mappers = new LinkedHashMap<>(mapperBuilders.size());
        for (Map.Entry> entry : mapperBuilders.entrySet()) {
            String name = entry.getKey();
            MapperBuilder builder = entry.getValue();
            Mapper mapper = builder.build(name);
            mappers.put(name, mapper);
        }

        Map analyzers = new LinkedHashMap<>();
        for (Map.Entry entry : analyzerBuilders.entrySet()) {
            String name = entry.getKey();
            Analyzer analyzer = entry.getValue().analyzer();
            analyzers.put(name, analyzer);
        }

        Analyzer defaultAnalyzer;
        if (defaultAnalyzerName == null) {
            defaultAnalyzer = StandardAnalyzers.DEFAULT.get();
        } else {
            defaultAnalyzer = analyzers.get(defaultAnalyzerName);
            if (defaultAnalyzer == null) {
                defaultAnalyzer = StandardAnalyzers.get(defaultAnalyzerName);
                if (defaultAnalyzer == null) {
                    try {
                        defaultAnalyzer = (new ClasspathAnalyzerBuilder(defaultAnalyzerName)).analyzer();
                    } catch (Exception e) {
                        throw new IndexException(e, "Not found analyzer: '{}'", defaultAnalyzerName);
                    }
                }
                analyzers.put(defaultAnalyzerName, defaultAnalyzer);
            }
        }
        return new Schema(defaultAnalyzer, mappers, analyzers);
    }

    /**
     * Returns the JSON representation of this builder.
     *
     * @return a JSON {@code String}
     */
    public String toJson() {
        try {
            return JsonSerializer.toString(this);
        } catch (IOException e) {
            throw new IndexException(e, "Unformateable JSON schema: {}", e.getMessage());
        }
    }

    /**
     * Returns the {@link Schema} contained in the specified JSON {@code String}.
     *
     * @param json a {@code String} containing the JSON representation of the {@link Schema} to be parsed
     * @return the schema contained in the specified JSON {@code String}
     */
    public static SchemaBuilder fromJson(String json) {
        try {
            return JsonSerializer.fromString(json, SchemaBuilder.class);
        } catch (IOException e) {
            throw new IndexException(e, "Unparseable JSON schema: {}: {}", e.getMessage(), json);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy