2012-08-09 59 views
1

我們在整個應用程序中使用Hibernate Search(4.1)來管理資源的搜索和索引,但有時需要管理索引中的「計算」值,例如調用到@IndexEmbedded或@Field連接到干將沒有實際的性質:基於對「計算」值的更改,Hibernate搜索不是重新索引

public class Resource { 
      @ManyToMany(...) 
      private List<Keyword> keywords = new ArrayList<Keyword>(); 


      public List<Keyword> getKeywords() { 
        return keywords; 
      } 

      public List<Keyword> setKeywords(List<Keyword> keyword>) { 
        this.keywords=keywords; 
      }; 


      @IndexedEmbedded 
      public List<Keyword> getIndexedKeywords() { 
        List<Keyword> toReturn = new ArrayList<Keyword>(); 
        for (Keyword keyword: getKeywords()) { 
          if (keyword.isIndexed) { 
            toReturn.add(keyword); 
          } 
        } 
        return toReturn; 
      } 

    } 

...

public void saveResource(Resource resource, Collection<Keyword> keywords) { 
     resource.getKeywords().retainAll(keywords); 
     resource.getKeywords().addAll(keywords); 
     session.saveOrUpdate(resource); 
     // will trigger a persist in the database correctly, but will not trigger a reindex 
}; 

但調用saveResource不會導致HibernateSearch重新索引。不應該嗎?

回答

0

HibernateSearch 4.1(或3.4之後的所有內容)使用'智能'檢查來查看是否需要進行重新索引,這與@IndexedEmbedded對Hibernate持久字段的調用相匹配。如果這些不匹配,那麼Hibernate Search不會重新對對象進行重新索引,因爲檢查沒有看到任何匹配的內容,因此不會調用reindex。這很可能是因爲HibernateSearch必須對索引字段執行大量的反射和內省來確定底層值是否已更改,或者計算並存儲每個調用的結果,以便可以確定值是否已更改,從而既減緩重建索引過程和/或用存儲的散列值或整個結果值來擴大lucene索引。可能最好是簡單地污染導致重新索引的字段,或者最後手動重新索引對象。