compass 分页查询

compass 分页查询  



public class IndexDao {
//声明配置信息
    private Compass compass ;
   
    public IndexDao(){
       // compass.cfg.xml得到配置信息  默认的配置信息
       CompassConfiguration cfg = new CompassConfiguration().configure();
       compass = cfg.buildCompass();
    }
   
   
//调用方法时候需要传入参数 查询的内容,起始条目数,要显示是条目数
    public QueryResult search(String queryString,int firstResult,int maxReasults){
       //打开session会话
       CompassSession session = compass.openSession();
       //开启session事务
       CompassTransaction tx = session.beginLocalTransaction();
       //用session建立一个查询
       CompassQueryBuilder queryBuilder = session.queryBuilder();
       //得到查询结果
       CompassQuery query = queryBuilder.queryString(queryString).toQuery();
       //按照postTime属性进行排列,默认是升序
//query.addSort(“postTime”,SortDirection.REVERSE);//是降序
       query.addSort("postTime");
       //获得结果集
       CompassHits hits = query.hits();
       //为了防止角标越界,所以在结果集和要取得的内容之间取最小值
       int end = Math.min(hits.length(), firstResult+maxReasults);
       List<Aritcle> list = new ArrayList<Aritcle>();
       for(int i=firstResult;i<end;i++){
           //取得每一个结果
           Aritcle aritcle = (Aritcle) hits.data(i);
          
           //高亮  返回的是高亮后的字符串
           String ht = hits.highlighter(i).fragment("content");
           if(ht!=null){
              aritcle.setContent(ht);
           }
          
           list.add(aritcle);
       }
       tx.commit();
       session.close();
       return new QueryResult(hits.length(),list);
    }  
}
//单字段查询
@SuppressWarnings("unchecked")
    public List<Product> searchProducts(String searchString) {
        Compass compass = compassTemplate.getCompass();
        CompassSession session = compass.openSession();

        List<Product> list = new ArrayList<Product>();
        CompassHits hits = session.queryBuilder().queryString(
                "name:" + searchString).toQuery().hits();
        for (int i = 0; i < hits.getLength(); i++) {
            Product hitProduct = (Product) hits.data(i);
            list.add(hitProduct);
        }
        return list;
    }
 
//多字段查询
 
ExpressionHits ThatjackContain the term jack in the default search fieldjack london (jack AND london)Contains the term jack and london in the default search fieldjack OR londonContains the term jack or london, or both, in the default search field+jack +london (jack AND london)Contains both jack and london in the default search fieldname:jackContains the term jack in the name property (meta-data)name:jack -city:london (name:jack AND NOT city:london)Have jack in the name property and don't have london in the city propertyname:"jack london"Contains the exact phrase jack london in the name propertyname:"jack london"~5Contain the term jack and london within five positions of one anotherjack*Contain terms that begin with jackjack~Contains terms that are close to the word jackbirthday:[1870/01/01 TO 1920/01/01]Have the birthday values between the specified values. Note that it is a lexicography range
CompassHits hits = session.createQueryBuilder()  .queryString("+name:jack +familyName:london")    .setAnalyzer("an1") // use a different analyzer  .toQuery()    .addSort("familyName", CompassQuery.SortPropertyType.STRING)    .addSort("birthdate", CompassQuery.SortPropertyType.INT)  .hits();

Another example for building a query that requires the name to be jack, and the familyName not to be london: 
CompassQueryBuilder queryBuilder = session.createQueryBuilder();CompassHits hits =  queryBuilder.bool()    .addMust( queryBuilder.term("name", "jack") )    .addMustNot( queryBuilder.term("familyName", "london") )  .toQuery()    .addSort("familyName", CompassQuery.SortPropertyType.STRING)    .addSort("birthdate", CompassQuery.SortPropertyType.INT)  .hits();
定义可搜索的对象,最好使用一个单独的类,专门用户搜索的,只需要定义在搜索结果页面中需要显示的属性。
在类上生命@searchable,说明这个是可以被搜索的类
在有唯一标识作用属性上生命@searchableId,这个必须要有。
在属性(getter)上生命@searchPropetry,并指定store与index策略,还可以指定boost(这个表示要搜索的内容的重要程度,默认是1F)

  


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章