我有简单的代码来测试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的配置,因为我无法在生产环境中更改实体类。
请您帮我配置或直接阅读文档。
答案 0 :(得分:1)
使用SearchMapping时,您应该像注释一样指定字段: id 只是 DemoA 的有效字段,因此正确的映射如下所示:
SearchMapping mapping = new SearchMapping()
.entity(DemoA.class)
.property("id", ElementType.METHOD).field()
.entity(DemoB.class).indexed()
;
另请注意,我删除了 providedId():最近的Infinispan版本不再需要。
我认为SearchMapping至少应警告你,甚至可能立即抛出异常:打开JIRA HSEARCH-1328 。