2013-05-16 70 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