Luclipublic class Lucli extends Object Main class for lucli: the Lucene Command Line Interface.
This class handles mostly the actual CLI part, command names, help, etc. |
Fields Summary |
---|
static final String | DEFAULT_INDEX | static final String | HISTORYFILE | public static final int | MAX_TERMS | static final int | NOCOMMAND | static final int | UNKOWN | static final int | INFO | static final int | SEARCH | static final int | OPTIMIZE | static final int | QUIT | static final int | HELP | static final int | COUNT | static final int | TERMS | static final int | INDEX | static final int | TOKENS | static final int | EXPLAIN | String | historyFile | TreeMap | commandMap | LuceneMethods | luceneMethods | boolean | enableReadline |
Constructors Summary |
---|
public Lucli(String[] args)Main entry point. The first argument can be a filename with an
application initialization file. //false: use plain java. True: shared library readline
String line;
historyFile = System.getProperty("user.home") + File.separator + HISTORYFILE;
/*
* Initialize the list of commands
*/
addCommand("info", INFO, "Display info about the current Lucene index. Example: info");
addCommand("search", SEARCH, "Search the current index. Example: search foo", 1);
addCommand("count", COUNT, "Return the number of hits for a search. Example: count foo", 1);
addCommand("optimize", OPTIMIZE, "Optimize the current index");
addCommand("quit", QUIT, "Quit/exit the program");
addCommand("help", HELP, "Display help about commands");
addCommand("terms", TERMS, "Show the first " + MAX_TERMS + " terms in this index. Supply a field name to only show terms in a specific field. Example: terms");
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);
//parse command line arguments
parseArgs(args);
ConsoleReader cr = new ConsoleReader();
//Readline.readHistoryFile(fullPath);
cr.setHistory(new History(new File(historyFile)));
// set completer with list of words
Completor[] comp = new Completor[]{
new SimpleCompletor(getCommandsAsArray()),
new FileNameCompletor()
};
cr.addCompletor (new ArgumentCompletor(comp));
// main input loop
luceneMethods = new LuceneMethods(DEFAULT_INDEX);
while (true) {
try {
line = cr.readLine("lucli> ");
if (line != null) {
handleCommand(line, cr);
}
} catch (java.io.EOFException eof) {
System.out.println("");//new line
exit();
} catch (UnsupportedEncodingException enc) {
enc.printStackTrace(System.err);
} catch (ParseException pe) {
pe.printStackTrace(System.err);
} catch (IOException ioe) {
ioe.printStackTrace(System.err);
}
}
|
Methods Summary |
---|
private void | addCommand(java.lang.String name, int id, java.lang.String help)Add a command to the list of commands for the interpreter for a
command that doesn't take any parameters.
addCommand(name, id, help, 0);
| private void | addCommand(java.lang.String name, int id, java.lang.String help, int params)Add a command to the list of commands for the interpreter.
Command command = new Command(name, id, help, params);
commandMap.put(name, command);
| private void | error(java.lang.String message)
System.err.println("Error:" + message);
| private void | exit()
System.exit(0);
| private int | getCommandId(java.lang.String name, int params)
name = name.toLowerCase(); //treat uppercase and lower case commands the same
Command command = (Command) commandMap.get(name);
if (command == null) {
return(UNKOWN);
}
else {
if(command.params > params) {
error(command.name + " needs at least " + command.params + " arguments.");
return (NOCOMMAND);
}
return (command.id);
}
| private java.lang.String[] | getCommandsAsArray()
Set commandSet = commandMap.keySet();
String[] commands = new String[commandMap.size()];
int i = 0;
for (Iterator iter = commandSet.iterator(); iter.hasNext();) {
String cmd = (String) iter.next();
commands[i++] = cmd;
}
return commands;
| private void | handleCommand(java.lang.String line, jline.ConsoleReader cr)
String [] words = tokenizeCommand(line);
if (words.length == 0)
return; //white space
String query = "";
if (line.trim().startsWith("#")) // # = comment
return;
//Command name and number of arguments
switch (getCommandId(words[0], words.length - 1)) {
case INFO:
luceneMethods.info();
break;
case SEARCH:
for (int ii = 1; ii < words.length; ii++) {
query += words[ii] + " ";
}
luceneMethods.search(query, false, false, cr);
break;
case COUNT:
for (int ii = 1; ii < words.length; ii++) {
query += words[ii] + " ";
}
luceneMethods.count(query);
break;
case QUIT:
exit();
break;
case TERMS:
if(words.length > 1)
luceneMethods.terms(words[1]);
else
luceneMethods.terms(null);
break;
case INDEX:
LuceneMethods newLm = new LuceneMethods(words[1]);
try {
newLm.info(); //will fail if can't open the index
luceneMethods = newLm; //OK, so we'll use the new one
} catch (IOException ioe) {
//problem we'll keep using the old one
error(ioe.toString());
}
break;
case OPTIMIZE:
luceneMethods.optimize();
break;
case TOKENS:
for (int ii = 1; ii < words.length; ii++) {
query += words[ii] + " ";
}
luceneMethods.search(query, false, true, cr);
break;
case EXPLAIN:
for (int ii = 1; ii < words.length; ii++) {
query += words[ii] + " ";
}
luceneMethods.search(query, true, false, cr);
break;
case HELP:
help();
break;
case NOCOMMAND: //do nothing
break;
case UNKOWN:
System.out.println("Unknown command: " + words[0] + ". Type help to get a list of commands.");
break;
}
| private void | help()
Iterator commands = commandMap.keySet().iterator();
while (commands.hasNext()) {
Command command = (Command) commandMap.get(commands.next());
System.out.println("\t" + command.name + ": " + command.help);
}
| public static void | main(java.lang.String[] args)
new Lucli(args);
| private void | message(java.lang.String text)
System.out.println(text);
| private void | parseArgs(java.lang.String[] args)
if (args.length > 0) {
usage();
System.exit(1);
}
| private java.lang.String[] | tokenizeCommand(java.lang.String line)
StringTokenizer tokenizer = new StringTokenizer(line, " \t");
int size = tokenizer.countTokens();
String [] tokens = new String[size];
for (int ii = 0; tokenizer.hasMoreTokens(); ii++) {
tokens[ii] = tokenizer.nextToken();
}
return tokens;
| private void | usage()
message("Usage: lucli.Lucli");
message("(currently, no parameters are supported)");
|
|