Lucene_Hello(示例)

(1)创建project

(2)导入Lucene的核心包

(3)编写代码建立索引

/lucene01/src/cn/hk/lucene/TestIndex.java:

 1 package cn.hk.lucene;
 2 
 3 import java.io.File;
 4 import java.io.FileReader;
 5 import java.io.IOException;
 6 import org.apache.lucene.analysis.standard.StandardAnalyzer;
 7 import org.apache.lucene.document.Document;
 8 import org.apache.lucene.document.Field;
 9 import org.apache.lucene.index.CorruptIndexException;
10 import org.apache.lucene.index.IndexWriter;
11 import org.apache.lucene.index.IndexWriterConfig;
12 import org.apache.lucene.store.Directory;
13 import org.apache.lucene.store.FSDirectory;
14 import org.apache.lucene.util.Version;
15 
16 /**
17  * 生成索引
18  *
19  */
20 public class TestIndex {
21     public static void main(String[] args) {
22         IndexWriter writer = null;
23         
24         try {
25             //1、创建Directory
26             //在内存中创建
27             //Directory directory = new RAMDirectory();
28             //在磁盘中创建
29             Directory directory = FSDirectory.open(new File("e:\\lucene"));            
30             
31             //2、创建IndexWriter
32             IndexWriterConfig config = new IndexWriterConfig(
33                     Version.LUCENE_35,
34                     new StandardAnalyzer(Version.LUCENE_35));
35             
36             writer = new IndexWriter(directory,config);
37             
38             //3、创建Document
39             Document doc = null;
40             
41             //获取files下的所有文件
42             File list = new File("e:\\lucene\\files");
43             
44             //4、添加Field属性
45             //遍历获取到的文件的集合
46             for(File file : list.listFiles()){
47                 //实例化文档对象
48                 doc = new Document();
49                 //存储文件的名称信息(对名称进行存储但不分析)
50                 doc.add(new Field("filename",file.getName(),Field.Store.YES, Field.Index.NOT_ANALYZED));
51                 doc.add(new Field("path",file.getAbsolutePath(),Field.Store.YES,Field.Index.NOT_ANALYZED));
52                 
53                 doc.add(new Field("content",new FileReader(file)));
54                 
55                 //将document添加到writer中
56                 writer.addDocument(doc);
57             }
58             
59         } catch (IOException e) {
60             e.printStackTrace();
61         }
62         finally{
63             if(writer != null){
64                 try {
65                     writer.close();
66                 } catch (CorruptIndexException e) {
67                     // TODO Auto-generated catch block
68                     e.printStackTrace();
69                 } catch (IOException e) {
70                     // TODO Auto-generated catch block
71                     e.printStackTrace();
72                 }
73             }
74         }
75     }
76 }

/lucene01/src/cn/hk/lucene/TestSearch.java:

 1 package cn.hk.lucene;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 import org.apache.lucene.analysis.standard.StandardAnalyzer;
 6 import org.apache.lucene.document.Document;
 7 import org.apache.lucene.index.IndexReader;
 8 import org.apache.lucene.queryParser.ParseException;
 9 import org.apache.lucene.queryParser.QueryParser;
10 import org.apache.lucene.search.IndexSearcher;
11 import org.apache.lucene.search.Query;
12 import org.apache.lucene.search.ScoreDoc;
13 import org.apache.lucene.search.TopDocs;
14 import org.apache.lucene.store.Directory;
15 import org.apache.lucene.store.FSDirectory;
16 import org.apache.lucene.util.Version;
17 
18 /**
19  * 检索
20  *
21  */
22 public class TestSearch {
23     public static void main(String[] args) {
24         IndexReader reader =null;
25         try {
26             //1、创建Dierctory
27             Directory directory = FSDirectory.open(new File("e:\\lucene"));
28 
29             //2、创建IndexReader
30             reader = IndexReader.open(directory);
31             
32             //3、创建Searcher
33             IndexSearcher searcher = new IndexSearcher(reader);
34             
35             //4、创建Query
36             //参数:匹配器的版本、查询的属性、分析器
37             QueryParser parser = new QueryParser(Version.LUCENE_35,
38                     "content",
39                     new StandardAnalyzer(Version.LUCENE_35));
40             
41             //查询的条件(相当于数据库中的where部分)
42             Query query = parser.parse("users");
43             
44             //5、创建TopDocs
45             //使用查询器进行查询(条件,查询次数)
46             TopDocs docs = searcher.search(query, 20);
47             
48             //6、根据TopDocs获取ScoreDoc
49             ScoreDoc[] sds =  docs.scoreDocs;
50             
51             //7、根据searcher和ScoreDoc获取具体的document(分词所出现的文档)对象
52             for(ScoreDoc sd : sds){
53                 //表示分词所在的文档对象
54                 Document doc = searcher.doc(sd.doc);
55                 
56                 //输出文档的信息
57                 System.out.println(doc.get("filename") + " | " + doc.get("path"));
58             }
59             
60         } catch (IOException | ParseException e) {
61             // TODO Auto-generated catch block
62             e.printStackTrace();
63         }            
64         finally{
65             if(reader != null){
66                 try {
67                     reader.close();
68                 } catch (IOException e) {
69                     // TODO Auto-generated catch block
70                     e.printStackTrace();
71                 }
72             }
73         }
74         
75     }
76 }

猜你喜欢

转载自www.cnblogs.com/zhzcode/p/9783424.html
今日推荐