2013-05-16 35 views
1

我有简单的代码来测试Infinispan中的搜索引擎。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和它的子类复员。我想通过超级职位ID进行搜索。然而,这个演示代码生成org.hibernate.search.SearchException: Unable to find field id in com.genesis.inifispan.InifinispanTest$DemoB

我假设我错过了搜索映射配置中的继承配置,但是查看文档我什么也没找到。我想要基于Java的配置,因为我无法在生产环境中更改实体类。

请问,你能帮我配置或指导我正确阅读文档。

回答

1

当使用SearchMapping应指定就像你使用注释字段:IDDemoA有效场而已,所以正确的映射是这样的:

SearchMapping mapping = new SearchMapping() 
    .entity(DemoA.class) 
     .property("id", ElementType.METHOD).field() 
    .entity(DemoB.class).indexed() 
    ; 

另外请注意我删除providedId():在最近的Infinispan版本中不再需要。

我认为SearchMapping至少应该警告你,甚至可能会立即抛出异常:打开JIRA HSEARCH-1328

+0

该配置允许继承。谢谢。 顺便说一句,id = 1000在缓存中,导致我的循环10'000 ;-) –

+0

hooo正确:)我会修改答案 – Sanne