elasticsearch中存儲的所有文檔java
1.matchAllQuery()ui
matchAllQuery()方法用來匹配所有文檔spa
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); } } }
控制檯將輸出所有文檔3d
輸出: -------------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)rest
matchQuery("filedname","value")匹配單個字段,匹配字段名爲filedname,值爲value的文檔code
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)blog
多個字段匹配某一個值索引
[java] view plain copy QueryBuilder queryBuilder = QueryBuilders.multiMatchQuery("music", "name", "interest");//搜索name中或interest中包含有music的文檔(必須與music一致)
控制檯輸出以下ci
-------------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對應一條記錄)最大數量。