3.2节我们已经运行了一个Lucene建立索引的小程序,这一节我们就以这个小程序为例讲解一下Lucene建立索引的过程。

 1 import java.nio.charset.StandardCharsets;
 2 import java.nio.file.Files;
 3 import java.nio.file.Paths;
 4 import java.io.*;
 5 
 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.document.StringField;
10 import org.apache.lucene.document.TextField;
11 import org.apache.lucene.index.IndexWriter;
12 import org.apache.lucene.index.IndexWriterConfig;
13 import org.apache.lucene.store.Directory;
14 import org.apache.lucene.store.FSDirectory;
15 import org.apache.lucene.util.Version;
16 
17 /**
18  * @author csl
19  * @description: 
20  * 依赖jar:Lucene-core,lucene-analyzers-common,lucene-queryparser
21  * 作用:简单的索引建立
22  */
23 public class Indexer {
24     public static Version luceneVersion = Version.LATEST;
25     /**
26      * 建立索引
27      */
28     public static void createIndex(){
29         IndexWriter writer = null;
30         try{
31             //1、创建Directory
32             //Directory directory = new RAMDirectory();//创建内存directory
33             Directory directory = FSDirectory.open(Paths.get("index"));//在硬盘上生成Directory00
34             //2、创建IndexWriter
35             IndexWriterConfig iwConfig = new IndexWriterConfig( new StandardAnalyzer());
36             writer = new IndexWriter(directory, iwConfig);
37             //3、创建document对象
38             Document document = null;
39             //4、为document添加field对象
40             File f = new File("raw");//索引源文件位置
41             for (File file:f.listFiles()){
42                     document = new Document();
43                     document.add(new StringField("path", f.getName(),Field.Store.YES));
44                     System.out.println(file.getName());
45                     document.add(new StringField("name", file.getName(),Field.Store.YES));
46                     InputStream stream = Files.newInputStream(Paths.get(file.toString()));
47                     document.add(new TextField("content", new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))));//textField内容会进行分词
48                     //document.add(new TextField("content", new FileReader(file)));  如果不用utf-8编码的话直接用这个就可以了
49                     writer.addDocument(document);
50             }
51         }catch(Exception e){
52             e.printStackTrace();
53         }finally{
54             //6、使用完成后需要将writer进行关闭
55             try {
56                 writer.close();
57             } catch (IOException e) {
58                 e.printStackTrace();
59             }
60         }
61     }
62     public static void main(String[] args) throws IOException
63     {
64         createIndex();
65     }
66 }
View Code

相关文章:

  • 2021-07-17
  • 2022-01-14
  • 2021-10-26
  • 2022-02-04
  • 2021-11-02
  • 2022-01-05
  • 2022-12-23
  • 2021-12-12
猜你喜欢
  • 2021-11-08
  • 2021-10-31
  • 2021-05-02
  • 2021-06-03
  • 2021-10-07
  • 2021-05-03
相关资源
相似解决方案