91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

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

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

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

巴中市| 阜宁县| 延吉市| 达拉特旗| 榆中县| 江山市| 博爱县| 治县。| 闽侯县| 博野县| 嘉义县| 翁牛特旗| 南昌县| 阿拉尔市| 澄城县| 辽宁省| 三台县| 穆棱市| 台山市| 垫江县| 尼勒克县| 柳江县| 凉山| 太和县| 平湖市| 凤阳县| 吉首市| 霍邱县| 兴和县| 鹿泉市| 永寿县| 冷水江市| 泸溪县| 延吉市| 阳城县| 土默特右旗| 呼玛县| 手游| 东源县| 翼城县| 环江|