elasticsearch中存儲(chǔ)的全部文檔
創(chuàng)新互聯(lián)建站長期為千余家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為吉安企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站,吉安網(wǎng)站改版等技術(shù)服務(wù)。擁有十載豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
1.matchAllQuery()
matchAllQuery()方法用來匹配全部文檔
public class QueryTest { public static void main(String[] args) { //創(chuàng)建對(duì)象,設(shè)置集群名稱和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); } } }
控制臺(tái)將輸出全部文檔
輸出: -------------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")匹配單個(gè)字段,匹配字段名為filedname,值為value的文檔
java] view plain copy //單個(gè)匹配,搜索name為jack的文檔 QueryBuilder queryBuilder = QueryBuilders.matchQuery("name", "jack");
此時(shí)會(huì)輸出一條記錄,因?yàn)橹挥幸粋€(gè)name為jack的文檔
-------------1------------ jack 1995-03-21 football music
3.multiMatchQuery(Object text, String... fieldNames)
多個(gè)字段匹配某一個(gè)值
[java] view plain copy QueryBuilder queryBuilder = QueryBuilders.multiMatchQuery("music", "name", "interest");//搜索name中或interest中包含有music的文檔(必須與music一致)
控制臺(tái)輸出如下
-------------1------------ jack 1995-03-21 football music -------------2------------ tom 1995-09-11 reading music -------------3------------ jackson 1995-08-01 basketball music reading
4.wildcardQuery()模糊查詢
模糊查詢,?匹配單個(gè)字符,*匹配多個(gè)字符
WildcardQueryBuilder queryBuilder = QueryBuilders.wildcardQuery("name", "*jack*");//搜索名字中含有jack文檔(name中只要包含jack即可)
控制臺(tái)輸出如下
-------------1------------ jackson 1995-08-01 basketball music reading -------------2------------ jack 1995-03-21 football music
5.使用BoolQueryBuilder進(jìn)行復(fù)合查詢
使用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,相當(dāng)于and boolQueryBuilder.must(queryBuilder1); boolQueryBuilder.must(queryBuilder2);
控制臺(tái)輸出如下(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,相當(dāng)于or boolQueryBuilder.should(queryBuilder1); boolQueryBuilder.should(queryBuilder2);
控制臺(tái)輸出(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參數(shù)進(jìn)行分頁。From定義查詢結(jié)果開始位置,size定義返回的hits(一條hit對(duì)應(yīng)一條記錄)最大數(shù)量。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。