我有简单的代码,测试在英菲尼斯潘搜索引擎。
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
我假设我错过了Search Mapping configuration中的继承配置,但是通过查看文档,我什么也没有找到。我想有基于Java的配置,因为我不能改变生产环境中的实体类。
请,您可以帮助我配置或指导我以正确的方式阅读文档。
发布于 2013-05-17 05:58:47
在使用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 。
https://stackoverflow.com/questions/16591764
复制相似问题