当前位置:   article > 正文

JavaEE进阶——全文检索之Lucene框架_java 全文搜索引擎框架 lucene

java 全文搜索引擎框架 lucene

I. 引言

全文检索

全文检索首先对要搜索的文档进行分词,然后形成索引,通过查询索引来查询文档。先创建索引,然后根据索引来进行搜索。比如查字典,字典的偏旁部首就类似于索引,字典的具体内容则类似于文档内容。

应用场景:

  • 搜索引擎
  • 站内搜索
  • 文件系统的搜索

Lucence

Lucene是Apache的一个全文检索引擎工具包,通过Lucene可以让程序员快速开发一个全文检索功能。Lucene不是搜索引擎,仅仅是一个工具包。它不能独立运行,不能单独对外提供服务(Solr、ElasticSearch)。

II. Lucence实现全文检索流程

全文检索流程

  1. 索引流程:采集数据——文档处理——存储到索引库中
  2. 搜索流程:输入查询条件——通过Lucene查询器查询索引——从索引库中取出结果——视图渲染

:Lucene本身不能进行视图渲染。


Lucene的索引与搜索

文档域:文档域存储的信息就是采集到的信息,通过Document对象来存储,具体说是通过Document对象中Field域来存储数据。比如:数据库中一条记录会存储一个一个Document对象,数据库中一列会存储成Document中一个Field域。Field域的name为字段名,value则为具体值。文档域中,Document对象之间是没有关系的,而且每个Document中的Field域也不一定一样。

索引域:主要是为了搜索使用的,索引域的内容是经过Lucene分词之后存储的。

倒排索引表:传统方法是先找到文件,如何在文件中找内容,在文件内容中匹配搜索关键字,这种方法是顺序扫描方法,数据量大就搜索慢。 倒排索引结构是根据内容(词语)找文档,包括索引和文档两部分。索引即词汇表,它是在索引中匹配搜索关键字,由于索引内容量有限并且采用固定优化算法搜索速度很快,先找到索引中的词汇,词汇又与文档关联,从而最终找到了文档。

III. 入门案例

环境准备

案例需求

使用Lucene完成对数据库中商品信息的索引和搜索功能。


工程搭建

需要导入如下依赖jar包:

  • MySQL驱动包
  • Analysis包
  • Core包
  • QueryParse包

其中,Analysis包、Core包和QueryParse包分别在lucene-7.4.0\analysis\common、lucene-7.4.0\core和lucene-7.4.0\queryparser文件夹下,最终工程结构如下:


准备数据

这里的数据主要来源于数据库商品表,需要通过Jdbc查询数据库准备数据。

Product实体类:

public class Product {
   
    private Long id;
    private String title;
    private String sellPoint;
    private Long price;
    private int num;
    private String barcode;
    private String image;
    private Long cid;
    private int status;
    private Date created;
    private Date updated;
    /** getter and setter **/
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

ProductDao数据库查询:

public class ProductDao {
   

    public List<Product> queryProducts() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;

        ResultSet resultSet = null;
        List<Product> products = new ArrayList<>();

        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/taotao", "root", "1234");

            String sql = "SELECT * FROM tb_item";
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();

            // 结果集解析
            while (resultSet.next()) {
                Product product = new Product();
                product.setId(resultSet.getLong("id"));
                product.setTitle(resultSet.getString("title"));
                product.setSellPoint(resultSet.getString("sell_point"));
                product.setBarcode(resultSet.getString("barcode"));
                product.setImage(resultSet.getString("image"));
                product.setPrice(resultSet.getLong("price"));
                product.setCid(resultSet.getLong("cid"));
                product.setNum(resultSet.getInt("num"));
                product.setStatus(resultSet.getInt("status"));
                product.setCreated(resultSet.getDate("created"));
                product.setUpdated(resultSet.getDate("updated"));
                products.add(product);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return products;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

创建索引


IndexWriter是索引过程的核心组件,通过IndexWriter可以创建新索引、更新索引、删除索引操作。IndexWriter需要通过Directory对索引进行存储操作。

Directory描述了索引的存储位置,底层封装了I/O操作,负责对索引进行存储。它是一个抽象类,它的子类常用的包括FSDirectory(在文件系统存储索引)、RAMDirectory(在内存存储索引)。

@Test
public vo
  • 1
  • 2
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/469134
推荐阅读
相关标签
  

闽ICP备14008679号