亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Elasticsearch QueryBuilder簡單查詢實現解析

發布時間:2020-08-25 04:00:30 來源:腳本之家 閱讀:605 作者:晨曦曙光 欄目:編程語言

elasticsearch中存儲的全部文檔

Elasticsearch QueryBuilder簡單查詢實現解析

1.matchAllQuery()

matchAllQuery()方法用來匹配全部文檔

public class QueryTest { 
  public static void main(String[] args) { 
    //創建對象,設置集群名稱和IP地址 
    ElasticsearchUtils es = new ElasticsearchUtils("im_shan", 
        "localhost"); 
    String indexName = "school";//索引名稱 
    String typeName = "student";//類型名稱 
    //搜索全部文檔 
    QueryBuilder queryBuilder = QueryBuilders.matchAllQuery(); 
    SearchResponse searchResponse = es.searcher(indexName, typeName, 
      queryBuilder); 
    SearchHits hits = searchResponse.getHits(); 
    SearchHit[] searchHits = hits.getHits(); 
    int i = 0; 
    for (SearchHit searchHit : searchHits) { 
      String name = (String) searchHit.getSource().get("name"); 
      String birth = (String) searchHit.getSource().get("birth"); 
      String interest = (String) searchHit.getSource().get("interest"); 
      System.out.println("-------------" + (++i) + "------------"); 
      System.out.println(name); 
      System.out.println(birth); 
      System.out.println(interest); 
    } 
 
  } 
}

控制臺將輸出全部文檔

輸出: 
-------------1------------ 
amy 
1995-11-10 
singing dancing 
-------------2------------ 
angle 
1996-05-01 
singing reading 
-------------3------------ 
jackson 
1995-08-01 
basketball music reading 
-------------4------------ 
jack 
1995-03-21 
football music 
-------------5------------ 
tom 
1995-09-11 
reading music

2.matchQuery(String name,Object text)

matchQuery("filedname","value")匹配單個字段,匹配字段名為filedname,值為value的文檔

java] view plain copy 
//單個匹配,搜索name為jack的文檔 
QueryBuilder queryBuilder = QueryBuilders.matchQuery("name", "jack"); 

此時會輸出一條記錄,因為只有一個name為jack的文檔

-------------1------------ 
jack 
1995-03-21 
football music 

3.multiMatchQuery(Object text, String... fieldNames)

多個字段匹配某一個值

[java] view plain copy
QueryBuilder queryBuilder = QueryBuilders.multiMatchQuery("music", 
      "name", "interest");//搜索name中或interest中包含有music的文檔(必須與music一致)

控制臺輸出如下

-------------1------------ 
jack 
1995-03-21 
football music 
-------------2------------ 
tom 
1995-09-11 
reading music 
-------------3------------ 
jackson 
1995-08-01 
basketball music reading

4.wildcardQuery()模糊查詢

模糊查詢,?匹配單個字符,*匹配多個字符

WildcardQueryBuilder queryBuilder = QueryBuilders.wildcardQuery("name", 
      "*jack*");//搜索名字中含有jack文檔(name中只要包含jack即可) 

 控制臺輸出如下

-------------1------------ 
jackson 
1995-08-01 
basketball music reading 
-------------2------------ 
jack 
1995-03-21 
football music

5.使用BoolQueryBuilder進行復合查詢

使用must

java] view plain copy
//模糊查詢 
WildcardQueryBuilder queryBuilder1 = QueryBuilders.wildcardQuery( 
      "name", "*jack*");//搜索名字中含有jack的文檔 
WildcardQueryBuilder queryBuilder2 = QueryBuilders.wildcardQuery( 
      "interest", "*read*");//搜索interest中含有read的文檔 
 
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); 
//name中必須含有jack,interest中必須含有read,相當于and 
boolQueryBuilder.must(queryBuilder1); 
boolQueryBuilder.must(queryBuilder2);

控制臺輸出如下(name中必須含有jack,interest中必須含有read,只有一條文檔匹配):

-------------1------------ 
jackson 
1995-08-01 
basketball music reading 

使用should

WildcardQueryBuilder queryBuilder1 = QueryBuilders.wildcardQuery( 
      "name", "*jack*");//搜索名字中含有jack的文檔 
WildcardQueryBuilder queryBuilder2 = QueryBuilders.wildcardQuery( 
      "interest", "*read*");//搜索interest中含有read的文檔 
 
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); 
//name中含有jack或者interest含有read,相當于or 
boolQueryBuilder.should(queryBuilder1); 
boolQueryBuilder.should(queryBuilder2);

控制臺輸出(name中含有jack或者interest中含有read的都將被匹配):

------------1------------ 
jackson 
1995-08-01 
basketball music reading 
-------------2------------ 
angle 
1996-05-01 
singing reading 
-------------3------------ 
jack 
1995-03-21 
football music 
-------------4------------ 
tom 
1995-09-11 
reading music

6.分頁

通過from和size參數進行分頁。From定義查詢結果開始位置,size定義返回的hits(一條hit對應一條記錄)最大數量。

Elasticsearch QueryBuilder簡單查詢實現解析

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

延寿县| 运城市| 宿松县| 平顺县| 合江县| 信丰县| 陵川县| 嵩明县| 凌源市| 洞口县| 佛学| 津市市| 镇宁| 曲阳县| 奉化市| 阳西县| 阿瓦提县| 葫芦岛市| 陆河县| 永善县| 双流县| 年辖:市辖区| 鱼台县| 台中县| 都兰县| 报价| 璧山县| 临沭县| 巩留县| 威远县| 浙江省| 昌平区| 海丰县| 天镇县| 淮阳县| 萝北县| 六盘水市| 大洼县| 印江| 保康县| 托里县|