lucene(全文搜索)_建立索引_根据关键字全文搜索_源码下载

项目结构:

效果图:

需要建立索引的文件(我们需要从中查找出关键字的文档)

建立好的所有文件

搜索关键字”lucene”信息

大家是不是也想亲自动手尝试一下呢…

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

代码部分

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

准备工作:

下载:lucene-3.5.0.zip

下载地址:

下载完后,香港服务器,美国空间,解压缩,香港空间,可以得到:

lucene-core-3.5.0.jar

junit-4.7.jar

把这两个jar包加入到项目构建路径下面…看看—–>项目结构

/lucene_0100_helloworld/src/com/b510/lucene/LuceneIndex.java

* com.b510.lucene; java.io.File; 7 import java.io.FileReader; 8 import java.io.IOException; 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.CorruptIndexException; 14 import org.apache.lucene.index.IndexReader; 15 import org.apache.lucene.index.IndexWriter; 16 import org.apache.lucene.index.IndexWriterConfig; 17 import org.apache.lucene.queryParser.ParseException; 18 import org.apache.lucene.queryParser.QueryParser; 19 import org.apache.lucene.search.IndexSearcher; 20 import org.apache.lucene.search.Query; 21 import org.apache.lucene.search.ScoreDoc; 22 import org.apache.lucene.search.TopDocs; 23 import org.apache.lucene.store.Directory; 24 import org.apache.lucene.store.FSDirectory; 25 import org.apache.lucene.store.LockObtainFailedException; 26 import org.apache.lucene.util.Version; * Lucene create Index and search key word 30 * Hongten (hongtenzone@foxmail.com) <br /> 32 * @date 2012-11-28 LuceneIndex {* 创建索引 index() { 40IndexWriter writer = null; 41try { 42// 1.创建Directory 43// 这种方式是建立在内存中 44// Directory directory = new RAMDirectory();Directory directory = FSDirectory.open(new File( 47″D:/WordPlace/lucene/lucene_0100_helloworld/lucene/index”));IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_35, 50new StandardAnalyzer(Version.LUCENE_35)); 51 52writer = new IndexWriter(directory, iwc);Document doc = null;File f = new File( 57″D:/WordPlace/lucene/lucene_0100_helloworld/lucene/example”); 58for (File file : f.listFiles()) { 59doc = new Document(); 60doc.add(new Field(“content”, new FileReader(file))); 61doc.add(new Field(“filename”, file.getName(), Field.Store.YES, 62 Field.Index.NOT_ANALYZED)); 63doc.add(new Field(“path”, file.getAbsolutePath(), 64 Field.Store.YES, Field.Index.NOT_ANALYZED));writer.addDocument(doc); 67 } 68} catch (CorruptIndexException ce) { 69 ce.printStackTrace(); 70} catch (LockObtainFailedException e) { 71 e.printStackTrace(); 72} catch (IOException e) { 73 e.printStackTrace(); 74} finally { 75try { 76if (writer != null) { 77 writer.close(); 78 } 79} catch (CorruptIndexException e2) { 80 e2.printStackTrace(); 81} catch (IOException ioe) { 82 ioe.printStackTrace(); 83 } 84 } 85 }* 搜索关键字为key的n条记录 89 * key 91 *关键字 n 93 *搜索的记录数search(String key, int n) { 96try {Directory directory = FSDirectory.open(new File( 99″D:/WordPlace/lucene/lucene_0100_helloworld/lucene/index”));IndexReader reader = IndexReader.open(directory);IndexSearcher searcher = new IndexSearcher(reader);104// 4.创建搜索的QueryQueryParser parser = new QueryParser(Version.LUCENE_35, “content”,107new StandardAnalyzer(Version.LUCENE_35));Query query = parser.parse(key);TopDocs tds = searcher.search(query, n);ScoreDoc[] sds = tds.scoreDocs;114for (ScoreDoc sd : sds) {Document document = searcher.doc(sd.doc);System.out.println(“文件名称:[” + document.get(“filename”)119+ “] 文件路径:[” + document.get(“path”) + “]”);120 }reader.close();124} catch (CorruptIndexException e) {125 e.printStackTrace();126} catch (IOException e) {127 e.printStackTrace();128} catch (ParseException e) {129 e.printStackTrace();130 }131 }132 133 }天再高又怎样,踮起脚尖就更接近阳光。

lucene(全文搜索)_建立索引_根据关键字全文搜索_源码下载

相关文章:

你感兴趣的文章:

标签云: