List of usage examples for org.apache.solr.core SolrCore registerNewSearcherListener
public void registerNewSearcherListener(SolrEventListener listener)
From source file:com.searchbox.SuggesterComponent.java
License:Apache License
@Override // run on loadup of solr public void inform(SolrCore core) { LOGGER.trace(("Hit inform")); // pull in stop words which will be used later loadStopWords(core.getResourceLoader()); if (storeDirname != null) { storeDir = new File(storeDirname); if (!storeDir.isAbsolute()) { storeDir = new File(core.getDataDir() + File.separator + storeDir); }//from ww w . ja v a 2 s . c om if (!storeDir.exists()) { LOGGER.warn("Directory " + storeDir.getAbsolutePath() + " doesn't exist for re-load of suggester, creating emtpy " + "directory, make sure to use suggester.build before first use!"); storeDir.mkdirs(); } else { try { // load premade dictionary object readFile(storeDir); } catch (Exception ex) { LOGGER.error("Error loading sbsuggester model"); } } } // check to see if the new searcher should trigger a build on optimize // or commit if (buildOnCommit || buildOnOptimize) { LOGGER.info("Registering newSearcher listener for Searchbox Suggester: "); core.registerNewSearcherListener(this); } }
From source file:com.searchbox.TaggerComponent.java
License:Apache License
public void inform(SolrCore core) { LOGGER.trace(("Hit inform")); if (storeDirname != null) { storeDir = new File(storeDirname); if (!storeDir.isAbsolute()) { //Where does core come from? storeDir = new File(core.getDataDir() + File.separator + storeDir); }/*from w ww . jav a 2s .co m*/ if (!storeDir.exists()) { LOGGER.warn("Directory " + storeDir.getAbsolutePath() + " doesn't exist for re-load of tagger, creating emtpy directory, make sure to use sbtagger.build before first use!"); storeDir.mkdirs(); } else { try { dfb = Tagger.loadTagger(storeDir, boostsFileName); } catch (Exception ex) { LOGGER.error("Error loading Tagger model"); } } } if (buildOnCommit || buildOnOptimize) { LOGGER.info("Registering newSearcher listener for Searchbox Tagger: "); core.registerNewSearcherListener(this); } }
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 w w . ja v a 2s. 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)); } } } } }
From source file:org.dice.solrenhancements.spellchecker.DiceSpellCheckComponent.java
License:Apache License
@Override public void inform(SolrCore core) { if (initParams != null) { LOG.info("Initializing spell checkers"); boolean hasDefault = false; for (int i = 0; i < initParams.size(); i++) { if (initParams.getName(i).equals("spellchecker")) { NamedList spellchecker = (NamedList) initParams.getVal(i); String className = (String) spellchecker.get("classname"); // TODO: this is a little bit sneaky: warn if class isnt supplied // so that its mandatory in a future release? if (className == null) className = IndexBasedSpellChecker.class.getName(); SolrResourceLoader loader = core.getResourceLoader(); SolrSpellChecker checker = loader.newInstance(className, SolrSpellChecker.class); if (checker != null) { String dictionary = checker.init(spellchecker, core); if (dictionary != null) { boolean isDefault = dictionary.equals(SolrSpellChecker.DEFAULT_DICTIONARY_NAME); if (isDefault == true && hasDefault == false) { hasDefault = true; } else if (isDefault == true && hasDefault == true) { throw new RuntimeException("More than one dictionary is missing name."); }//from w ww . j av a 2 s . co m spellCheckers.put(dictionary, checker); } else { if (hasDefault == false) { spellCheckers.put(SolrSpellChecker.DEFAULT_DICTIONARY_NAME, checker); hasDefault = true; } else { throw new RuntimeException("More than one dictionary is missing name."); } } // Register event listeners for this SpellChecker core.registerFirstSearcherListener(new SpellCheckerListener(core, checker, false, false)); boolean buildOnCommit = Boolean.parseBoolean((String) spellchecker.get("buildOnCommit")); boolean buildOnOptimize = Boolean .parseBoolean((String) spellchecker.get("buildOnOptimize")); if (buildOnCommit || buildOnOptimize) { LOG.info("Registering newSearcher listener for spellchecker: " + checker.getDictionaryName()); core.registerNewSearcherListener( new SpellCheckerListener(core, checker, buildOnCommit, buildOnOptimize)); } } else { throw new RuntimeException("Can't load spell checker: " + className); } } } Map<String, QueryConverter> queryConverters = new HashMap<String, QueryConverter>(); core.initPlugins(queryConverters, QueryConverter.class); //ensure that there is at least one query converter defined if (queryConverters.size() == 0) { LOG.info("No queryConverter defined, using default converter"); queryConverters.put("queryConverter", new SpellingQueryConverter()); } //there should only be one if (queryConverters.size() == 1) { queryConverter = queryConverters.values().iterator().next(); IndexSchema schema = core.getLatestSchema(); String fieldTypeName = (String) initParams.get("queryAnalyzerFieldType"); FieldType fieldType = schema.getFieldTypes().get(fieldTypeName); Analyzer analyzer = fieldType == null ? new WhitespaceAnalyzer(core.getSolrConfig().luceneMatchVersion) : fieldType.getQueryAnalyzer(); //TODO: There's got to be a better way! Where's Spring when you need it? queryConverter.setAnalyzer(analyzer); } } }