Example usage for org.apache.solr.spelling.suggest SolrSuggester init

List of usage examples for org.apache.solr.spelling.suggest SolrSuggester init

Introduction

In this page you can find the example usage for org.apache.solr.spelling.suggest SolrSuggester init.

Prototype

public String init(NamedList<?> config, SolrCore core) 

Source Link

Document

Uses the config and the core to initialize the underlying Lucene suggester

Usage

From source file:org.alfresco.solr.component.AsyncBuildSuggestComponent.java

License:Open Source License

@Override
public void inform(SolrCore core) {
    if (initParams != null) {
        LOG.info("Initializing SuggestComponent");
        boolean hasDefault = false;
        for (int i = 0; i < initParams.size(); i++) {
            if (initParams.getName(i).equals(CONFIG_PARAM_LABEL)) {
                NamedList suggesterParams = (NamedList) initParams.getVal(i);
                SolrSuggester suggester = new SolrSuggester();
                boolean buildOnStartup;
                Object buildOnStartupObj = suggesterParams.get(BUILD_ON_STARTUP_LABEL);
                if (buildOnStartupObj == null) {
                    File storeFile = suggester.getStoreFile();
                    buildOnStartup = storeFile == null || !storeFile.exists();
                } else {
                    buildOnStartup = Boolean.parseBoolean((String) buildOnStartupObj);
                }//w ww .j av  a  2 s. c om
                boolean buildOnCommit = Boolean
                        .parseBoolean((String) suggesterParams.get(BUILD_ON_COMMIT_LABEL));
                boolean buildOnOptimize = Boolean
                        .parseBoolean((String) suggesterParams.get(BUILD_ON_OPTIMIZE_LABEL));
                boolean enabled = Boolean.parseBoolean((String) suggesterParams.get(ENABLED_LABEL));
                long minSecsBetweenBuilds = Long
                        .parseLong(core.getCoreDescriptor().getCoreProperty(MIN_SECS_BETWEEN_BUILDS, "-1"));
                SuggesterCache suggesterCache = new SuggesterCache(core, suggesterParams, enabled,
                        buildOnCommit, buildOnOptimize, buildOnStartup);

                String dictionary = suggester.init(suggesterParams, core);
                if (dictionary != null) {
                    boolean isDefault = dictionary.equals(DEFAULT_DICT_NAME);
                    if (isDefault && !hasDefault) {
                        hasDefault = true;
                    } else if (isDefault) {
                        throw new RuntimeException("More than one dictionary is missing name.");
                    }
                    suggesterCache.setBeanName(dictionary);
                    suggesters.put(dictionary, suggesterCache);
                } else {
                    if (!hasDefault) {
                        suggesterCache.setBeanName(DEFAULT_DICT_NAME);
                        suggesters.put(DEFAULT_DICT_NAME, suggesterCache);
                        hasDefault = true;
                    } else {
                        throw new RuntimeException("More than one dictionary is missing name.");
                    }
                }

                try {
                    suggesterCache.afterPropertiesSet();
                } catch (Exception e) {
                    LOG.error("Unable to initialise SuggesterCache.", e);
                    throw new RuntimeException("Unable to initialise SuggesterCache.", e);
                }

                // Register event listeners for this Suggester
                core.registerFirstSearcherListener(new SuggesterListener(suggesterCache, minSecsBetweenBuilds));
                if (buildOnCommit || buildOnOptimize) {
                    LOG.debug("Registering newSearcher listener for suggester: " + suggester.getName());
                    core.registerNewSearcherListener(
                            new SuggesterListener(suggesterCache, minSecsBetweenBuilds));
                }
            }
        }
    }
}