Spring Compass (Lucene) 全文本搜索排序问题



import org.compass.core.Compass;
import org.compass.core.CompassHits;
import org.compass.core.CompassQuery;
import org.compass.core.CompassSession;
import org.compass.core.CompassTemplate;
import org.compass.core.CompassQuery.SortDirection;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.Token;
import org.apache.lucene.analysis.TokenStream;
...

public List<Product> searchProducts(String topMenuId, String middleMenuId, String menuId, String brandId,
  String searchWord, String orderByColumn) {
  Analyzer analyzer = new net.paoding.analysis.analyzer.PaodingAnalyzer();
  Reader r = new StringReader(searchWord);
  TokenStream ts = (TokenStream) analyzer.tokenStream("", r);
  log.debug("=====paoding_analyzer start ====");
  Token t;
  try {
    while ((t = ts.next()) != null) {
     log.debug(t.termText());
   }
  } catch (IOException e) {
   e.printStackTrace();
  }
  log.debug("=====paoding_analyzer end====");

  Compass compass = compassTemplate.getCompass();
  CompassSession session = compass.openSession();
  List<Product> list = new ArrayList<Product>();

  StringBuilder sql = new StringBuilder();
  sql.append(searchWord);
  boolean hasAnd = false;

  if (StringUtils.isNotEmpty(menuId)) {
    sql.append(hasAnd ? "" : " AND ");
    sql.append(" product_menu_id:").append(menuId);
    hasAnd = true;
  }
  if (StringUtils.isNotEmpty(brandId)) {
    sql.append(hasAnd ? "" : " AND ");
    sql.append(" product_brand_id:").append(brandId);
  }
  log.debug(sql.toString());

  CompassQuery compassQuery = session.queryBuilder().queryString(sql.toString()).toQuery();


  //排序部分,SortDirection.AUTO,SortDirection.REVERSE 逆序排序
  if (StringUtils.isNotEmpty(orderByColumn)) {
    if("click_ratio".equals(orderByColumn)){
      compassQuery.addSort( orderByColumn ,
        CompassQuery.SortPropertyType.INT,
        SortDirection.AUTO);
    }else if("saled_amount".equals(orderByColumn)){
      compassQuery.addSort( orderByColumn ,
        CompassQuery.SortPropertyType.INT,
        SortDirection.AUTO);
    }else if("web_price".equals(orderByColumn)){
      compassQuery.addSort( orderByColumn ,
        CompassQuery.SortPropertyType.FLOAT,
        SortDirection.AUTO);
    }else if("crtdate".equals(orderByColumn)){
      compassQuery.addSort( orderByColumn ,
        CompassQuery.SortPropertyType.STRING,
        SortDirection.AUTO);
    }
  }

  CompassHits hits = compassQuery.hits();

  log.debug("hits:" + hits.getLength());
  for (int i = 0; i < hits.length(); i++) {
    Product p = (Product) hits.data(i);
    // p.setName(hits.highlighter(i).fragment("name"));
    // p.setBrand(hits.highlighter(i).fragment("brand"));
    list.add(p);
  }

  return list;
}
...


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值