我们刚刚发布了 Hibernate Search 7.1.0.Alpha2,这是 Hibernate Search 下一个次要版本的第二个 alpha 发布版。
这个版本带来了更多的向量搜索功能和改进,同时也集成了 Elasticsearch 的/OpenSearch 的向量搜索功能。
新功能
Hibernate Search 7.1 仍处于开发的早期阶段:一些功能可能仍不完整或可能以不兼容的方式更改。 |
依赖项升级
- Hibernate ORM (HSEARCH-5060)
-
Hibernate Search 现在依赖于 Hibernate ORM 6.4.2.Final。这带来了其他一些修复,其中包括解决在使用 ORM 判别器多租户时可能出现的 大量索引问题 的一些修复。
- Lucene (HSEARCH-5043)
-
Lucene 后端现在使用 Lucene 9.9.1。除了其他改进之外,它还带来了更好的向量搜索性能。
- Elasticsearch (HSEARCH-5032)
-
Elasticsearch 后端现在也兼容 Elasticsearch 8.12 以及其他已经兼容的版本。
- 其他
-
-
HSEARCH-5032: 升级到 Elasticsearch 客户端 8.12.0
-
HSEARCH-5057: 升级到 AWS SDK 2.23.3
-
HSEARCH-5047: 升级到 JBeret 2.2.0.Final
-
Lucene 和 Elasticsearch 后端的向量搜索
这个版本的Hibernate Search是在之前的alpha版本之上构建的,并集成了Elasticsearch/OpenSearch的向量搜索功能。简要回顾一下:向量搜索提供了搜索二进制(图像、音频或视频)或文本数据的方法:外部工具将这些数据转换为向量(字节数组或浮点数组,也称为“嵌入”),然后用于Hibernate Search中的索引和查询。Hibernate Search引入了一个新的字段类型@VectorField
和一个新的谓词knn
,以便向量可以索引并在其上进行搜索。
向量字段可以与以byte
或float
数组表示的向量数据一起工作。默认情况下,byte[]
和float[]
属性类型将与新的字段类型一起工作。对于任何其他实体属性类型,应实现自定义的值桥或值绑定器。请注意,索引向量必须具有相同的长度,并且该长度应提前指定以便创建模式
@Entity
@Indexed
public class Book {
@Id
private Integer id;
@VectorField(dimension = 512)
private float[] coverImageEmbeddings;
// Other properties ...
}
通过knn
谓词执行向量相似性搜索
float[] coverImageEmbeddingsVector = /*...*/
List<Book> hits = searchSession.search( Book.class )
.where( f ->
// provide the number of similar documents to look for:
f.knn( 5 )
// the name of the vector field:
.field( "coverImageEmbeddings" )
// matched documents will be the ones whose indexed vector
// is "most similar" to this vector
.matching( coverImageEmbeddingsVector )
// additionally an optional filter can be supplied
// to provide a regular fulltext search predicate
.filter( f.match().field( "authors.firstName" ).matching( "arthur" ) )
).fetchHits( 20 );
由于knn
谓词的本质,它总是会尝试找到最近的向量,即使找到的向量彼此之间相当远,即不太相似。这可能会导致查询返回不相关的结果。
为了解决这个问题,knn
谓词允许配置所需的最小相似度
float[] coverImageEmbeddingsVector = /*...*/
List<Book> hits = searchSession.search( Book.class )
.where( f ->
// Create a knn predicate as usual:
f.knn( 5 ).field( "coverImageEmbeddings" ).matching( coverImageEmbeddingsVector )
// Specify the required minimum similarity value, to filter out irrelevant results:
.requiredMinimumSimilarity( 5 ) )
.fetchHits( 20 );
请注意,每个后端在向量搜索方面可能都有自己的特定性和限制。有关更多详细信息,请参阅相关的文档。
这个版本的Hibernate Search对向量搜索进行了一些重命名。特别是
|
在元模型中查找字段的特性
现在,在检查元模型时,可以查看字段可用的特性(谓词/排序/投影等)。
SearchMapping mapping = /*...*/ ;
// Retrieve a SearchIndexedEntity:
SearchIndexedEntity<Book> bookEntity = mapping.indexedEntity( Book.class );
// Get the descriptor for that index.
// The descriptor exposes the index metamodel:
IndexDescriptor indexDescriptor = bookEntity.indexManager().descriptor();
// Retrieve a field by name
// and inspect its capabilities if such field is present:
indexDescriptor.field( "releaseDate" ).ifPresent( field -> {
if ( field.isValueField() ) {
// Get the descriptor for the field type:
IndexValueFieldTypeDescriptor type = field.toValueField().type();
// Inspect the "traits" of a field type:
// each trait represents a predicate/sort/projection/aggregation
// that can be used on fields of that type.
Set<String> traits = type.traits();
if ( traits.contains( IndexFieldTraits.Aggregations.RANGE ) ) {
// ...
}
if ( traits.contains( IndexFieldTraits.Predicates.EXISTS ) ) {
// ...
}
// ...
}
} );
其他改进和错误修复
-
HSEARCH-5034:Hibernate Search将允许在向
BeanConfigurationContext
注册bean时传递BeanReference<? extends T>
。 -
HSEARCH-5004:Hibernate Search将默认使用Hibernate ORM的默认值,而不是强制使用
SqlTypes.CHAR
为OutboxEvent
/Agent
ID。
还有更多。有关上一版本以来所有更改的完整列表,请参阅发布说明。
如何获取此版本
所有详细信息均可在hibernate.org上的专用页面上找到并保持最新。
入门和迁移
对于新应用,请参阅入门指南
对于现有应用,假设您也升级了依赖项,Hibernate Search 7.1 是 7.0 的直接替代品。有关已弃用的配置和 API 的信息包含在迁移指南中。