2012-09-10 121 views
0

如何使用Hibernate Search執行搜索,不會從數據庫中檢索實際實體,而只是返回這些實體的文檔緩存記錄?我確保將索引內需要的字段存儲起來。在這個過程中數據庫將處於活動狀態,我只是想減少不必要的負載。直接從Lucene索引中檢索Hibernate搜索結果

@Column 
@Field (index = Index.YES, store = Store.YES) 
private String title; 

@Id 
@Column 
@DocumentId 
@Field (store = Store.YES) 
private String guid; 

Session sess = sessionFactory.openSession(); 
FullTextSession fts = org.hibernate.search.Search.getFullTextSession(sess); 
//returns matching Articles from database, how would I retrieve only the index records? 
Query query = fts.createFullTextQuery(luceneQuery, Article.class); 

版本:

Hibernate Search的4.1.1.Final

Hibernate的核心4.1.6.Final

的Lucene 3.5

回答

2

看看休眠的投影功能按照docs中的描述進行搜索。 關鍵是要調用query.setProjection與你想從索引檢索字段名稱的列表。例如:

query.setProjection("field1", "field2", "field3"); 

但請注意,因此您將獲得對象數組而不是託管Hibernate實體。