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)

  


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