Hibernate Search不仅能够提供高效、准确的全文搜索功能,还能够与现有的JPA体系无缝集成,简化开发流程并降低维护成本。

Hibernate Search不仅能够提供高效、准确的全文搜索功能,还能够与现有的JPA体系无缝集成,简化开发流程并降低维护成本。

哪些公司使用了Hibernate Search?

  • Red Hat : 作为 Hibernate Search 的主要维护者,Red Hat 在其多个产品和服务中使用了 Hibernate Search。
  • BBC:在内容管理系统中使用 Hibernate Search 提高新闻报道的检索效率。
  • CNN:在多媒体资产管理系统中使用 Hibernate Search 支持记者的工作流程。
  • Adobe: 在其文档管理系统中使用 Hibernate Search 来提供高效的搜索功能。
  • MuleSoft:使用 Hibernate Search 提供集成平台中的搜索功能。
  • WildFly:红帽的 WildFly 应用服务器也集成了 Hibernate Search 以支持搜索需求。
  • US Department of Veterans Affairs (VA):在医疗记录管理系统中使用 Hibernate Search 支持医疗服务。
  • NASA:在数据管理系统中使用 Hibernate Search 处理大量的科学数据。
  • Stanford University:在学术资源管理系统中使用 Hibernate Search 提高文献检索效率。
  • Harvard University:在图书馆信息系统中使用 Hibernate Search 支持学生和研究人员的信息查询。
  • AT&T:在客户服务系统中使用 Hibernate Search 提供快速响应的搜索功能。
  • Verizon:在网络管理系统中使用 Hibernate Search 优化设备信息的检索。

应用场景

1. 文档管理与检索

  • 法律文件存储:将大量的法律文件(如合同、判决书、法规等)存储在数据库中,并通过 Hibernate Search 实现快速检索。
  • 版本控制:记录每个条款的历史版本,支持回滚到之前的版本。

2. 知识管理系统

  • 案例库:构建内部案例库,律师可以通过关键词快速查找相关案例。
  • 法律研究:支持复杂的查询条件,帮助律师进行深入的法律研究。

3. 客户关系管理

  • 客户档案:管理客户的法律文件和历史记录,方便律师随时查看。
  • 沟通记录:记录与客户的沟通记录,支持全文搜索和高级过滤。

4. 合规性检查

  • 法规监测:自动监测最新的法律法规变化,并提醒律师进行相应的调整。
  • 风险评估:通过搜索和分析相关法规,帮助企业进行合规性风险评估。

Hibernate Search 的优势

1. 与JPA无缝集成

  • 现有基础:如果你已经在项目中使用了Spring Data JPA来管理数据库操作,Hibernate Search可以直接与其集成,无需额外的配置和学习成本。
  • 一致性:保持技术和架构的一致性,减少不同技术栈之间的切换和适配工作。

2. 强大的全文搜索功能

  • 多字段搜索:支持在多个字段上进行全文搜索,满足复杂的查询需求。
  • 模糊搜索:内置的模糊搜索功能可以帮助用户找到拼写错误或类似的条目。
  • 高亮显示:可以通过配置轻松实现搜索结果中的关键字高亮显示,提高用户体验。

3. 丰富的查询功能

  • 布尔查询:支持复杂的布尔逻辑(AND、OR、NOT),方便用户构建精确的搜索条件。
  • 范围查询:可以对数值型字段进行范围查询,例如日期范围、价格范围等。
  • 排序和分页:内置的支持排序和分页功能,确保搜索结果的相关性和易用性。

4. 易于扩展和维护

  • 模块化设计:Hibernate Search的设计使得它可以很容易地与其他组件集成,便于未来的功能扩展。
  • 社区和支持:拥有活跃的开源社区和丰富的文档资源,遇到问题时可以获得及时的帮助和解决方案。

5. 高性能

  • 索引优化:Hibernate Search提供了高效的索引机制,确保快速的数据检索能力。
  • 缓存机制:利用缓存技术加速常用数据的访问速度,提高系统性能。

6. 安全性

  • 权限控制:可以根据不同的用户角色分配不同的搜索权限,确保数据的安全性。

7. 自动索引管理

  • 增量索引:Hibernate Search会自动处理实体对象的变化(如新增、修改、删除),并在后台更新索引。
  • 批处理:支持批量索引操作,提高索引效率。

8. 灵活的分析器配置

  • 多种分析器:提供多种内置分析器(如标准分析器、停用词过滤器等),并允许自定义分析器以适应特定的语言和需求。
  • 多语言支持:支持多种语言的文本分析,适合国际化应用。

代码实操

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.0</version>
        <relativePath/><!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>lawmatcher</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>lawmatcher</name>
    <description>Law Document Clause Matching System</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate.search.orm</groupId>
            <artifactId>hibernate-search-mapper-orm</artifactId>
            <version>6.2.0.Final</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
  • 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.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.

添加点测试数据

添加一些中国《劳动法》的真实条款数据用于测试

-- 插入中国《劳动法》的部分条款数据
INSERT INTO legal_provision (title, content) VALUES 
('第十七条', '劳动合同依法订立即具有法律约束力,当事人必须履行劳动合同规定的义务。'), -- 合同生效即具法律效力
('第三十条', '用人单位解除劳动合同,应当提前三十日以书面形式通知劳动者本人。'), -- 解除合同需提前通知
('第四十四条', '有下列情形之一的,用人单位应当按照下列标准支付高于劳动者正常工作时间工资的工资报酬:(一)安排劳动者延长工作时间的,支付不低于工资百分之一百五十的工资报酬;(二)休息日安排劳动者工作又不能安排补休的,支付不低于工资百分之二百的工资报酬;(三)法定休假日安排劳动者工作的,支付不低于工资百分之三百的工资报酬。'), -- 加班费规定
('第五十条', '工资应当以货币形式按月支付给劳动者本人。不得克扣或者无故拖欠劳动者的工资。'), -- 工资发放规定
('第八十二条', '用人单位自用工之日起超过一个月不满一年未与劳动者订立书面劳动合同的,应当向劳动者每月支付二倍的月工资。'); -- 未签订合同的补偿规定
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/lawmatcher?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
# 使用MySQL 8的方言
spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
# 自动更新数据库模式
spring.jpa.hibernate.ddl-auto=update
# 显示SQL语句
spring.jpa.show-sql=true
# Hibernate Search配置
spring.jpa.properties.hibernate.search.backend.directory.type=lucene
spring.jpa.properties.hibernate.search.backend.directory.root=/tmp/lucene-indexes
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

法律条款实体类

package com.example.lawmatcher.model;

import org.hibernate.search.mapper.pojo.mapping.definition.annotation.FullTextField;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.GenericField;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.Indexed;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

/**
 * 法律条款实体类
 */
@Entity
@Indexed
publicclass LegalProvision {

    /**
     * 主键ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 条款标题
     */
    @GenericField
    private String title;

    /**
     * 条款内容
     */
    @FullTextField(analyzer = "standard")
    private String content;

    // Getters and setters
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}
  • 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.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.

法律条款仓库接口

package com.example.lawmatcher.repository;

import com.example.lawmatcher.model.LegalProvision;
import org.hibernate.search.mapper.orm.Search;
import org.hibernate.search.mapper.orm.session.SearchSession;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import java.util.List;

/**
 * 法律条款仓库接口
 */
@Repository
publicinterface LegalProvisionRepository extends JpaRepository<LegalProvision, Long> {

    /**
     * 根据内容搜索法律条款
     *
     * @param query 搜索关键字
     * @return 匹配的法律条款列表
     */
    default List<LegalProvision> searchByContent(String query) {
        EntityManager entityManager = getEntityManager();
        SearchSession searchSession = Search.session(entityManager);

        return searchSession.search(LegalProvision.class)
                .where(f -> f.match()
                        .fields("title", "content")
                        .matching(query))
                .fetchHits(20);
    }

    /**
     * 根据多个条件进行复杂查询
     *
     * @param keyword 关键词
     * @param field 字段名
     * @return 匹配的法律条款列表
     */
    default List<LegalProvision> advancedSearch(String keyword, String field) {
        EntityManager entityManager = getEntityManager();
        SearchSession searchSession = Search.session(entityManager);

        return searchSession.search(LegalProvision.class)
                .where(f -> f.match()
                        .field(field)
                        .matching(keyword))
                .fetchHits(20);
    }

    @PersistenceContext
    EntityManager getEntityManager();
}
  • 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.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.

Controller

package com.example.lawmatcher.controller;

import com.example.lawmatcher.model.LegalProvision;
import com.example.lawmatcher.repository.LegalProvisionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
publicclass LegalProvisionController {

    @Autowired
    private LegalProvisionRepository legalProvisionRepository;

    /**
     * 根据查询关键字搜索法律条款
     *
     * @param query 查询关键字
     * @return 匹配的法律条款列表
     */
    @GetMapping("/search")
    public List<LegalProvision> search(@RequestParam String query) {
        return legalProvisionRepository.searchByContent(query);
    }

    /**
     * 根据多个条件进行复杂查询
     *
     * @param keyword 关键词
     * @param field 字段名
     * @return 匹配的法律条款列表
     */
    @GetMapping("/advanced-search")
    public List<LegalProvision> advancedSearch(@RequestParam String keyword, @RequestParam String field) {
        return legalProvisionRepository.advancedSearch(keyword, field);
    }
}
  • 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.

主应用程序类

package com.example.lawmatcher;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class LawMatcherApplication {
    public static void main(String[] args) {
        SpringApplication.run(LawMatcherApplication.class, args);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

测试

GET http://localhost:8080/advanced-search?keyword=工资&field=content

Respons:

[
    {
        "id": 4,
        "title": "第五十条",
        "content": "工资应当以货币形式按月支付给劳动者本人。不得克扣或者无故拖欠劳动者的工资。"
    },
    {
        "id": 5,
        "title": "第八十二条",
        "content": "用人单位自用工之日起超过一个月不满一年未与劳动者订立书面劳动合同的,应当向劳动者每月支付二倍的月工资。"
    },
    {
        "id": 3,
        "title": "第四十四条",
        "content": "有下列情形之一的,用人单位应当按照下列标准支付高于劳动者正常工作时间工资的工资报酬:(一)安排劳动者延长工作时间的,支付不低于工资百分之一百五十的工资报酬;(二)休息日安排劳动者工作又不能安排补休的,支付不低于工资百分之二百的工资报酬;(三)法定休假日安排劳动者工作的,支付不低于工资百分之三百的工资报酬。"
    }
]
文章来自:51CTO

Loading

作者 yinhua

发表回复