lucene in action_index and search

在这里给大家来一些关于lucene in action的一些东东

你可以到:了解更新,更全的关于lucene的信息。

下面我做的demo,分享给大家:

项目结构:

运行Index时,美国空间,Run Configurations:

[两个参数之间有空格]

args[0] = “C:\Users\hongjie\Desktop\hongten_temp\hongten_index”;

args[1] = “C:\Users\hongjie\Desktop\hongten_temp\lucene-3.5.0”;

运行效果:

运行Searcher时,Run Configurations:

[两个参数之间有空格]

args[0] = “C:\Users\hongjie\Desktop\hongten_temp\hongten_index”;

args[1] = “cnhances”;

运行效果:

============================================================

代码部分:

============================================================

/lucene/src/com/b510/index/Indexer.java

* com.b510.index; java.io.File; 7 import java.io.FileFilter; 8 import java.io.FileReader; org.apache.lucene.analysis.standard.StandardAnalyzer; 11 import org.apache.lucene.document.Document; 12 import org.apache.lucene.document.Field; 13 import org.apache.lucene.index.IndexWriter; 14 import org.apache.lucene.store.Directory; 15 import org.apache.lucene.store.FSDirectory; 16 import org.apache.lucene.util.Version; * 建立索引 20 * hongten(hongtenzone@foxmail.com)<br> 22 * @date 2013-4-5 Indexer {main(String[] args) throws Exception { 27if (args.length != 2) {IllegalArgumentException(“Usage: java ” + Indexer.class.getName() + ” <index dir><data dir>”); 29 }String indexDir = args[0]; 32String dataDir = args[1];start = System.currentTimeMillis(); 35Indexer index = new Indexer(indexDir); 36int numIndexed; 37try { 38numIndexed = index.index(dataDir, new TextFilesFilter()); 39} finally { 40 index.close(); 41 } 42long end = System.currentTimeMillis(); 43 44System.out.println(“indexing ” + numIndexed + ” files took ” + (end – start) + ” millseconds!”); 45 } IndexWriter writer; 48 49@SuppressWarnings(“deprecation”) 50public Indexer(String indexDir) throws Exception { 51Directory dir = FSDirectory.open(new File(indexDir));writer = new IndexWriter(dir, new StandardAnalyzer(Version.LUCENE_35), true, IndexWriter.MaxFieldLength.UNLIMITED); 54 }* close the IndexWriter 58 * Exceptionclose() throws Exception { 62 writer.close(); 63 }* 返回被索引文件数 67 * dataDir 69 *文件目录 filter Exceptionindex(String dataDir, FileFilter filter) throws Exception { 75File[] files = new File(dataDir).listFiles(); (File f : files) { 78if (!f.isDirectory() && !f.isHidden() && f.exists() && f.canRead() && (filter == null || filter.accept(f))) { 79 indexFile(f); 80 } 81 } writer.numDocs(); 84 }* 向lucene中添加文档 88 * f 90 *文档 ExceptionindexFile(File f) throws Exception { 94System.out.println(“Indexing ” + f.getCanonicalPath()); 95Document doc = getDocument(f); 96 writer.addDocument(doc); 97 }* 添加索引101 * f103 *被索引的文件 ExceptionDocument getDocument(File f) throws Exception {108Document doc = new Document();109doc.add(new Field(“contents”, new FileReader(f)));110doc.add(new Field(“filename”, f.getName(), Field.Store.YES, Field.Index.NOT_ANALYZED));111doc.add(new Field(“fullpath”, f.getCanonicalPath(), Field.Store.YES, Field.Index.NOT_ANALYZED));112return doc;113 }* 只索引.txt文件,美国服务器,香港服务器,采用FileFilter117 * hongten(hongtenzone@foxmail.com)<br>119 * @date 2013-4-5TextFilesFilter implements FileFilter {122 @Override accept(File pathname) {124return pathname.getName().toLowerCase().endsWith(“.txt”);125 }126 }127 128 }人生不如意十之八-九,与其诅咒黑暗,倒不如在生命中点燃一盏灯

lucene in action_index and search

相关文章:

你感兴趣的文章:

标签云: