4.2. 查询 API


Data Grid 8 引入了一个更新的 Query API,它更易于使用,并具有更轻量的设计。与 Data Grid 7.x 相比,在分布式缓存中的值中搜索时,您可以获得更高效的查询性能。

注意

因为 Data Grid 8 Query API 已通过可重构,所以有几个功能和功能资源现已弃用。

本节重点介绍从以前的版本迁移时您需要对配置进行的更改。这些更改应包括计划删除所有已弃用的接口、方法或其他配置。

有关已弃用的功能 和功能的完整列表,请参阅 Data Grid Deprecations 和 Removals (红帽知识库)。

索引数据网格缓存

Data Grid Lucene Directory、InfinispanIndexManagerAffinityIndexManager 索引管理器以及 Hibernate Search 的 Infinispan Directory 供应商在 8.0 中被弃用,并在 8.1 中删除。

auto-config 属性在 8.1 中弃用,并计划删除。

配置索引模式配置的 index () 方法已弃用。当您在配置中启用索引时,Data Grid 会自动选择管理索引的最佳方法。

重要

不再支持几个索引配置值,如果包含它们,则会导致严重配置错误。

您应该对配置进行以下更改:

  • .indexing ().index (Index.NONE) 更改为 indexing ().enabled (false)
  • 按如下方式更改所有其他枚举值:index ing ().enabled (true)

声明性地,如果您的配置包含其他索引配置元素,则不需要指定 enabled="true"。但是,如果您以编程方式配置索引,则必须调用 enabled () 方法。同样,JSON 格式的 Data Grid 配置必须明确启用索引,例如:

"indexing": {
      "enabled": "true"
      ...
      },

索引类型

当未拒绝类型与索引缓存一起使用时,您必须在索引配置中声明所有索引类型,或者 Data Grid 会记录警告信息。此要求适用于 Java 类和 Protobuf 类型。

在 Data Grid 8 中启用索引
  • 声明性

    <distributed-cache name="my-cache">
       <indexing>
         <indexed-entities>
           <indexed-entity>com.acme.query.test.Car</indexed-entity>
           <indexed-entity>com.acme.query.test.Truck</indexed-entity>
         </indexed-entities>
       </indexing>
    </distributed-cache>
  • 以编程方式

    import org.infinispan.configuration.cache.*;
    
    ConfigurationBuilder config=new ConfigurationBuilder();
    config.indexing().enable().addIndexedEntity(Car.class).addIndexedEntity(Truck.class);

在缓存中查询值

org.infinispan.query.SearchManager 接口在 Data Grid 8 中已弃用,不再支持 Lucene 和 Hibernate Search native 对象。

删除的方法

  • 使用 Lucene Queries 的 .getQuery () 方法。改为使用来自 org.infinispan.query.Search 入口点的 Ickle 查询的替代方法。

    同样,在调用 .getQuery () 时无法再指定多个目标实体类。Ickle 查询字符串提供实体。

  • .buildQueryBuilderForClass (),直接构建 Hibernate Search 查询。改为使用 Ickle 查询。

org.infinispan.query.CacheQuery 接口也被弃用。您应该从 Search.getQueryFactory () 方法获取 org.infinispan.query.dsl.Query 接口。

请注意,org.infinispan.query.dsl.Query 的实例不再缓存查询结果,并允许在调用 list () 等方法时重新执行查询。

实体映射

现在,所有情况下都必须注解需要 @SortableField 排序的字段。

4.2.1. 查询 8.2 中的 API 更改

Data Grid 升级 Hibernate 和 Apache Lucene 库,以提高 Query API 的性能和功能。作为此升级的一部分,Data Grid 引入了新的索引功能,并删除几个 Hibernate 和 Lucene 注解。

查询统计信息

只有在缓存配置中以声明性方式启用统计时,Data Grid 8.2 才会公开查询和索引的统计信息,如下所示:

<replicated-cache name="myReplicatedCache" statistics="true">
  <!-- Cache configuration goes here. -->
</replicated-cache>

不再通过 JMX 启用查询和索引统计信息。

索引数据网格缓存

声明索引类型

Data Grid 8.1 允许在索引配置中取消拒绝类型。从 Data Grid 8.2 开始,您必须在配置中声明所有索引类型。此要求适用于 Java 类和 Protobuf 类型。有关声明索引类型的更多信息,请参阅 8.1 迁移详情。

索引管理器

Data Grid 8.2 使用 near-real-time 作为默认索引管理器,不再需要配置。

  • Data Grid 8.1:

    <indexing>
      <property name="default.indexmanager">near-real-time</property>
    </indexing>
  • Data Grid 8.2:

    <indexing enabled="true"/>
索引读者和写器

Data Grid 8.2 引入了索引读取器和索引写入器,两者都是用于创建索引的内部组件。

要适应您的配置,您应该:

  1. 删除使用 property 元素或 .addProperty () 方法的索引配置。
  2. 使用以下方法之一配置索引行为:

    • 声明:添加 < index-reader> 和 & lt ;index-writer> 元素。
    • 编程方式:添加 builder.indexing ().reader ()builder.indexing ().writer () 方法。

读取器刷新

使用 8.2 中添加的 refresh-interval 属性为索引读取器配置刷新周期。

  • Data Grid 8.1:

    <indexing>
      <property name="default.reader.async_refresh_period_ms">1000</property>
    </indexing>
  • Data Grid 8.2:

    <indexing>
      <index-reader refresh-interval="1000"/>
    </indexing>

writer 提交间隔

使用 8.2 中添加的 commit-interval 属性来配置索引写入器提交的时间间隔。在 Data Grid 8.2 索引中,默认是异步的,default.worker.execution 属性不再使用。

  • Data Grid 8.1:

    <indexing>
       <property name="default.worker.execution">async</property>
       <property name="default.index_flush_interval">500</property>
    </indexing>
  • Data Grid 8.2:

    <indexing>
       <index-writer commit-interval="500"/>
    </indexing>

Lucene 索引调整属性

Data Grid 8.2 添加 ram-buffer-size 属性和 index-merge 元素,带有替换调优索引的属性的 factormax-size 属性。

  • Data Grid 8.1:

    <indexing>
       <property name="default.indexwriter.merge_factor">30</property>
       <property name="default.indexwriter.merge_max_size">1024</property>
       <property name="default.indexwriter.ram_buffer_size">256</property>
    </indexing>
  • Data Grid 8.2:

    <indexing>
       <index-writer ram-buffer-size="256">
           <index-merge factor="30" max-size="1024"/>
       </index-writer>
    </indexing>
索引存储

Data Grid 8.2 包含一个 storage 属性,它替换之前版本中的 property 元素配置。storage 属性允许您配置是否将索引存储在 JVM 堆或主机文件系统中。

文件系统存储

  • Data Grid 8.1:

    <indexing>
      <property name="default.directory_provider">filesystem</property>
      <property name="default.indexBase">${java.io.tmpdir}/baseDir</property>
    </indexing>
  • Data Grid 8.2:

    <indexing storage="filesystem" path="${java.io.tmpdir}/baseDir"/>

JVM 堆存储

  • Data Grid 8.1:

    <indexing>
     <property name="default.directory_provider">local-heap</property>
    </indexing>
  • Data Grid 8.2:

    <indexing storage="local-heap">
    </indexing>
调整索引属性

将索引配置迁移到 Data Grid 8.2 时,您还应进行以下更改:

  • 删除 lucene_version 属性。

    重要

    不要使用您使用带有 Data Grid 8.2 的旧 Lucene 版本创建的索引。

    在调整索引配置后,您应该在第一次启动 Data Grid 时重建索引,以完成迁移到 Data Grid 8.2。

  • 删除 default.sharding_strategy.nbr_of_shards 属性。
    此属性在没有替换的情况下已弃用。
  • 删除 infinispan.query.lucene.max-boolean-clauses 属性。
    从 Data Grid 8.2 开始,您应该将其设置为 JVM 属性。
Hibernate 和 Lucene 注解

有关迁移 Hibernate 和 Lucene 注解的信息,如 @Field@Indexed@SortableField 等等,请参阅 Hibernate Search Migration GuideAnnotation mapping 部分。

4.2.2. 在 8.3 中查询 API 更改

Data Grid 8.3 删除 IndexedQueryMode 参数。Data Grid 会自动检测查询缓存的最佳模式,并在早期版本中忽略此可选参数。

4.2.3. 查询 8.4 中的 API 更改

Data Grid 原生注解

Data Grid 8.4 引入了新的索引注解:@ Indexed ,@ Basic,@Decimal,@Keyword,@Text, 和 @Embedded.每个注解都支持一组属性,可用于进一步描述实体的索引方式。

这些新注解替换了 Hibernate Search 注解,这意味着您不再需要使用 @ProtoDoc 注解来给 Java 类添加远程缓存的 @ProtoDoc 注释。所有注解都作为注释复制到生成的 .proto 文件中。

下表总结了 Hibernate Search 5 (HS5)注解和 Data Grid 原生注解之间的字段映射:

Expand
HS5 注解索引属性Data Grid 原生注解描述

@Field(index=Index.YES)

可搜索

@basic, @Decimal, @Keyword, @Text

之前标记为索引的字段现在是可搜索的。

@Field(store = Store.YES)

projectable = true

@basic, @Decimal, @Keyword, @Text

以前标记为 storage 的字段现在可以被项目处理。

输入 String && @Field (analyze = Analyze.YES)

analyzer = "<definition>"

@Text

在索引过程中,带有分析器定义标记的字符串字段仍然会被分析。

@Field(analyze = Analyze.NO) && (@Field(store = Store.YES) OR @Field(sortable = Sortable.YES))

sortable = true

@basic, @Decimal, @Keyword

没有分析但存储在索引中的字段或标记为可排序的字段现在可以排序。

N/A

aggregable = true

@basic, @Decimal, @Keyword

无法使用 Hibernate Search 5 注解执行聚合操作。

N/A

normalizer = "lowercase"

@Keyword

因为过程中潜在的数据丢失,无法映射分析或规范化的字段。

查询效率

您可以使用 default-max-results 缓存属性限制查询实例返回的结果数量。default-max-results 的默认值为 100。限制查询返回的结果数量可显著提高没有显式限制的查询的性能。

Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2026 Red Hat
返回顶部