1.程序宏观结构图

lucene入门创建索引——(二)

 

2.创建索引过程

lucene入门创建索引——(二)

 

 

3.代码实现

创建索引库:

1)  创建JavaBean对象

2)  创建Docment对象

3)  将JavaBean对象所有的属性值,均放到Document对象中去,属性名可以和JavaBean相同或不同

4)  创建IndexWriter对象

5)  将Document对象通过IndexWriter对象写入索引库中

6)  关闭IndexWriter对象

 

Fled域属性

lucene入门创建索引——(二)

lucene入门创建索引——(二)

lucene入门创建索引——(二)

 

 

Jar包:

lucene入门创建索引——(二)

lucene入门创建索引——(二)

 

 

需要建立索引的文件:(对文件名字,大小,路径,内容进行存储,但是只对文件内容和名字建立索引)

lucene入门创建索引——(二)

 

代码:

 1 // 创建索引
 2     @Test
 3     public void testIndex() throws Exception {
 4         // 第一步:创建一个java工程,并导入jar包。
 5         // 第二步:创建一个indexwriter对象。
 6         Directory directory = FSDirectory.open(new File("E:\\lucene1\\index"));
 7         // Directory directory = new RAMDirectory();//保存索引到内存中 (内存索引库)
 8 //        Analyzer analyzer = new StandardAnalyzer();// 官方推荐
 9         Analyzer analyzer = new IKAnalyzer();// 官方推荐
10         IndexWriterConfig config = new IndexWriterConfig(Version.LATEST, analyzer);
11         IndexWriter indexWriter = new IndexWriter(directory, config);
12         // 1)指定索引库的存放位置Directory对象
13         // 2)指定一个分析器,对文档内容进行分析。
14         // 第三步:创建field对象,将field添加到document对象中。
15         File f = new File("E:\\lucene1\\searchfiles");
16         File[] listFiles = f.listFiles();
17         for (File file : listFiles) {
18             // 第三步:创建document对象。
19             Document document = new Document();
20             // 文件名称
21             String file_name = file.getName();
22             Field fileNameField = new TextField("fileName", file_name, Store.YES);
23             // 文件大小
24             long file_size = FileUtils.sizeOf(file);
25             Field fileSizeField = new LongField("fileSize", file_size, Store.YES);
26             // 文件路径
27             String file_path = file.getPath();
28             Field filePathField = new StoredField("filePath", file_path);
29             // 文件内容
30             String file_content = FileUtils.readFileToString(file);
31             Field fileContentField = new TextField("fileContent", file_content, Store.YES);
32 
33             document.add(fileNameField);
34             document.add(fileSizeField);
35             document.add(filePathField);
36             document.add(fileContentField);
37             // 第四步:使用indexwriter对象将document对象写入索引库,此过程进行索引创建。并将索引和document对象写入索引库。
38             indexWriter.addDocument(document);
39 
40         }
41         // 第五步:关闭IndexWriter对象。
42         indexWriter.close();
43     }

结果:至此创建索引完成,以后的搜索靠他们了。

lucene入门创建索引——(二)

4.luke可视化工具查看索引

lucene入门创建索引——(二)

 

lucene入门创建索引——(二)

lucene入门创建索引——(二)

 

查看存储的域信息

lucene入门创建索引——(二)

 

 

查看对文件名字建立的索引

lucene入门创建索引——(二)

相关文章:

  • 2021-04-22
  • 2021-11-07
  • 2021-11-07
  • 2021-06-06
  • 2021-11-04
  • 2021-11-11
  • 2021-11-14
  • 2021-12-02
猜你喜欢
  • 2021-06-10
  • 2021-11-03
  • 2021-09-10
  • 2021-07-15
  • 2021-07-17
  • 2021-10-27
  • 2021-05-03
  • 2021-08-31
相关资源
相似解决方案