天天看點

ElasticSearch實作高亮、範圍查詢和多條件組合查詢(java和kibana)

ElasticSearch用戶端配置參考之前的部落格

1.高亮

高亮的本質其實是将搜尋結果中需要高亮的字段進行替換

public List<Map<String,Object>> searchHighLight(String keyWord,int currentPage,int pageSize) throws Exception{
        List<Map<String, Object>> list = new ArrayList<>();
        if(currentPage <= 1){
            currentPage = 1;
        }
        //未處理最大頁數
        SearchRequest searchRequest = new SearchRequest("index_one");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name",keyWord);
        searchSourceBuilder.query(termQueryBuilder).from((currentPage-1)*pageSize).size(pageSize);
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        //對name字段進行高亮顯示
        highlightBuilder.field("name");
        //高亮的樣式設定
        highlightBuilder.preTags("<font color='red'>");
        highlightBuilder.postTags("</font>");
        //添加高亮設定
        searchSourceBuilder.highlighter(highlightBuilder);
        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        SearchHit[] searchHits = searchResponse.getHits().getHits();
        for(SearchHit hit : searchHits){
            //從這裡可以看出可以設定多個字段進行高亮
            Map<String, HighlightField> map = hit.getHighlightFields();
            //高亮字段
            HighlightField name = map.get("name");
            //原本的搜尋結果
            Map<String,Object> sourceAsMap =  hit.getSourceAsMap();
            //所謂高亮就是将原本的搜尋結果中需要高亮的字段進行替換
            String n_name = "";
            if(name !=null){
                Text[] texts = name.fragments();
                for(Text text : texts){
                    n_name +=text;
                }
            }
            //高亮字段替換
            sourceAsMap.put("name",n_name);
            list.add(sourceAsMap);
        }
        return list;
    }
           

2.範圍查詢

/**
 *
 *
 *
 * 範圍查詢
 * 每一個文檔是一個map
 *
 *
 *GET /index_one/_doc/_search
 * {
 *   "query":{
 *     "range":{
 *       "price":{
 *         "gte":50,
 *         "lte":100
 *       }
 *     }
 *   },
 *   "from":0,
 *   "size":20
 * }
 *
 *
 * {
 *   "took" : 1,
 *   "timed_out" : false,
 *   "_shards" : {
 *     "total" : 1,
 *     "successful" : 1,
 *     "skipped" : 0,
 *     "failed" : 0
 *   },
 *   "hits" : {
 *     "total" : {
 *       "value" : 16,
 *       "relation" : "eq"
 *     },
 *     "max_score" : 1.0,
 *     "hits" : [
 *       {
 *         "_index" : "index_one",
 *         "_type" : "_doc",
 *         "_id" : "2",
 *         "_score" : 1.0,
 *         "_source" : {
 *           "name" : "瘋狂Java講義 第5版 附CD光牒 李剛 java語言教材計算機書教程Java8語言 瘋狂Java講義 第5版 附CD光牒 李剛 java語言教材計算機書教程Java8語言",
 *           "price" : 68.1
 *         }
 *       },
 *       {
 *         "_index" : "index_one",
 *         "_type" : "_doc",
 *         "_id" : "6",
 *         "_score" : 1.0,
 *         "_source" : {
 *           "name" : "瘋狂JAVA講義 第四版 新華書店,正版保證,關注店鋪成為會員可享店鋪專屬優惠,團購客戶請咨詢線上客服!",
 *           "price" : 62.5
 *         }
 *       },
 *       {
 *         "_index" : "index_one",
 *         "_type" : "_doc",
 *         "_id" : "7",
 *         "_score" : 1.0,
 *         "_source" : {
 *           "name" : "瘋狂Java講義(第5版)(含DVDCD光牒一張) 博庫神券狂潮~專題内滿65減50,滿125減100,更有9.9元秒殺套裝等你來搶~點選進入",
 *           "price" : 70.47
 *         }
 *       },
 *       {
 *         "_index" : "index_one",
 *         "_type" : "_doc",
 *         "_id" : "8",
 *         "_score" : 1.0,
 *         "_source" : {
 *           "name" : "瘋狂JAVA講義(第5版)(含DVDCD光牒一張) 新華書店,正版保證,關注店鋪成為會員可享店鋪專屬優惠,團購客戶請咨詢線上客服!",
 *           "price" : 81.5
 *         }
 *       },
 *       {
 *         "_index" : "index_one",
 *         "_type" : "_doc",
 *         "_id" : "10",
 *         "_score" : 1.0,
 *         "_source" : {
 *           "name" : "瘋狂Java講義(第5版)(博文視點出品) 榮獲“文津圖書獎”“中國好書”的典藏級國民地理書《這裡是中國》第2部。百年重塑山河,建設改變中國!",
 *           "price" : 67.9
 *         }
 *       },
 *       {
 *         "_index" : "index_one",
 *         "_type" : "_doc",
 *         "_id" : "13",
 *         "_score" : 1.0,
 *         "_source" : {
 *           "name" : "瘋狂Java面試講義 資料結構、算法與技術素養 圖書 新華書店,正版保證,關注店鋪成為會員可享店鋪專屬優惠,團購客戶請咨詢線上客服!",
 *           "price" : 62.5
 *         }
 *       },
 *       {
 *         "_index" : "index_one",
 *         "_type" : "_doc",
 *         "_id" : "15",
 *         "_score" : 1.0,
 *         "_source" : {
 *           "name" : "官方正版 瘋狂Java講義 第5版含DVDCD光牒一張 李剛 java語言程式設計 計算機自學教程書籍 官方正版 瘋狂Java講義 第5版含DVDCD光牒一張 李剛 java語言程式設計 計算機自學教程書籍",
 *           "price" : 76.5
 *         }
 *       },
 *       {
 *         "_index" : "index_one",
 *         "_type" : "_doc",
 *         "_id" : "17",
 *         "_score" : 1.0,
 *         "_source" : {
 *           "name" : "瘋狂Java講義(第3版)(含CD光牒) ava從入門到精通 java程式設計思想java核心技術 新華書店,正版保證,關注店鋪領取專屬優惠,雙旦季遇禮,全場滿就送點選進入",
 *           "price" : 65.4
 *         }
 *       },
 *       {
 *         "_index" : "index_one",
 *         "_type" : "_doc",
 *         "_id" : "18",
 *         "_score" : 1.0,
 *         "_source" : {
 *           "name" : "瘋狂Java講義(附CD光牒第4版瘋狂軟體教育标準教材) 瘋狂Java講義(附CD光牒第4版瘋狂軟體教育标準教材)",
 *           "price" : 60.5
 *         }
 *       },
 *       {
 *         "_index" : "index_one",
 *         "_type" : "_doc",
 *         "_id" : "19",
 *         "_score" : 1.0,
 *         "_source" : {
 *           "name" : "瘋狂Java講義(附CD光牒第3版瘋狂軟體教育标準教材)Java經典暢銷書全面更新 團購批發價格從優請聯系客服,博庫神券狂潮~專題内滿125減100,滿65減50,優惠套裝等你來搶~點選進入",
 *           "price" : 81.75
 *         }
 *       },
 *       {
 *         "_index" : "index_one",
 *         "_type" : "_doc",
 *         "_id" : "20",
 *         "_score" : 1.0,
 *         "_source" : {
 *           "name" : "瘋狂JAVA講義 第四版 新華書店,正版保證,關注店鋪成為會員可享店鋪專屬優惠,團購客戶請咨詢線上客服!",
 *           "price" : 82.8
 *         }
 *       },
 *       {
 *         "_index" : "index_one",
 *         "_type" : "_doc",
 *         "_id" : "21",
 *         "_score" : 1.0,
 *         "_source" : {
 *           "name" : "瘋狂JAVA講義 第四版 新華書店,正版保證,關注店鋪成為會員可享店鋪專屬優惠,團購客戶請咨詢線上客服!",
 *           "price" : 82.8
 *         }
 *       },
 *       {
 *         "_index" : "index_one",
 *         "_type" : "_doc",
 *         "_id" : "23",
 *         "_score" : 1.0,
 *         "_source" : {
 *           "name" : "瘋狂Java講義(第4版) 低價悅讀名家雜文、小說。點選檢視更多",
 *           "price" : 51.23
 *         }
 *       },
 *       {
 *         "_index" : "index_one",
 *         "_type" : "_doc",
 *         "_id" : "25",
 *         "_score" : 1.0,
 *         "_source" : {
 *           "name" : "瘋狂Java講義(第5版)(含DVDCD光牒一張) 瘋狂Java講義(第5版)(含DVDCD光牒一張)",
 *           "price" : 86.3
 *         }
 *       },
 *       {
 *         "_index" : "index_one",
 *         "_type" : "_doc",
 *         "_id" : "28",
 *         "_score" : 1.0,
 *         "_source" : {
 *           "name" : "瘋狂Java講義第5版含DVD一張 瘋狂Java講義第5版含DVD一張",
 *           "price" : 77.2
 *         }
 *       },
 *       {
 *         "_index" : "index_one",
 *         "_type" : "_doc",
 *         "_id" : "29",
 *         "_score" : 1.0,
 *         "_source" : {
 *           "name" : "瘋狂Java講義(第5版)(含DVDCD光牒一張) 瘋狂Java講義(第5版)(含DVDCD光牒一張)",
 *           "price" : 86.3
 *         }
 *       }
 *     ]
 *   }
 * }
 *
 *
 * @param min  最小價格
 * @param max  最大價格
 * @return
 */
public List<Map<String,Object>> queryRange(int min,int max) throws Exception{

    List<Map<String,Object>> list = new ArrayList<>();
    SearchRequest searchRequest = new SearchRequest("index_one");
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    //建構組合查詢對象
    //from預設為0,size預設為10, 是以總共16條,傳回了10條
    RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("price");
    rangeQueryBuilder.gte(min).lte(max);
    //searchSourceBuilder.query(rangeQueryBuilder)
    searchSourceBuilder.query(rangeQueryBuilder).from(0).size(20);
    searchRequest.source(searchSourceBuilder);
    SearchResponse searchResponse = client.search(searchRequest,RequestOptions.DEFAULT);
    SearchHit[] hits = searchResponse.getHits().getHits();
    for(SearchHit hit : hits){
        Map<String,Object> map = hit.getSourceAsMap();
        list.add(map);
    }
  return list;
}
           

3.多條件組合查詢(must)

/**
     * 多條件組合查詢
     *
     *
     * GET /index_one/_doc/_search
     * {
     *   "query":{
     *     "bool":{
     *       "must":[
     *         {"term":{
     *           "name":"關注"
     *         }},
     *         {
     *           "range":{
     *             "price":{
     *               "gte":50,
     *               "lte":100
     *             }
     *           }
     *         }
     *         ]
     *     }
     *   }
     * }
     *
     *
     *
     * {
     *   "took" : 4,
     *   "timed_out" : false,
     *   "_shards" : {
     *     "total" : 1,
     *     "successful" : 1,
     *     "skipped" : 0,
     *     "failed" : 0
     *   },
     *   "hits" : {
     *     "total" : {
     *       "value" : 6,
     *       "relation" : "eq"
     *     },
     *     "max_score" : 2.3121393,
     *     "hits" : [
     *       {
     *         "_index" : "index_one",
     *         "_type" : "_doc",
     *         "_id" : "6",
     *         "_score" : 2.3121393,
     *         "_source" : {
     *           "name" : "瘋狂JAVA講義 第四版 新華書店,正版保證,關注店鋪成為會員可享店鋪專屬優惠,團購客戶請咨詢線上客服!",
     *           "price" : 62.5
     *         }
     *       },
     *       {
     *         "_index" : "index_one",
     *         "_type" : "_doc",
     *         "_id" : "20",
     *         "_score" : 2.3121393,
     *         "_source" : {
     *           "name" : "瘋狂JAVA講義 第四版 新華書店,正版保證,關注店鋪成為會員可享店鋪專屬優惠,團購客戶請咨詢線上客服!",
     *           "price" : 82.8
     *         }
     *       },
     *       {
     *         "_index" : "index_one",
     *         "_type" : "_doc",
     *         "_id" : "21",
     *         "_score" : 2.3121393,
     *         "_source" : {
     *           "name" : "瘋狂JAVA講義 第四版 新華書店,正版保證,關注店鋪成為會員可享店鋪專屬優惠,團購客戶請咨詢線上客服!",
     *           "price" : 82.8
     *         }
     *       },
     *       {
     *         "_index" : "index_one",
     *         "_type" : "_doc",
     *         "_id" : "8",
     *         "_score" : 2.2545283,
     *         "_source" : {
     *           "name" : "瘋狂JAVA講義(第5版)(含DVDCD光牒一張) 新華書店,正版保證,關注店鋪成為會員可享店鋪專屬優惠,團購客戶請咨詢線上客服!",
     *           "price" : 81.5
     *         }
     *       },
     *       {
     *         "_index" : "index_one",
     *         "_type" : "_doc",
     *         "_id" : "13",
     *         "_score" : 2.2409074,
     *         "_source" : {
     *           "name" : "瘋狂Java面試講義 資料結構、算法與技術素養 圖書 新華書店,正版保證,關注店鋪成為會員可享店鋪專屬優惠,團購客戶請咨詢線上客服!",
     *           "price" : 62.5
     *         }
     *       },
     *       {
     *         "_index" : "index_one",
     *         "_type" : "_doc",
     *         "_id" : "17",
     *         "_score" : 2.1892588,
     *         "_source" : {
     *           "name" : "瘋狂Java講義(第3版)(含CD光牒) ava從入門到精通 java程式設計思想java核心技術 新華書店,正版保證,關注店鋪領取專屬優惠,雙旦季遇禮,全場滿就送點選進入",
     *           "price" : 65.4
     *         }
     *       }
     *     ]
     *   }
     * }
     *
     *
     *
     *
     *
     * @param keyWord  關鍵字
     * @param min    價格最小
     * @param max    價格最大
     * @return
     */
     public List<Map<String,Object>> rangeTermQuery(String keyWord,int min,int max) throws Exception{

        SearchRequest searchRequest = new SearchRequest("index_one");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name",keyWord);
        RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("price").gte(min).lte(max);
        boolQueryBuilder.must(termQueryBuilder).must(rangeQueryBuilder);
        searchSourceBuilder.query(boolQueryBuilder);
        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest,RequestOptions.DEFAULT);
        SearchHit[] searchHits = searchResponse.getHits().getHits();
        List<Map<String,Object>> list = new ArrayList<>();
        for(SearchHit hit : searchHits){
           list.add(hit.getSourceAsMap());
        }
        return list;
    }
           

4.總結

(1)在使用Java用戶端操作Es時,個人感覺先在kibana中用文法進行測試,然後再通過Java代碼實作,根據文法的層級關系,可以很容易地寫出代碼

(2)查詢的分頁是有預設值的,from預設值為0,size的預設值為10,是以在不設定分頁的情況下,預設傳回10條資料

繼續閱讀