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

项目结构:

效果图:

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

建立好的所有文件

搜索关键字"lucene"信息

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

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

代码部分

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

准备工作:

下载lucene-3.5.0.zip

下载地址http://archive.apache.org/dist/lucene/java/3.5.0/

下载完后,解压缩,可以得到:

lucene-core-3.5.0.jar

junit-4.7.jar

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

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

  1 /**
  2  * 
  3  */
  4 package com.b510.lucene;
  5 
  6 import java.io.File;
  7 import java.io.FileReader;
  8 import java.io.IOException;
  9 
 10 import 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;
 27 
 28 /**
 29  * Lucene create Index and search key word
 30  * 
 31  * @author Hongten (hongtenzone@foxmail.com) <br />
 32  * @date 2012-11-28
 33  */
 34 public class LuceneIndex {
 35 
 36     /**
 37      * 创建索引
 38      */
 39     public void index() {
 40         IndexWriter writer = null;
 41         try {
 42             // 1.创建Directory
 43             // 这种方式是建立在内存中
 44             // Directory directory = new RAMDirectory();
 45             // 这种方式是存放在硬盘中
 46             Directory directory = FSDirectory.open(new File(
 47                     "D:/WordPlace/lucene/lucene_0100_helloworld/lucene/index"));
 48             // 2.创建IndexWriter
 49             IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_35,
 50                     new StandardAnalyzer(Version.LUCENE_35));
 51 
 52             writer = new IndexWriter(directory, iwc);
 53             // 3.创建Document对象
 54             Document doc = null;
 55             // 4.为Document添加Field
 56             File f = new File(
 57                     "D:/WordPlace/lucene/lucene_0100_helloworld/lucene/example");
 58             for (File file : f.listFiles()) {
 59                 doc = new Document();
 60                 doc.add(new Field("content", new FileReader(file)));
 61                 doc.add(new Field("filename", file.getName(), Field.Store.YES,
 62                         Field.Index.NOT_ANALYZED));
 63                 doc.add(new Field("path", file.getAbsolutePath(),
 64                         Field.Store.YES, Field.Index.NOT_ANALYZED));
 65                 // 5.通过IndexWriter添加文档到索引中
 66                 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 {
 75             try {
 76                 if (writer != null) {
 77                     writer.close();
 78                 }
 79             } catch (CorruptIndexException e2) {
 80                 e2.printStackTrace();
 81             } catch (IOException ioe) {
 82                 ioe.printStackTrace();
 83             }
 84         }
 85     }
 86 
 87     /**
 88      * 搜索关键字为key的n条记录
 89      * 
 90      * @param key
 91      *            关键字
 92      * @param n
 93      *            搜索的记录数
 94      */
 95     public void search(String key, int n) {
 96         try {
 97             // 1.创建Directory
 98             Directory directory = FSDirectory.open(new File(
 99                     "D:/WordPlace/lucene/lucene_0100_helloworld/lucene/index"));
100             // 2.创建IndexReader
101             IndexReader reader = IndexReader.open(directory);
102             // 3.根据IndexReader创建IndexSearcher
103             IndexSearcher searcher = new IndexSearcher(reader);
104             // 4.创建搜索的Query
105             // 创建parser来确定要搜索的文件的内容,第二个参数表示搜索的域
106             QueryParser parser = new QueryParser(Version.LUCENE_35, "content",
107                     new StandardAnalyzer(Version.LUCENE_35));
108             // 创建query,表示搜索域为content中包含key的文档
109             Query query = parser.parse(key);
110             // 5.根据searcher搜索并返回TopDocs
111             TopDocs tds = searcher.search(query, n);
112             // 6.根据TopDocs获取ScoreDoc对象
113             ScoreDoc[] sds = tds.scoreDocs;
114             for (ScoreDoc sd : sds) {
115                 // 7.根据searcher和ScordDoc对象获取具体的Document对象
116                 Document document = searcher.doc(sd.doc);
117                 // 8.根据Document对象获取需要的值
118                 System.out.println("文件名称:[" + document.get("filename")
119                         + "]  文件路径:[" + document.get("path") + "]");
120             }
121 
122             // 9.关闭reader
123             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_0100_helloworld/src/com/b510/lucene/LuceneIndexTest.java

 1 /**
 2  * 
 3  */
 4 package com.b510.lucene;
 5 
 6 import org.junit.Test;
 7 
 8 /**
 9  * @author Hongten (hongtenzone@foxmail.com) <br />
10  * @date 2012-11-28
11  */
12 public class LuceneIndexTest {
13 
14     @Test
15     public final void testIndex() {
16         LuceneIndex luceneIndex=  new LuceneIndex();
17         luceneIndex.index();
18     }
19     
20     @Test
21     public final void testSearch(){
22         LuceneIndex luceneIndex=  new LuceneIndex();
23         luceneIndex.search("lucene", 10);
24     }
25 
26 }

 

项目源码下载地址https://files.cnblogs.com/hongten/lucene_0100_helloworld.zip

posted @ 2012-11-28 10:29  Hongten  阅读(4267)  评论(0编辑  收藏  举报
Fork me on GitHub