HipsterZipster
HipsterZipster

Reputation: 1376

Retrieve Hibernate Search results directly from Lucene Index

How can I execute a search using Hibernate Search that will NOT retrieve the actual entities from the database and will instead just return the Document cache records for these entities? I am making sure to store the fields that I need inside the index. The database will be active and available during this process, I just wanted to decrease the unnecessary load.

@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);  

Versions:

Hibernate Search 4.1.1.Final

Hibernate Core 4.1.6.Final

Lucene 3.5

Upvotes: 0

Views: 1010

Answers (1)

Hardy
Hardy

Reputation: 19109

have a look at the Projection feature of Hibernate Search as described in the docs. The key is to call query.setProjection with the list of field names you want to retrieve from the index. Something like:

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

Note though that as a result you will get object arrays and not managed Hibernate entities.

Upvotes: 2

Related Questions