You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-commits@lucene.apache.org by ot...@apache.org on 2007/03/16 21:48:44 UTC

svn commit: r519140 - /lucene/solr/trunk/src/java/org/apache/solr/request/SpellCheckerRequestHandler.java

Author: otis
Date: Fri Mar 16 13:48:43 2007
New Revision: 519140

URL: http://svn.apache.org/viewvc?view=rev&rev=519140
Log:
- SOLR-81: the correct SpellCheckerRequestHandler.java this time

Modified:
    lucene/solr/trunk/src/java/org/apache/solr/request/SpellCheckerRequestHandler.java

Modified: lucene/solr/trunk/src/java/org/apache/solr/request/SpellCheckerRequestHandler.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/java/org/apache/solr/request/SpellCheckerRequestHandler.java?view=diff&rev=519140&r1=519139&r2=519140
==============================================================================
--- lucene/solr/trunk/src/java/org/apache/solr/request/SpellCheckerRequestHandler.java (original)
+++ lucene/solr/trunk/src/java/org/apache/solr/request/SpellCheckerRequestHandler.java Fri Mar 16 13:48:43 2007
@@ -18,6 +18,8 @@
 package org.apache.solr.request;
 
 import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.search.spell.Dictionary;
+import org.apache.lucene.search.spell.LuceneDictionary;
 import org.apache.lucene.search.spell.SpellChecker;
 import org.apache.lucene.store.FSDirectory;
 import org.apache.solr.core.SolrCore;
@@ -38,7 +40,7 @@
  */
 public class SpellCheckerRequestHandler extends RequestHandlerBase {
 
-    private static SpellChecker spellChecker;
+    private SpellChecker spellChecker;
 
     /*
      * From http://wiki.apache.org/jakarta-lucene/SpellChecker
@@ -58,10 +60,15 @@
     private boolean onlyMorePopular = false;
 
     private String spellcheckerIndexDir;
-
+    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 );
-        spellcheckerIndexDir = invariants.get("spellcheckerIndexDir");
+        super.init(args);
+        SolrParams p = SolrParams.toSolrParams(args);
+        restrictToField = p.get("termSourceField");
+        spellcheckerIndexDir = p.get("spellcheckerIndexDir");
         try {
             spellChecker = new SpellChecker(FSDirectory.getDirectory(spellcheckerIndexDir));
         } catch (IOException e) {
@@ -73,13 +80,38 @@
             throws Exception {
         SolrParams p = req.getParams();
         String words = p.get("q");
+        String cmd = p.get("cmd");
+        if (cmd != null && cmd.equals("rebuild"))
+            rebuild(req);
+
+        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);
+        }
 
-        System.out.println(getDescription());
-        int numSug = 5;
         String[] suggestions = spellChecker.suggestSimilar(words, numSug,
                 reader, restrictToField, onlyMorePopular);
 
         rsp.add("suggestions", Arrays.asList(suggestions));
+    }
+
+    /** Rebuilds the SpellChecker index using values from the <code>termSourceField</code> from the
+     * index pointed to by the current {@link IndexSearcher}.
+     */
+    private void rebuild(SolrQueryRequest req) throws IOException {
+        IndexReader indexReader = req.getSearcher().getReader();
+        Dictionary dictionary = new LuceneDictionary(indexReader, termSourceField);
+        spellChecker.indexDictionary(dictionary);
+        spellChecker.setSpellIndex(FSDirectory.getDirectory(spellcheckerIndexDir));
     }
 
     //////////////////////// SolrInfoMBeans methods //////////////////////