lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mikemcc...@apache.org
Subject svn commit: r793526 - in /lucene/java/trunk/contrib: CHANGES.txt lucli/src/java/lucli/LuceneMethods.java lucli/src/java/lucli/Lucli.java
Date Mon, 13 Jul 2009 10:06:02 GMT
Author: mikemccand
Date: Mon Jul 13 10:06:01 2009
New Revision: 793526

URL: http://svn.apache.org/viewvc?rev=793526&view=rev
Log:
LUCENE-1740: add 'analyzer' command to Lucli, to change analyzer from the default StandardAnalyzer

Modified:
    lucene/java/trunk/contrib/CHANGES.txt
    lucene/java/trunk/contrib/lucli/src/java/lucli/LuceneMethods.java
    lucene/java/trunk/contrib/lucli/src/java/lucli/Lucli.java

Modified: lucene/java/trunk/contrib/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/java/trunk/contrib/CHANGES.txt?rev=793526&r1=793525&r2=793526&view=diff
==============================================================================
--- lucene/java/trunk/contrib/CHANGES.txt (original)
+++ lucene/java/trunk/contrib/CHANGES.txt Mon Jul 13 10:06:01 2009
@@ -68,6 +68,10 @@
 
  8. LUCENE-1522: Added contrib/fast-vector-highlighter, a new alternative
     highlighter.  (Koji Sekiguchi via Mike McCandless)
+
+ 9. LUCENE-1740: Added "analyzer" command to Lucli, enabling changing
+    the analyzer from the default StandardAnalyzer.  (Bernd Fondermann
+    via Mike McCandless)
  
 Optimizations
 

Modified: lucene/java/trunk/contrib/lucli/src/java/lucli/LuceneMethods.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/contrib/lucli/src/java/lucli/LuceneMethods.java?rev=793526&r1=793525&r2=793526&view=diff
==============================================================================
--- lucene/java/trunk/contrib/lucli/src/java/lucli/LuceneMethods.java (original)
+++ lucene/java/trunk/contrib/lucli/src/java/lucli/LuceneMethods.java Mon Jul 13 10:06:01
2009
@@ -104,12 +104,29 @@
   private String fieldsArray[]; //Fields as an array
   private Searcher searcher;
   private Query query; //current query string
+  private String analyzerClassFQN = null; // Analyzer class, if NULL, use default Analyzer
 
   public LuceneMethods(String index) {
     indexName = index;
     message("Lucene CLI. Using directory '" + indexName + "'. Type 'help' for instructions.");
   }
 
+    private Analyzer createAnalyzer() {
+        if (analyzerClassFQN == null) return new StandardAnalyzer();
+        try {
+            Class aClass = Class.forName(analyzerClassFQN);
+            Object obj = aClass.newInstance();
+            if (!(obj instanceof Analyzer)) {
+                message("Given class is not an Analyzer: " + analyzerClassFQN);
+                return new StandardAnalyzer();
+            }
+            return (Analyzer)obj;
+        } catch (Exception e) {
+            message("Unable to use Analyzer " + analyzerClassFQN);
+            return new StandardAnalyzer();
+        }
+    }
+
 
   public void info() throws java.io.IOException {
     IndexReader indexReader = IndexReader.open(indexName);
@@ -185,9 +202,9 @@
     //another option is to just do message(doc);
   }
 
-  public void optimize() throws IOException {
+    public void optimize() throws IOException {
     //open the index writer. False: don't create a new one
-    IndexWriter indexWriter = new IndexWriter(indexName, new StandardAnalyzer(), false);
+    IndexWriter indexWriter = new IndexWriter(indexName, createAnalyzer(), false);
     message("Starting to optimize index.");
     long start = System.currentTimeMillis();
     indexWriter.optimize();
@@ -196,10 +213,10 @@
   }
 
 
-  private Query explainQuery(String queryString) throws IOException, ParseException {
+    private Query explainQuery(String queryString) throws IOException, ParseException {
 
     searcher = new IndexSearcher(indexName);
-    Analyzer analyzer = new StandardAnalyzer();
+    Analyzer analyzer = createAnalyzer();
     getFieldInfo();
 
     int arraySize = indexedFields.size();
@@ -220,7 +237,7 @@
   private Hits initSearch(String queryString) throws IOException, ParseException {
 
     searcher = new IndexSearcher(indexName);
-    Analyzer analyzer = new StandardAnalyzer();
+    Analyzer analyzer = createAnalyzer();
     getFieldInfo();
 
     int arraySize = fields.size();
@@ -278,7 +295,7 @@
     Map tokenMap = new HashMap();
     final int maxFieldLength = 10000;
 
-    Analyzer analyzer = new StandardAnalyzer();
+    Analyzer analyzer = createAnalyzer();
     Iterator fields = doc.getFields().iterator();
     final Token reusableToken = new Token();
     while (fields.hasNext()) {
@@ -374,5 +391,14 @@
     return entries;
   }
 
+    public void analyzer(String word) {
+        if ("current".equals(word)) {
+            String current = analyzerClassFQN == null ? "StandardAnalyzer" : analyzerClassFQN;
+            message("The currently used Analyzer class is: " + current);
+            return;
+        }
+        analyzerClassFQN = word;
+        message("Switched to Analyzer class " + analyzerClassFQN);
+    }
 }
 

Modified: lucene/java/trunk/contrib/lucli/src/java/lucli/Lucli.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/contrib/lucli/src/java/lucli/Lucli.java?rev=793526&r1=793525&r2=793526&view=diff
==============================================================================
--- lucene/java/trunk/contrib/lucli/src/java/lucli/Lucli.java (original)
+++ lucene/java/trunk/contrib/lucli/src/java/lucli/Lucli.java Mon Jul 13 10:06:01 2009
@@ -95,6 +95,7 @@
 	final static int INDEX = 7;
 	final static int TOKENS = 8;
 	final static int EXPLAIN = 9;
+	final static int ANALYZER = 10;
 
 	String historyFile;
 	TreeMap commandMap = new TreeMap();
@@ -124,6 +125,7 @@
 		addCommand("index", INDEX, "Choose a different lucene index. Example index my_index", 1);
 		addCommand("tokens", TOKENS, "Does a search and shows the top 10 tokens for each document.
Verbose! Example: tokens foo", 1);
 		addCommand("explain", EXPLAIN, "Explanation that describes how the document scored against
query. Example: explain foo", 1);
+		addCommand("analyzer", ANALYZER, "Specifies the Analyzer class to be used. Example: analyzer
org.apache.lucene.analysis.SimpleAnalyzer", 1);
 
 		//parse command line arguments
 		parseArgs(args);
@@ -234,6 +236,9 @@
 				}
 				luceneMethods.search(query, true, false, cr);
 				break;
+			case ANALYZER:
+				luceneMethods.analyzer(words[1]);
+				break;
 			case HELP:
 				help();
 				break;



Mime
View raw message