您好,登錄后才能下訂單哦!
elasticsearch中存儲的全部文檔
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對應一條記錄)最大數量。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。