Search in sources :

Example 1 with SolrSuggester

use of org.apache.solr.spelling.suggest.SolrSuggester in project lucene-solr by apache.

the class SuggestComponent method prepare.

/** Responsible for issuing build and rebuild command to the specified {@link SolrSuggester} */
@Override
public void prepare(ResponseBuilder rb) throws IOException {
    SolrParams params = rb.req.getParams();
    LOG.info("SuggestComponent prepare with : " + params);
    if (!params.getBool(COMPONENT_NAME, false)) {
        return;
    }
    boolean buildAll = params.getBool(SUGGEST_BUILD_ALL, false);
    boolean reloadAll = params.getBool(SUGGEST_RELOAD_ALL, false);
    final Collection<SolrSuggester> querysuggesters;
    if (buildAll || reloadAll) {
        querysuggesters = suggesters.values();
    } else {
        querysuggesters = getSuggesters(params);
    }
    if (params.getBool(SUGGEST_BUILD, false) || buildAll) {
        for (SolrSuggester suggester : querysuggesters) {
            suggester.build(rb.req.getCore(), rb.req.getSearcher());
        }
        rb.rsp.add("command", (!buildAll) ? "build" : "buildAll");
    } else if (params.getBool(SUGGEST_RELOAD, false) || reloadAll) {
        for (SolrSuggester suggester : querysuggesters) {
            suggester.reload(rb.req.getCore(), rb.req.getSearcher());
        }
        rb.rsp.add("command", (!reloadAll) ? "reload" : "reloadAll");
    }
}
Also used : SolrParams(org.apache.solr.common.params.SolrParams) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SolrSuggester(org.apache.solr.spelling.suggest.SolrSuggester)

Example 2 with SolrSuggester

use of org.apache.solr.spelling.suggest.SolrSuggester in project SearchServices by Alfresco.

the class AsyncBuildSuggestComponent method inform.

@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);
                }
                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));
                }
            }
        }
    }
}
Also used : NamedList(org.apache.solr.common.util.NamedList) SolrSuggester(org.apache.solr.spelling.suggest.SolrSuggester) File(java.io.File) IOException(java.io.IOException)

Example 3 with SolrSuggester

use of org.apache.solr.spelling.suggest.SolrSuggester in project lucene-solr by apache.

the class SuggestComponent method inform.

@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();
                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.");
                    }
                    suggesters.put(dictionary, suggester);
                } else {
                    if (!hasDefault) {
                        suggesters.put(DEFAULT_DICT_NAME, suggester);
                        hasDefault = true;
                    } else {
                        throw new RuntimeException("More than one dictionary is missing name.");
                    }
                }
                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);
                }
                boolean buildOnCommit = Boolean.parseBoolean((String) suggesterParams.get(BUILD_ON_COMMIT_LABEL));
                boolean buildOnOptimize = Boolean.parseBoolean((String) suggesterParams.get(BUILD_ON_OPTIMIZE_LABEL));
                if (buildOnCommit || buildOnOptimize || buildOnStartup) {
                    SuggesterListener listener = new SuggesterListener(core, suggester, buildOnCommit, buildOnOptimize, buildOnStartup, core.isReloaded());
                    LOG.info("Registering searcher listener for suggester: " + suggester.getName() + " - " + listener);
                    core.registerFirstSearcherListener(listener);
                    core.registerNewSearcherListener(listener);
                }
            }
        }
    }
}
Also used : NamedList(org.apache.solr.common.util.NamedList) SolrSuggester(org.apache.solr.spelling.suggest.SolrSuggester) File(java.io.File)

Example 4 with SolrSuggester

use of org.apache.solr.spelling.suggest.SolrSuggester in project lucene-solr by apache.

the class SuggestComponent method process.

/** 
   * Responsible for using the specified suggester to get the suggestions 
   * for the query and write the results 
   * */
@Override
public void process(ResponseBuilder rb) throws IOException {
    SolrParams params = rb.req.getParams();
    LOG.info("SuggestComponent process with : " + params);
    if (!params.getBool(COMPONENT_NAME, false) || suggesters.isEmpty()) {
        return;
    }
    boolean buildAll = params.getBool(SUGGEST_BUILD_ALL, false);
    boolean reloadAll = params.getBool(SUGGEST_RELOAD_ALL, false);
    Set<SolrSuggester> querySuggesters;
    try {
        querySuggesters = getSuggesters(params);
    } catch (SolrException ex) {
        if (!buildAll && !reloadAll) {
            throw ex;
        } else {
            querySuggesters = new HashSet<>();
        }
    }
    String query = params.get(SUGGEST_Q);
    if (query == null) {
        query = rb.getQueryString();
        if (query == null) {
            query = params.get(CommonParams.Q);
        }
    }
    if (query != null) {
        int count = params.getInt(SUGGEST_COUNT, 1);
        boolean highlight = params.getBool(SUGGEST_HIGHLIGHT, false);
        boolean allTermsRequired = params.getBool(SUGGEST_ALL_TERMS_REQUIRED, true);
        String contextFilter = params.get(SUGGEST_CONTEXT_FILTER_QUERY);
        if (contextFilter != null) {
            contextFilter = contextFilter.trim();
            if (contextFilter.length() == 0) {
                contextFilter = null;
            }
        }
        SuggesterOptions options = new SuggesterOptions(new CharsRef(query), count, contextFilter, allTermsRequired, highlight);
        Map<String, SimpleOrderedMap<NamedList<Object>>> namedListResults = new HashMap<>();
        for (SolrSuggester suggester : querySuggesters) {
            SuggesterResult suggesterResult = suggester.getSuggestions(options);
            toNamedList(suggesterResult, namedListResults);
        }
        rb.rsp.add(SuggesterResultLabels.SUGGEST, namedListResults);
    }
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SuggesterOptions(org.apache.solr.spelling.suggest.SuggesterOptions) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) CharsRef(org.apache.lucene.util.CharsRef) SuggesterResult(org.apache.solr.spelling.suggest.SuggesterResult) SolrParams(org.apache.solr.common.params.SolrParams) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SolrSuggester(org.apache.solr.spelling.suggest.SolrSuggester) SolrException(org.apache.solr.common.SolrException) HashSet(java.util.HashSet)

Example 5 with SolrSuggester

use of org.apache.solr.spelling.suggest.SolrSuggester in project lucene-solr by apache.

the class SuggestComponent method initializeMetrics.

@Override
public void initializeMetrics(SolrMetricManager manager, String registryName, String scope) {
    registry = manager.registry(registryName);
    manager.registerGauge(this, registryName, () -> ramBytesUsed(), true, "totalSizeInBytes", getCategory().toString(), scope);
    MetricsMap suggestersMap = new MetricsMap((detailed, map) -> {
        for (Map.Entry<String, SolrSuggester> entry : suggesters.entrySet()) {
            SolrSuggester suggester = entry.getValue();
            map.put(entry.getKey(), suggester.toString());
        }
    });
    manager.registerGauge(this, registryName, suggestersMap, true, "suggesters", getCategory().toString(), scope);
}
Also used : MetricsMap(org.apache.solr.metrics.MetricsMap) SolrSuggester(org.apache.solr.spelling.suggest.SolrSuggester) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MetricsMap(org.apache.solr.metrics.MetricsMap)

Aggregations

SolrSuggester (org.apache.solr.spelling.suggest.SolrSuggester)8 HashMap (java.util.HashMap)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)4 SolrParams (org.apache.solr.common.params.SolrParams)4 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)4 File (java.io.File)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 CharsRef (org.apache.lucene.util.CharsRef)2 NamedList (org.apache.solr.common.util.NamedList)2 SuggesterOptions (org.apache.solr.spelling.suggest.SuggesterOptions)2 SuggesterResult (org.apache.solr.spelling.suggest.SuggesterResult)2 IOException (java.io.IOException)1 SolrException (org.apache.solr.common.SolrException)1 MetricsMap (org.apache.solr.metrics.MetricsMap)1