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

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

配置pom.xml, 导入依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 "> <modelVersion>4.0.0</modelVersion> <groupId>com.healchow</groupId> <artifactId>lucene-first</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>lucene-first</name> <url></url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- mysql版本 --> <mysql.version>5.1.44</mysql.version> <!-- lucene版本 --> <lucene.version>4.10.4</lucene.version> </properties> <dependencies> <!-- mysql数据库依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!-- lucene依赖包 --> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-core</artifactId> <version>${lucene.version}</version> </dependency> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-analyzers-common</artifactId> <version>${lucene.version}</version> </dependency> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-queryparser</artifactId> <version>${lucene.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> </project> 准备原始数据采集 准备图书POJO public class Book { private Integer id; // int(11) DEFAULT NULL, private String bookname; // varchar(500) DEFAULT NULL, private Float price; // float DEFAULT NULL, private String pic; // varchar(200) DEFAULT NULL, private String bookdesc; // varchar(2000) DEFAULT NULL // Getters/Setters @Override public String toString() { return "Book [id=" + id + ", bookname=" + bookname + ", price=" + price + ", pic=" + pic + ", bookdesc=" + bookdesc + "]"; } } 准备图书DAO接口 public interface BookDao { /** * 查询全部图书 */ List<Book> queryBookList(); } 实现图书DAO接口 public class BookDaoImpl implements BookDao { /** * 查询全部图书 */ public List<Book> listAll() { // 创建图书结果集合List List<Book> books = new ArrayList<Book>(); Connection conn = null; PreparedStatement preStatement = null; ResultSet resultSet = null; try { // 加载驱动 Class.forName("com.mysql.jdbc.Driver"); // 创建数据库连接对象 conn = DriverManager.getConnection( "jdbc:mysql://127.0.0.1:3306/lucene?useSSL=true", "root", "password"); // 定义查询SQL String sql = "select * from book"; // 创建Statement语句对象 preStatement = conn.prepareStatement(sql); // 执行语句, 得到结果集 resultSet = preStatement.executeQuery(); // 处理结果集 while (resultSet.next()) { // 创建图书对象 Book book = new Book(); book.setId(resultSet.getInt("id")); book.setBookname(resultSet.getString("bookname")); book.setPrice(resultSet.getFloat("price")); book.setPic(resultSet.getString("pic")); book.setBookdesc(resultSet.getString("bookdesc")); // 将查询到的结果添加到list中 books.add(book); } } catch (Exception e) { e.printStackTrace(); } finally { // 释放资源 try { if (null != conn) conn.close(); if (null != preStatement) preStatement.close(); if (null != resultSet) resultSet.close(); } catch (Exception e) { e.printStackTrace(); } } return books; } /** * 测试功能的主方法 */ public static void main(String[] args) { // 创建图书Dao的实现对象 BookDao bookDao = new BookDaoImpl(); List<Book> books = bookDao.listAll(); // 如果结果不为空, 则便利输出 for (Book book : books) { System.out.println(book); } } }

测试结果如下:

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

索引流程的实现

采集原始数据;

创建文档对象(Document);

创建分析器对象(Analyzer), 用于分词;

创建索引配置对象(IndexWriterConfig), 用于配置Lucene;

创建索引库目录位置对象(Directory), 指定索引库的存储位置;

创建索引写入对象(IndexWriter), 将文档对象写入索引库;

使用IndexWriter对象, 创建索引;

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwjysx.html