public QueryResult<Product_InfoBean> getSearch_Product(final Product_InfoBean product){
return this.getCompassTemplate().execute(
new CompassCallback<QueryResult<Product_InfoBean>>() {
public QueryResult<Product_InfoBean> doInCompass(CompassSession session)throws CompassException {
QueryResult<Product_InfoBean> qr=new QueryResult<Product_InfoBean>();
long beginTime = System.currentTimeMillis();
List<Product_InfoBean> result = new ArrayList<Product_InfoBean>();
CompassQueryBuilder queryBuilder = session.queryBuilder();
/* 搜索条件
* 如果所有条件都不满足则都为 click_Num>0,即:点击量都大于1,用于列出全部内容 *
*/
//价格大于 小于 等于
CompassQuery price=queryBuilder.gt("click_Num","0");
if(!StringUtils.isNullOrEmpty(product.getD_Price())&&!StringUtils.isNullOrEmpty(product.getX_Price())){
double d=Double.parseDouble(product.getD_Price());
double x=Double.parseDouble(product.getX_Price());
if(x>0&&d>0&&x<d){
price=queryBuilder.between("qudao_Price", product.getD_Price(),product.getX_Price(),true);
}else{price=queryBuilder.gt("click_Num","0");}
}else{price=queryBuilder.gt("click_Num","0");}
//一级分类
CompassQuery bigkind_Id=queryBuilder.gt("click_Num","0");
if(!StringUtils.isNullOrEmpty(product.getBigkind_Id())){
bigkind_Id=queryBuilder.spanEq("bigkind_Id", product.getBigkind_Id());
}
//二级分类
CompassQuery skind_Id=queryBuilder.gt("click_Num","0");
if(!StringUtils.isNullOrEmpty(product.getSkind_Id())){
skind_Id=queryBuilder.spanEq("skind_Id", product.getSkind_Id());
}
//三级分类
CompassQuery tkind_Id=queryBuilder.gt("click_Num","0");
if(!StringUtils.isNullOrEmpty(product.getTkind_Id())){
tkind_Id=queryBuilder.spanEq("tkind_Id", product.getTkind_Id());
}
//搜索关键字
CompassQuery keywords=queryBuilder.gt("click_Num","0");
if(!StringUtils.isNullOrEmpty(product.getKeywords())){
keywords=queryBuilder.queryString(product.getKeywords()).toQuery();
}
//不包含关键字的条件 暂时不需要
/* 排序开始(默认按时间排序)
*
* 分别按 价格 、点击量、订货量、上架时间
*
* 多个索引不指定排序字段类型会报
* Exception:
java.lang.RuntimeException: field "xxx" does not appear to be indexed
*/
String plsx="add_Time";
SortPropertyType type=SortPropertyType.STRING;
if(!StringUtils.isNullOrEmpty(product.getSx())){
if(product.getSx().trim().equals("jg")){
//价格
plsx="qudao_Price";
type=SortPropertyType.DOUBLE;
}else if(product.getSx().trim().equals("dn")){
//点击量
plsx="click_Num";
type=SortPropertyType.LONG;
}else if(product.getSx().trim().equals("xn")){
//订货量 销售量
plsx="sell_Num";
type=SortPropertyType.LONG;
}else if(product.getSx().trim().equals("sj")){
//按时间
plsx="add_Time";
type=SortPropertyType.STRING;
}
}else {plsx="add_Time";type=SortPropertyType.STRING;}
// 升降(默认降序)
SortDirection zd=SortDirection.REVERSE;
if(!StringUtils.isNullOrEmpty(product.getZd())){
if(product.getZd().trim().equals("s")){
//升序
zd=SortDirection.AUTO;
}else if(product.getZd().trim().equals("j")){
//降序
zd=SortDirection.REVERSE;
}else{zd=SortDirection.REVERSE;}
}else {zd=SortDirection.REVERSE;}
CompassHits hits = queryBuilder.bool()
.addMust(price)
.addMust(bigkind_Id)
.addMust(skind_Id)
.addMust(tkind_Id)
.addMust(keywords)
.toQuery()
.addSort(plsx,type,zd)
.hits();
System.out.println("==============="+hits.toString());
//int length=product.getBeforeCount()+product.getLineSize();
int length = Math.min(hits.length(), product.getBeforeCount()+product.getLineSize());
if(length>hits.length()) length=hits.length();
/* for (CompassHit compassHit : hits) {*/
for(int i=product.getBeforeCount();i<length;i++){
System.out.println("----------"+hits.data(i).toString()+"----------");
Product_InfoBean cproduct=(Product_InfoBean)hits.data(i);
//针对需要加高亮的字段先判断
String cproductName=hits.highlighter(i).fragment("product_Name");
if(cproductName!=null){cproduct.setProduct_Name(cproductName);}
String cproductSName=hits.highlighter(i).fragment("product_SName");
if(cproductSName!=null){cproduct.setProduct_SName(cproductSName);}
String cproductKeyWords=hits.highlighter(i).fragment("product_KeyWords");
if(cproductKeyWords!=null){cproduct.setProduct_KeyWords(cproductKeyWords);}
result.add(cproduct);
}
long costTime = System.currentTimeMillis() - beginTime;
qr.setCosttime(costTime);
qr.setResultlist(result);
qr.setTotalrecord(hits.length());
qr.setResultlist(result);
return qr;
}
}); }
compass 的多条件搜索排序加分页
虚的就不多说了直接上代码