LUCENE-1740: add 'analyzer' command to Lucli, to change analyzer from the default StandardAnalyzer

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@793526 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2009-07-13 10:06:01 +00:00
parent a284608778
commit 91aedd6685
3 changed files with 41 additions and 6 deletions

View File

@ -68,6 +68,10 @@ New features
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

View File

@ -104,12 +104,29 @@ class LuceneMethods {
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 @@ class LuceneMethods {
//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 @@ class LuceneMethods {
}
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 @@ class LuceneMethods {
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 @@ class LuceneMethods {
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 @@ class LuceneMethods {
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);
}
}

View File

@ -95,6 +95,7 @@ public class Lucli {
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 @@ public class Lucli {
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 @@ public class Lucli {
}
luceneMethods.search(query, true, false, cr);
break;
case ANALYZER:
luceneMethods.analyzer(words[1]);
break;
case HELP:
help();
break;