注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

无线时代辐射无穷

抓紧生宝宝,小心辐射

 
 
 

日志

 
 

hibernate search中的Projection查询  

2009-11-18 14:15:27|  分类: java |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
For some use cases, returning the domain object (graph) is overkill. Only a
small subset of the properties is necessary. Hibernate Search allows you to
return a subset of properties:
Example 5.9. Using projection instead of returning the full
domain object
org.hibernate.search.FullTextQuery query = s.createFullTextQuery(
luceneQuery, Book.class );
query.setProjection( "id", "summary", "body", "mainAuthor.name" );
List results = query.list();
Object[] firstResult = (Object[]) results.get(0);
Integer id = firstResult[0];
String summary = firstResult[1];
String body = firstResult[2];
String authorName = firstResult[3];
Hibernate Search extracts the properties from the Lucene index and
convert them back to their object representation, returning a list of Object[].
Projections avoid a potential database round trip (useful if the query
response time is critical), but has some constraints:
? the properties projected must be stored in the index
(@Field(store=Store.YES)), which increase the index size
? the properties projected must use a FieldBridge implementing
org.hibernate.search.bridge.TwoWayFieldBridge or
org.hibernate.search.bridge.TwoWayStringBridge, the latter being the
simpler version. All Hibernate Search built-in types are two-way.
? you can only project simple properties of the indexed entity or its
embedded associations. This means you cannot project a whole
embedded entity.
? projection does not work on collections or maps which are indexed via
@IndexedEmbedded
Projection is useful for another kind of use cases. Lucene provides some
metadata information to the user about the results. By using some special
placeholders, the projection mechanism can retrieve them:
Chapter 5. Querying
72 Hibernate 3.1.1.GA
Example 5.10. Using projection in order to retrieve meta
data
org.hibernate.search.FullTextQuery query = s.createFullTextQuery(
luceneQuery, Book.class );
query.setProjection( FullTextQuery.SCORE, FullTextQuery.THIS,
"mainAuthor.name" );
List results = query.list();
Object[] firstResult = (Object[]) results.get(0);
float score = firstResult[0];
Book book = firstResult[1];
String authorName = firstResult[2];
You can mix and match regular fields and special placeholders. Here is the
list of available placeholders:
? FullTextQuery.THIS: returns the initialized and managed entity (as a non
projected query would have done).
? FullTextQuery.DOCUMENT: returns the Lucene Document related to the
object projected.
? FullTextQuery.OBJECT_CLASS: returns the class of the indexed entity.
? FullTextQuery.SCORE: returns the document score in the query. Scores
are handy to compare one result against an other for a given query but are
useless when comparing the result of different queries.
? FullTextQuery.ID: the id property value of the projected object.
? FullTextQuery.DOCUMENT_ID: the Lucene document id. Careful, Lucene
document id can change overtime between two different IndexReader
opening (this feature is experimental).
? FullTextQuery.EXPLANATION: returns the Lucene Explanation object for
the matching object/document in the given query. Do not use if you retrieve
a lot of data. Running explanation typically is as costly as running the
whole Lucene query per matching element. Make sure you use projection!
  评论这张
 
阅读(497)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017