public <T extends Model> Page<T> search(String queryString,PageCriteria pageCriteria,Class<T> modelClass){
List<T> result = new ArrayList<>();
Compass compass = compassTemplate.getCompass();
CompassSession session=compass.openSession();
CompassHits hits= session.find(queryString);
LOG.info("命中:"+hits.getLength());
LOG.info("查询字符串:"+queryString);
if(pageCriteria!=null){
int start = (pageCriteria.getPage()-1) * pageCriteria.getSize();
int end = (pageCriteria.getPage()-1) * pageCriteria.getSize() + pageCriteria.getSize();
if (end > hits.getLength()) {
end = hits.getLength();
}
for(int i=start;i<end;i++){
if(hits.data(i).getClass()==modelClass){
//T t=(T)hits.data(i);
try{
T t=hightlight(modelClass,hits,i);
result.add(t);
}catch(Exception e){
result.add((T)hits.data(i));
}
}
}
}else{
for(int i=0;i<hits.getLength();i++){
if(hits.data(i).getClass()==modelClass){
//T t=(T)hits.data(i);
try{
T t=hightlight(modelClass,hits,i);
result.add(t);
}catch(Exception e){
result.add((T)hits.data(i));
}
}
}
}
session.close();
//对搜索结果按主键递减的顺序排序,最新的数据在最前面
//Comparator comparter=new BeanComparator("id");
//Collections.sort(result, comparter);
//Collections.reverse(result);
//建立页面对象
Page<T> page= new Page<>();
page.setModels(result);
page.setTotalRecords(hits.getLength());
return page;
}