我有在 Infinispan 中测试搜索引擎的简单代码。
public class InifinispanTest {
private static class DemoA {
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
private static class DemoB extends DemoA {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
public static void main(String[] args) throws IOException {
SearchMapping mapping = new SearchMapping();
mapping.entity(DemoB.class).indexed().providedId()
.property("id", ElementType.METHOD).field();
Properties properties = new Properties();
properties.put(org.hibernate.search.Environment.MODEL_MAPPING, mapping);
properties.put("hibernate.search.default.directory_provider", "ram");
properties.put("hibernate.search.default.indexmanager", "near-real-time");
Configuration infinispanConfiguration = new ConfigurationBuilder()
.indexing()
.enable()
.indexLocalOnly(true)
.withProperties(properties)
.loaders().passivation(true).addFileCacheStore()
.build();
DefaultCacheManager cacheManager = new DefaultCacheManager(infinispanConfiguration);
final Cache<Integer, DemoB> cache = cacheManager.getCache();
for (int i = 0; i < 10000; i++) {
final DemoB demo = new DemoB();
demo.setId((long) i);
cache.put(i, demo);
}
final SearchManager searchManager = Search.getSearchManager(cache);
final QueryBuilder queryBuilder = searchManager.buildQueryBuilderForClass(DemoB.class).get();
final Query query = queryBuilder.keyword().onField("id").matching(1000l).createQuery();
final CacheQuery query1 = searchManager.getQuery(query, DemoB.class);
for (Object result : query1.list()) {
System.out.println(result);
}
}
}
如您所见,有一个基类 DemoA 及其子类 DemoB。我想通过超类归档 id 进行搜索。然而这个演示代码生成org.hibernate.search.SearchException: Unable to find field id in com.genesis.inifispan.InifinispanTest$DemoB
我假设我错过了搜索映射配置中的继承配置,但是查看文档我一无所获。我想要基于 Java 的配置,因为我无法在生产环境中更改实体类。
请您帮我配置或指导我以正确的方式阅读文档。