import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
public class Exec {
public static void main(String[] args) {
DemoWindow dw = new DemoWindow("演示");
Toolkit theKit = dw.getToolkit();
Dimension wndSize = theKit.getScreenSize();
dw.setBounds(wndSize.width / 3, wndSize.height / 3, wndSize.width / 3,
wndSize.height / 3);
dw.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
dw.setVisible(true);
}
}
class DemoWindow extends JFrame implements ActionListener {
JTextArea jta = new JTextArea();
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp = new JScrollPane(jta, v, h);
JMenuBar menuBar = new JMenuBar();
JMenu index = new JMenu("建立索引");
JMenuItem indexSource = new JMenuItem("指定需要索引的目录");
JMenuItem indexDestination = new JMenuItem("指定存放索引的目录");
JMenuItem createIndex = new JMenuItem("建立索引");
JMenu search = new JMenu("搜索");
JMenuItem keywordsSearch = new JMenuItem("关键词检索");
Indexer indexer = null;
Searcher searcher = new Searcher();
String indexSourceStr = null;
String indexDestinationStr = null;
public DemoWindow(String title) {
super(title);
index.add(indexSource);
index.add(indexDestination);
index.addSeparator();
index.add(createIndex);
search.add(keywordsSearch);
menuBar.add(index);
menuBar.add(search);
setJMenuBar(menuBar);
indexDestination.setEnabled(false);
createIndex.setEnabled(false);
keywordsSearch.setEnabled(false);
indexSource.addActionListener(this);
indexDestination.addActionListener(this);
createIndex.addActionListener(this);
keywordsSearch.addActionListener(this);
add(jsp);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == indexSource) {
JFileChooser jfc = new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (jfc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
indexDestination.setEnabled(true);
indexSourceStr = jfc.getSelectedFile().getPath();
}
} else if (e.getSource() == indexDestination) {
JFileChooser jfc = new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (jfc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
createIndex.setEnabled(true);
indexDestinationStr = jfc.getSelectedFile().getPath();
}
} else if (e.getSource() == createIndex) {
indexer = new Indexer(indexSourceStr, indexDestinationStr);
Thread t = new Thread(new ProcessQuery(indexer, keywordsSearch));
t.start();
} else if (e.getSource() == keywordsSearch) {
String queryWords = JOptionPane.showInputDialog("请输入搜索词语:");
jta.setText(searcher.getResults(queryWords, indexDestinationStr));
}
}
}
class ProcessQuery implements Runnable {
Indexer indexer;
JMenuItem jmi;
public ProcessQuery(Indexer indexer, JMenuItem jmi) {
this.indexer = indexer;
this.jmi = jmi;
}
public void run() {
try {
indexer.start();
jmi.setEnabled(true);
} catch (Exception e) {
}
}
}
class Indexer {
String indexSourceStr;
String indexDestinationStr;
public Indexer(String indexSourceStr, String indexDestinationStr) {
this.indexSourceStr = indexSourceStr;
this.indexDestinationStr = indexDestinationStr;
}
public void start() {
try {
File dataDir = new File(indexSourceStr);
File indexDir = new File(indexDestinationStr);
index(indexDir, dataDir);
} catch (Exception e) {
e.printStackTrace();
}
}
public void index(File indexDir, File dataDir) throws IOException {
IndexWriter writer = new IndexWriter(indexDir, new StandardAnalyzer(),
true);
// indexDir表示索引的存放地点
// new StandardAnalyzer()表示分析器
// true表示如已有索引则覆盖之
writer.setUseCompoundFile(false);
// 表示生成多文件索引文件(默认为复合索引)
indexDirectory(writer, dataDir);
int numIndexed = writer.docCount();
writer.optimize();
writer.close();
}
private void indexDirectory(IndexWriter writer, File dir)
throws IOException {
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
File f = files;
if (f.isDirectory()) {
indexDirectory(writer, f); // recurse
} else if (f.getName().endsWith(".txt")) {
indexFile(writer, f);
}
}
}
private void indexFile(IndexWriter writer, File f) throws IOException {
if (f.isHidden() || !f.exists() || !f.canRead()) {
return;
}
System.out.println("Indexing " + f.getCanonicalPath());
// 建立索引关键代码
Document doc = new Document();
doc.add(Field.Text("contents", new FileReader(f)));
doc.add(Field.Keyword("filename", f.getCanonicalPath()));
writer.addDocument(doc);
}
}
class Searcher {
public String getResults(String queryWords, String indexDestinationStr) {
StringBuffer results = new StringBuffer();
try {
File indexDir = new File(indexDestinationStr);
Directory fsDir = FSDirectory.getDirectory(indexDir, false);
// 打开索引
IndexSearcher is = new IndexSearcher(fsDir);
// 解析查询词语
// Query query = new TermQuery(new Term("contents", "java"));
// Query query = new TermQuery(new Term("contents", "application"));
// Query query = new TermQuery(new Term("contents", "java AND NOT
// application"));
Query query = QueryParser.parse(queryWords, "contents",
new StandardAnalyzer());
// 搜索索引
Hits hits = is.search(query);
for (int i = 0; i < hits.length(); i++) {
Document doc = hits.doc(i);
results.append(doc.get("filename") + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
return results.toString();
}
}
[此贴子已经被作者于2010-12-14 09:28:50编辑过]