我使用Hibernate搜索将全文搜索应用于数据库表中的一个字段。我就是这样用的:
FullTextSession fts=Search.createFullTextSession(session);
QueryBuilder qb2 = fts.getSearchFactory().buildQueryBuilder()
.forEntity(DocumentVersion.class).get();
org.apache.lucene.search.Query searchQuery2 = qb2.keyword()
.onField("content")
.matching(search)
.createQuery();
FullTextQuery fullTextQuery2 = fts.createFullTextQuery(searchQuery2,DocumentVersion.class);
fullTextQuery2.setMaxResults(100);
List res=fullTextQuery2 .list();最后一行给出了根据查询的相关性排列的DocumentVersion对象。,但是当我使用标准只得到DocumentVersion中最相关的documentId字段时,总是返回按升序排列的documentId,这表明它没有考虑相关性或排名。
这方面的代码是:
FullTextSession fts=Search.createFullTextSession(session);
QueryBuilder qb2 =fts.getSearchFactory().buildQueryBuilder().forEntity(DocumentVersion.class).get();
org.apache.lucene.search.Query searchQuery2 = qb2.keyword()
.onField("content")
.matching(search)
.createQuery();
FullTextQuery fullTextQuery2 = fts.createFullTextQuery(searchQuery2, DocumentVersion.class);
org.hibernate.Criteria crquery = session.createCriteria(DocumentVersion.class);
crquery.setProjection(Projections.property("documentId"));
crquery.setMaxResults(100);
fullTextQuery2.setCriteriaQuery(crquery);
List res=crquery.list();正如上面所解释的那样,我已经搜索了很多搜索方法来选择前100名最相关或排名最高的documentIds,但无法找到这样做的方法。有人请救救我!DocumentVersion类如下所示:
@Entity
@Indexed
@Table(name = "t_document_version")
public class DocumentVersion {
private long id;
private long documentId;
@Field(index=Index.TOKENIZED, store=Store.NO, name="content")
private String content;
private Timestamp tstamp;
...
}发布于 2014-08-28 12:36:46
您不能将Hibernate搜索查询与Hibernate ORM标准查询(org.hibernate.Criteria)混合使用。通过ORM标准查询,只支持设置提取样式。如果您想使用Hibernate搜索投影,请查看single/#projections。
你必须这样做:
fullTextQuery2.setProjection("documentId");https://stackoverflow.com/questions/25542825
复制相似问题