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

1.程序宏观结构图

2.创建索引过程

3.代码实现

创建索引库:

1)  创建JavaBean对象

2)  创建Docment对象

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

4)  创建IndexWriter对象

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

6)  关闭IndexWriter对象

Jar包:

代码:

 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     }

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

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

时间: 2024-08-28 08:46:40

lucene入门创建索引——(一)的相关文章

lucene学习-创建索引

本文的lucene是基于lucene3.5版本. 使用lucene实现搜索引擎开发,核心的部分是建立索引和搜索.本节主要是记录创建索引部分的内容. 创建的索引结构如图所示. 创建索引的步骤分为以下几个步骤: 1.建立索引器IndexWriter 2.创建文档对象Document 3.建立信息对象字段Field 4.将Field对象添加到Document 5.将Document对象添加到IndexWriter对象中 下面简要介绍几个核心对象. (1).创建IndexWriter对象. IndexW

lucene之创建索引代码

public void createIndex() throws IOException { // 第一步采集数据:(jdbc采集数据) BookDao dao = new BookDaoImpl(); List<Book> queryBookList = dao.queryBookList(); // 将数据采集放到docment对象中 Document doc = null; List<Document> docList = new ArrayList<>(); f

搜索引擎系列 ---lucene简介 创建索引和搜索初步

一.什么是Lucene? Lucene最初是由Doug Cutting开发的,2000年3月,发布第一个版本,是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎 :Lucene得名于Doug妻子的中名,同时这也她外祖母的姓;目前是Apache基金会的一个顶级项目,同时也是学习搜索引擎入门必知必会. Lucene 是一个 JAVA 搜索类库,它本身并不是一个完整的解决方案,需要额外的开发工作. 优点:成熟的解决方案,有很多的成功案例.apache 顶级项目,正在持续快速的进步.庞大而活跃的开

搜索引擎系列 -lucene简介 创建索引和搜索初步步骤

一.什么是Lucene? Lucene最初是由Doug Cutting开发的,2000年3月,发布第一个版本,是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎 :Lucene得名于Doug妻子的中名,同时这也她外祖母的姓;目前是Apache基金会的一个顶级项目,同时也是学习搜索引擎入门必知必会. Lucene 是一个 JAVA 搜索类库,它本身并不是一个完整的解决方案,需要额外的开发工作. 优点:成熟的解决方案,有很多的成功案例.apache 顶级项目,正在持续快速的进步.庞大而活跃的开

lucene入门查询索引——(三)

1.用户接口(lucene不提供) 2.创建查询 3.执行查询 4.渲染结果: 5.过程分析 根据关键字查询索引库中的内容: 1)  创建IndexSearcher对象 2)  创建QueryParser对象 3)  创建Query对象来封装关键字 4)  用IndexSearcher对象去索引库中查询符合条件的前100条记录,不足100条记录的以实际为准 5)  获取符合条件的编号 6)  用indexSearcher对象去索引库中查询编号对应的Document对象 7)  将Document

lucene中创建索引库

package com.hope.lucene; import org.apache.commons.io.FileUtils;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.document.TextField;import org.apache.lucene.index.IndexWriter;import org.apach

一、创建索引之代码开发

jar包: Lucene包: lucene-core-4.10.3.jar lucene-analyzers-common-4.10.3.jar lucene-queryparser-4.10.3.jar 其它: commons-io-2.4.jar junit-4.9.jar package com.itheima.lucene; import java.io.File; import java.io.IOException; import org.apache.commons.io.File

全文检索之lucene的优化篇--创建索引库

在上一篇HelloWorld的基础上,建立一个directory的包,添加一个DirectoryTest的测试类,用来根据指定的索引目录创建目录存放指引. DirectoryTest类中的代码如下,基本上就是在HelloWorld的基础上改改就可以了. 里面一共三个方法,testDirectory(),测试创建索引库;testDirectoryFSAndRAM(),结合方法1的两种创建方式,优化;testDirectoryOptimize(),在方法2个基础上,研究索引的优化创建,减少创建的索引

Lucene入门程序-Java API的简单使用

Lucene入门程序 准备环境 JDK: 1.8.0_162 IDE: Eclipse Neon.3 数据库: MySQL 5.7.20 Lucene: 4.10.4(已经很稳定了,高版本对部分分词器支持不好) 准备数据 SET FOREIGN_KEY_CHECKS=0; -------------------------------- Table structure for `book` -------------------------------- DROP TABLE IF EXISTS