最近在项目中使用 Hibernate Search 做搜索,想搜索实体类中一个 Date 类型的字段,但是直接 传递一个字符串,不转换成 Date 进行查询时,后台如下报错误:
org.hibernate.search.bridge.BridgeException: Exception while calling bridge#objectToString
class: com.***.***.entity.MyEntity
field bridge: createDate
....
DAO 查询方法如下:
public List<E> basicsFuzzyQuery(Object keywd, String[] fields, PageModel pageModel) throws Exception{
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);
fullTextEntityManager.createIndexer().startAndWait();
SearchFactory searchFactory = fullTextEntityManager.getSearchFactory();
QueryBuilder queryBuilder = searchFactory.buildQueryBuilder().forEntity(entityClass).get();
org.apache.lucene.search.Query luceneQuery = queryBuilder.keyword().fuzzy().withThreshold(.8f).withPrefixLength(1).onFields(fields)
.matching(keywd).createQuery();
org.apache.lucene.search.Sort sort = new Sort(SortField.FIELD_SCORE, new SortField("id", SortField.STRING));
FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(luceneQuery,entityClass);
fullTextQuery.setSort(sort);
if(pageModel != null) {
fullTextQuery.setFirstResult(pageModel.getFirstIndex());
fullTextQuery.setMaxResults(pageModel.getPageSize());
}
List<E> list = fullTextQuery.getResultList();
return list;
}
实体类中注解如下:
@Field(index = Index.YES , store = Store.NO, analyze=Analyze.NO)
@DateBridge(resolution = Resolution.DAY)
private Date createDate;
后台报该异常解决办法如下,红色加粗部分为修改添加的:
public List<E> basicsFuzzyQuery(Object keywd, String[] fields, PageModel pageModel) throws Exception{
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);
fullTextEntityManager.createIndexer().startAndWait();
SearchFactory searchFactory = fullTextEntityManager.getSearchFactory();
QueryBuilder queryBuilder = searchFactory.buildQueryBuilder().forEntity(entityClass).get();
org.apache.lucene.search.Query luceneQuery = queryBuilder.keyword().fuzzy().withThreshold(.8f).withPrefixLength(1).onFields(fields)
<strong><span style="color:#FF0000;"> .ignoreFieldBridge()</span></strong>.matching(keywd).createQuery();
org.apache.lucene.search.Sort sort = new Sort(SortField.FIELD_SCORE, new SortField("id", SortField.STRING));
FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(luceneQuery,entityClass);
fullTextQuery.setSort(sort);
if(pageModel != null) {
fullTextQuery.setFirstResult(pageModel.getFirstIndex());
fullTextQuery.setMaxResults(pageModel.getPageSize());
}
List<E> list = fullTextQuery.getResultList();
return list;
}