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>
以编程方式
import org.infinispan.configuration.cache.*;
ConfigurationBuilder cacheCfg = ...
cacheCfg.indexing().enable()
.addIndexedEntity(Book.class)
.addProperty("property name", "propery value")
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>
以编程方式
cacheCfg.indexing()
.addIndexedEntity(Car.class)
.addIndexedEntity(Truck.class)
当缓存存储 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;
}
配置应该是:
<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>
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>
配置内存索引:
<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>
11.2.4. 索引管理器 复制链接链接已复制到粘贴板!
Data Grid 使用内部一个称为"Index Manager"的组件来控制如何将新数据应用到索引以及数据对搜索可见的时间。
当数据写入到缓存中时,默认索引管理器 基于目录的默认 写入索引。缺点是,在编写繁重的情况下,它可以显著减慢缓存写入速度,因为它需要在索引上执行名为"flushes"的恒定的昂贵的操作。
near-real-time 索引管理器与默认索引管理器类似,但利用 Lucene 的 Near-Real-Time 功能。它具有更好的写入性能,因为它经常将索引刷新到底层存储。缺陷是,在非清理关闭时,未清空的索引更改可能会丢失。可与 local-heap 或 filesystem 一起使用。
使用 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>
使用 文件系统的示例 :
<replicated-cache name="default">
<indexing>
<property name="default.indexmanager">near-real-time</property>
</indexing>
</replicated-cache>
11.2.5. 重建索引 复制链接链接已复制到粘贴板!
从缓存中存储的数据重建索引。如果您更改了索引类型或分析器定义等内容,则需要重建索引。同样,如果出于某种原因删除了索引,您可能需要重建索引。请注意,可能需要过些时间,因为需要重新处理网格中的所有数据。
Indexer indexer = Search.getIndexer(cache);
CompletionStage<Void> future = index.run();