Hibernate Search 技术文档
2024-12-20 12:00:20作者:宣利权Counsellor
1. 安装指南
1.1 环境要求
- Java 8 或更高版本
- Maven 或 Gradle 构建工具
- Hibernate ORM 5.4 或更高版本
1.2 Maven 依赖
在 pom.xml 文件中添加以下依赖:
<dependency>
<groupId>org.hibernate.search</groupId>
<artifactId>hibernate-search-mapper-orm</artifactId>
<version>7.2.2.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.search</groupId>
<artifactId>hibernate-search-backend-lucene</artifactId>
<version>7.2.2.Final</version>
</dependency>
1.3 Gradle 依赖
在 build.gradle 文件中添加以下依赖:
dependencies {
implementation 'org.hibernate.search:hibernate-search-mapper-orm:7.2.2.Final'
implementation 'org.hibernate.search:hibernate-search-backend-lucene:7.2.2.Final'
}
2. 项目的使用说明
2.1 配置文件
在 hibernate.cfg.xml 或 persistence.xml 中添加 Hibernate Search 的配置:
<property name="hibernate.search.default.directory_provider" value="filesystem"/>
<property name="hibernate.search.default.indexBase" value="/path/to/indexes"/>
2.2 实体映射
使用注解或编程 API 将实体映射到索引字段。例如:
@Entity
@Indexed
public class Book {
@Id
@GeneratedValue
private Integer id;
@FullTextField
private String title;
@ManyToMany
@IndexedEmbedded
private Set<Author> authors = new HashSet<>();
// Getters and setters
}
@Entity
public class Author {
@Id
@GeneratedValue
private Integer id;
@FullTextField
private String name;
@ManyToMany(mappedBy = "authors")
private Set<Book> books = new HashSet<>();
// Getters and setters
}
2.3 索引数据
使用 MassIndexer 索引现有数据:
SearchSession searchSession = Search.session(entityManager);
MassIndexer indexer = searchSession.massIndexer(Book.class);
indexer.startAndWait();
2.4 搜索数据
使用 Search DSL 构建全文搜索查询:
SearchResult<Book> result = Search.session(entityManager)
.search(Book.class)
.where(f -> f.match()
.fields("title", "authors.name")
.matching("Isaac"))
.fetch(20);
List<Book> hits = result.hits();
long totalHitCount = result.total().hitCount();
3. 项目API使用文档
3.1 SearchSession API
SearchSession 是 Hibernate Search 的核心接口,用于执行搜索操作。
SearchSession searchSession = Search.session(entityManager);
3.2 MassIndexer API
MassIndexer 用于批量索引数据库中的所有实体。
MassIndexer indexer = searchSession.massIndexer(Book.class);
indexer.startAndWait();
3.3 Search DSL API
Search DSL 提供了丰富的查询构建功能,支持多种查询类型和排序方式。
SearchResult<Book> result = searchSession.search(Book.class)
.where(f -> f.match()
.fields("title", "authors.name")
.matching("Isaac"))
.fetch(20);
4. 项目安装方式
4.1 Maven 安装
通过 Maven 安装 Hibernate Search,只需在 pom.xml 中添加相应的依赖。
4.2 Gradle 安装
通过 Gradle 安装 Hibernate Search,只需在 build.gradle 中添加相应的依赖。
4.3 手动安装
可以从 SourceForge 下载 Hibernate Search 的发布包,并手动集成到项目中。
通过以上步骤,您可以成功安装并使用 Hibernate Search 进行全文搜索。
登录后查看全文
热门项目推荐
PaddleOCR-VLPaddleOCR-VL 是一款顶尖且资源高效的文档解析专用模型。其核心组件为 PaddleOCR-VL-0.9B,这是一款精简却功能强大的视觉语言模型(VLM)。该模型融合了 NaViT 风格的动态分辨率视觉编码器与 ERNIE-4.5-0.3B 语言模型,可实现精准的元素识别。Python00- DDeepSeek-OCR暂无简介Python00
openPangu-Ultra-MoE-718B-V1.1昇腾原生的开源盘古 Ultra-MoE-718B-V1.1 语言模型Python00
HunyuanWorld-Mirror混元3D世界重建模型,支持多模态先验注入和多任务统一输出Python00
AI内容魔方AI内容专区,汇集全球AI开源项目,集结模块、可组合的内容,致力于分享、交流。03
Spark-Scilit-X1-13BFLYTEK Spark Scilit-X1-13B is based on the latest generation of iFLYTEK Foundation Model, and has been trained on multiple core tasks derived from scientific literature. As a large language model tailored for academic research scenarios, it has shown excellent performance in Paper Assisted Reading, Academic Translation, English Polishing, and Review Generation, aiming to provide efficient and accurate intelligent assistance for researchers, faculty members, and students.Python00
GOT-OCR-2.0-hf阶跃星辰StepFun推出的GOT-OCR-2.0-hf是一款强大的多语言OCR开源模型,支持从普通文档到复杂场景的文字识别。它能精准处理表格、图表、数学公式、几何图形甚至乐谱等特殊内容,输出结果可通过第三方工具渲染成多种格式。模型支持1024×1024高分辨率输入,具备多页批量处理、动态分块识别和交互式区域选择等创新功能,用户可通过坐标或颜色指定识别区域。基于Apache 2.0协议开源,提供Hugging Face演示和完整代码,适用于学术研究到工业应用的广泛场景,为OCR领域带来突破性解决方案。00- HHowToCook程序员在家做饭方法指南。Programmer's guide about how to cook at home (Chinese only).Dockerfile013
Spark-Chemistry-X1-13B科大讯飞星火化学-X1-13B (iFLYTEK Spark Chemistry-X1-13B) 是一款专为化学领域优化的大语言模型。它由星火-X1 (Spark-X1) 基础模型微调而来,在化学知识问答、分子性质预测、化学名称转换和科学推理方面展现出强大的能力,同时保持了强大的通用语言理解与生成能力。Python00- PpathwayPathway is an open framework for high-throughput and low-latency real-time data processing.Python00
项目优选
收起
OpenHarmony documentation | OpenHarmony开发者文档
Dockerfile
239
2.37 K
deepin linux kernel
C
24
6
React Native鸿蒙化仓库
JavaScript
216
291
暂无简介
Dart
539
118
仓颉编译器源码及 cjdb 调试工具。
C++
115
86
仓颉编程语言运行时与标准库。
Cangjie
122
97
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
999
589
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
589
116
Ascend Extension for PyTorch
Python
78
111
仓颉编程语言提供了 stdx 模块,该模块提供了网络、安全等领域的通用能力。
Cangjie
80
56