11.2. 索引条目值


在 Data Grid 缓存中索引条目值可显著提高搜索性能,并允许您执行全文本查询。但是,索引可能会降级 Data Grid 集群的写入吞吐量。因此,您应该计划使用策略来优化查询性能,具体取决于缓存模式和您的用例。有关 查询性能的更多信息

11.2.1. 配置

要通过 XML 启用索引,您需要在缓存配置中添加 & lt;indexing > 元素,指定索引的实体并选择性地传递附加属性。

注意

存在 & lt;indexing > 元素,省略 enabled 属性将自动启用索引,即使 enabled 属性的默认值在 XSD 模式中被定义为 "false "。在编程配置中,必须使用 enabled ()

声明性

<infinispan>
   <cache-container default-cache="default">
      <replicated-cache name="default">
         <indexing>
            <indexed-entities>
               <indexed-entity>com.acme.Book</indexed-entity>
            </indexed-entities>
            <property name="property.name">some value</property>
         </indexing>
      </replicated-cache>
   </cache-container>
</infinispan>
Copy to Clipboard Toggle word wrap

以编程方式

import org.infinispan.configuration.cache.*;

ConfigurationBuilder cacheCfg = ...
cacheCfg.indexing().enable()
            .addIndexedEntity(Book.class)
      .addProperty("property name", "propery value")
Copy to Clipboard Toggle word wrap

11.2.2. 指定索引实体

建议声明索引类型,因为它们将在下一个 Data Grid 版本中是必需的。

声明性

<infinispan>
   <cache-container default-cache="default">
      <replicated-cache name="default">
         <indexing>
            <indexed-entities>
                <indexed-entity>com.acme.query.test.Car</indexed-entity>
                <indexed-entity>com.acme.query.test.Truck</indexed-entity>
            </indexed-entities>
         </indexing>
      </replicated-cache>
   </cache-container>
</infinispan>
Copy to Clipboard Toggle word wrap

以编程方式

 cacheCfg.indexing()
       .addIndexedEntity(Car.class)
       .addIndexedEntity(Truck.class)
Copy to Clipboard Toggle word wrap

当缓存存储 protobuf 时,索引类型应该是 protobuf 模式中声明的消息。例如,对于以下模式:

package book_sample;

message Book {
    optional string title = 1;
    optional string description = 2;
    optional int32 publicationYear = 3; // no native Date type available in Protobuf

    repeated Author authors = 4;
}

message Author {
    optional string name = 1;
    optional string surname = 2;
}
Copy to Clipboard Toggle word wrap

配置应该是:

<infinispan>
  <cache-container default-cache="default">
    <replicated-cache name="books">
      <indexing>
        <indexed-entities>
          <indexed-entity>book_sample.Book</indexed-entity>
        </indexed-entities>
      </indexing>
    </replicated-cache>
  </cache-container>
</infinispan>
Copy to Clipboard Toggle word wrap

11.2.3. 索引存储

Data Grid 可以在文件系统中或内存中存储索引(local-heap)。文件系统是推荐的和默认配置,并且内存索引只应用于不需要在重启后进行中型索引的中型索引。

文件系统索引配置:

<replicated-cache name="myCache">
   <indexing>
      <indexed-entities>
         <indexed-entity>com.acme.Book</indexed-entity>
      </indexed-entities>
      <!-- Optional: this is the default setting -->
      <property name="default.directory_provider">filesystem</property>
      <!-- Optional: define base folder for indexes -->
      <property name="default.indexBase">${java.io.tmpdir}/baseDir</property>
   </indexing>
</replicated-cache>
Copy to Clipboard Toggle word wrap

配置内存索引:

<replicated-cache name="myCache">
   <indexing>
      <indexed-entities>
         <indexed-entity>com.acme.Book</indexed-entity>
      </indexed-entities>
      <property name="default.directory_provider">local-heap</property>
   </indexing>
</replicated-cache>
Copy to Clipboard Toggle word wrap

11.2.4. 索引管理器

Data Grid 使用内部一个称为"Index Manager"的组件来控制如何将新数据应用到索引以及数据对搜索可见的时间。

当数据写入到缓存中时,默认索引管理器 基于目录的默认 写入索引。缺点是,在编写繁重的情况下,它可以显著减慢缓存写入速度,因为它需要在索引上执行名为"flushes"的恒定的昂贵的操作。

near-real-time 索引管理器与默认索引管理器类似,但利用 Lucene 的 Near-Real-Time 功能。它具有更好的写入性能,因为它经常将索引刷新到底层存储。缺陷是,在非清理关闭时,未清空的索引更改可能会丢失。可与 local-heapfilesystem 一起使用。

使用 local-heap 的示例:

<replicated-cache name="default">
    <indexing>
        <property name="default.indexmanager">near-real-time</property>
        <property name="default.directory_provider">local-heap</property>
    </indexing>
</replicated-cache>
Copy to Clipboard Toggle word wrap

使用 文件系统的示例

<replicated-cache name="default">
    <indexing>
        <property name="default.indexmanager">near-real-time</property>
    </indexing>
</replicated-cache>
Copy to Clipboard Toggle word wrap

11.2.5. 重建索引

从缓存中存储的数据重建索引。如果您更改了索引类型或分析器定义等内容,则需要重建索引。同样,如果出于某种原因删除了索引,您可能需要重建索引。请注意,可能需要过些时间,因为需要重新处理网格中的所有数据。

Indexer indexer = Search.getIndexer(cache);
CompletionStage<Void> future = index.run();
Copy to Clipboard Toggle word wrap
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

Theme

© 2025 Red Hat