diff --git a/src/java/org/apache/solr/handler/SpellCheckerRequestHandler.java b/src/java/org/apache/solr/handler/SpellCheckerRequestHandler.java index 02f929ee415..8013d80f2d8 100644 --- a/src/java/org/apache/solr/handler/SpellCheckerRequestHandler.java +++ b/src/java/org/apache/solr/handler/SpellCheckerRequestHandler.java @@ -50,140 +50,140 @@ public class SpellCheckerRequestHandler extends RequestHandlerBase { private static Logger log = Logger.getLogger(SpellCheckerRequestHandler.class.getName()); - private SpellChecker spellChecker; + private SpellChecker spellChecker; - /* - * From http://wiki.apache.org/jakarta-lucene/SpellChecker - * If reader and restrictToField are both not null: - * 1. The returned words are restricted only to the words presents in the field - * "restrictToField "of the Lucene Index "reader". - * - * 2. The list is also sorted with a second criterium: the popularity (the - * frequence) of the word in the user field. - * - * 3. If "onlyMorePopular" is true and the mispelled word exist in the user field, - * return only the words more frequent than this. - * - */ - private static IndexReader nullReader = null; - private String restrictToField = null; - private boolean onlyMorePopular = false; + /* + * From http://wiki.apache.org/jakarta-lucene/SpellChecker + * If reader and restrictToField are both not null: + * 1. The returned words are restricted only to the words presents in the field + * "restrictToField "of the Lucene Index "reader". + * + * 2. The list is also sorted with a second criterium: the popularity (the + * frequence) of the word in the user field. + * + * 3. If "onlyMorePopular" is true and the mispelled word exist in the user field, + * return only the words more frequent than this. + * + */ + private static IndexReader nullReader = null; + private String restrictToField = null; + private boolean onlyMorePopular = false; - private Directory spellcheckerIndexDir = new RAMDirectory(); - private String dirDescription = "(ramdir)"; - private String termSourceField; - private static final float DEFAULT_ACCURACY = 0.5f; - private static final int DEFAULT_NUM_SUGGESTIONS = 1; + private Directory spellcheckerIndexDir = new RAMDirectory(); + private String dirDescription = "(ramdir)"; + private String termSourceField; + private static final float DEFAULT_ACCURACY = 0.5f; + private static final int DEFAULT_NUM_SUGGESTIONS = 1; - public void init(NamedList args) { - super.init(args); - SolrParams p = SolrParams.toSolrParams(args); - termSourceField = p.get("termSourceField"); + public void init(NamedList args) { + super.init(args); + SolrParams p = SolrParams.toSolrParams(args); + termSourceField = p.get("termSourceField"); - try { - String dir = p.get("spellcheckerIndexDir"); - if (null != dir) { - File f = new File(dir); - if ( ! f.isAbsolute() ) { - f = new File(SolrCore.getSolrCore().getDataDir(), dir); - } - dirDescription = f.getAbsolutePath(); - log.info("using spell directory: " + dirDescription); - spellcheckerIndexDir = FSDirectory.getDirectory(f); - } else { - log.info("using RAM based spell directory"); - } - spellChecker = new SpellChecker(spellcheckerIndexDir); - } catch (IOException e) { - throw new RuntimeException("Cannot open SpellChecker index", e); + try { + String dir = p.get("spellcheckerIndexDir"); + if (null != dir) { + File f = new File(dir); + if ( ! f.isAbsolute() ) { + f = new File(SolrCore.getSolrCore().getDataDir(), dir); } - } - - public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) - throws Exception { - SolrParams p = req.getParams(); - String words = p.get("q"); - String cmd = p.get("cmd"); - if (cmd != null) { - cmd = cmd.trim(); - if (cmd.equals("rebuild")) { - rebuild(req); - rsp.add("cmdExecuted","rebuild"); - } else if (cmd.equals("reopen")) { - reopen(); - rsp.add("cmdExecuted","reopen"); - } else { - throw new SolrException(400, "Unrecognized Command: " + cmd); - } - } - - Float accuracy; - int numSug; - try { - accuracy = p.getFloat("accuracy", DEFAULT_ACCURACY); - spellChecker.setAccuracy(accuracy); - } catch (NumberFormatException e) { - throw new RuntimeException("Accuracy must be a valid positive float", e); - } - try { - numSug = p.getInt("suggestionCount", DEFAULT_NUM_SUGGESTIONS); - } catch (NumberFormatException e) { - throw new RuntimeException("Spelling suggestion count must be a valid positive integer", e); - } - - if (null != words && !"".equals(words.trim())) { - String[] suggestions = - spellChecker.suggestSimilar(words, numSug, - nullReader, restrictToField, - onlyMorePopular); - - rsp.add("suggestions", Arrays.asList(suggestions)); - } - } - - /** Rebuilds the SpellChecker index using values from the termSourceField from the - * index pointed to by the current {@link IndexSearcher}. - */ - private void rebuild(SolrQueryRequest req) throws IOException, SolrException { - if (null == termSourceField) { - throw new SolrException - (500, "can't rebuild spellchecker index without termSourceField configured"); + dirDescription = f.getAbsolutePath(); + log.info("using spell directory: " + dirDescription); + spellcheckerIndexDir = FSDirectory.getDirectory(f); + } else { + log.info("using RAM based spell directory"); } - - IndexReader indexReader = req.getSearcher().getReader(); - Dictionary dictionary = new LuceneDictionary(indexReader, termSourceField); - spellChecker.indexDictionary(dictionary); + spellChecker = new SpellChecker(spellcheckerIndexDir); + } catch (IOException e) { + throw new RuntimeException("Cannot open SpellChecker index", e); + } + } + + public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) + throws Exception { + SolrParams p = req.getParams(); + String words = p.get("q"); + String cmd = p.get("cmd"); + if (cmd != null) { + cmd = cmd.trim(); + if (cmd.equals("rebuild")) { + rebuild(req); + rsp.add("cmdExecuted","rebuild"); + } else if (cmd.equals("reopen")) { reopen(); + rsp.add("cmdExecuted","reopen"); + } else { + throw new SolrException(400, "Unrecognized Command: " + cmd); + } } + + Float accuracy; + int numSug; + try { + accuracy = p.getFloat("accuracy", DEFAULT_ACCURACY); + spellChecker.setAccuracy(accuracy); + } catch (NumberFormatException e) { + throw new RuntimeException("Accuracy must be a valid positive float", e); + } + try { + numSug = p.getInt("suggestionCount", DEFAULT_NUM_SUGGESTIONS); + } catch (NumberFormatException e) { + throw new RuntimeException("Spelling suggestion count must be a valid positive integer", e); + } + + if (null != words && !"".equals(words.trim())) { + String[] suggestions = + spellChecker.suggestSimilar(words, numSug, + nullReader, restrictToField, + onlyMorePopular); + + rsp.add("suggestions", Arrays.asList(suggestions)); + } + } + + /** Rebuilds the SpellChecker index using values from the termSourceField from the + * index pointed to by the current {@link IndexSearcher}. + */ + private void rebuild(SolrQueryRequest req) throws IOException, SolrException { + if (null == termSourceField) { + throw new SolrException + (500, "can't rebuild spellchecker index without termSourceField configured"); + } + + IndexReader indexReader = req.getSearcher().getReader(); + Dictionary dictionary = new LuceneDictionary(indexReader, termSourceField); + spellChecker.indexDictionary(dictionary); + reopen(); + } - /** - * Reopens the SpellChecker index directory. - * Useful if an external process is responsible for building - * the spell checker index. - */ - private void reopen() throws IOException { - spellChecker.setSpellIndex(spellcheckerIndexDir); - } + /** + * Reopens the SpellChecker index directory. + * Useful if an external process is responsible for building + * the spell checker index. + */ + private void reopen() throws IOException { + spellChecker.setSpellIndex(spellcheckerIndexDir); + } - //////////////////////// SolrInfoMBeans methods ////////////////////// + //////////////////////// SolrInfoMBeans methods ////////////////////// - public String getVersion() { - return "$Revision$"; - } + public String getVersion() { + return "$Revision$"; + } - public String getDescription() { - return "The SpellChecker Solr request handler for SpellChecker index: " + dirDescription; - } + public String getDescription() { + return "The SpellChecker Solr request handler for SpellChecker index: " + dirDescription; + } - public String getSourceId() { - return "$Id$"; - } + public String getSourceId() { + return "$Id$"; + } - public String getSource() { - return "$URL$"; - } + public String getSource() { + return "$URL$"; + } - public URL[] getDocs() { - return null; - } + public URL[] getDocs() { + return null; + } }