在上一篇中介紹了ElasticSearch實戰系列二: ElasticSearch的DSL語句使用教程---圖文詳解,本篇文章就來說解下 ElasticSearch 6.x官方Java API的使用。html
目前市面上有幾種常見的ElasticSearch Java API架包,JestClient、SpringBoot整合的SpringData、Spring整合的ElasticsearchTemplate、Elasticsearch Bboss等一些開源架包,上述這些第三方整合的架包中,基本已經支持平常的使用,除了支持的ES版本會低一些而已。java
本文介紹的是ElasticSearch官方的Java High Level REST Client
的使用,Java High Level REST Client
是ElasticSearch官方目前推薦使用的,適用於6.x以上的版本,要求JDK在1.8以上,能夠很好的在大版本中進行兼容,而且該架包自身也包含Java Low Level REST Client
中的方法,能夠應對一些特需的狀況進行特殊的處理, 它對於一些經常使用的方法封裝Restful風格,能夠直接對應操做名調用使用便可,支持同步和異步(Async)調用。git
這裏咱們的使用也能夠直接對應上一篇文章中的DSL語句使用,這樣的話能夠很是方便的對照和學習使用。github
在對下述進行操做時,咱們先來看下Elasticsearch Java High Level REST Client
的初始化鏈接寫法吧。json
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost(elasticIp, elasticPort)));
是否是很簡單呢,關閉也很簡單,client不爲空直接close便可!api
ElasticSearch能夠直接新增數據,只要你指定了index(索引庫名稱)和type(類型)便可。在新增的時候你能夠本身指定主鍵ID,也能夠不指定,由 ElasticSearch自身生成。Elasticsearch Java High Level REST Client
新增數據提供了三種方法,這裏咱們就來看一下這三種寫法吧。緩存
新增數據代碼示例一,經過jsonString進行建立:app
String index = "test1"; String type = "_doc"; // 惟一編號 String id = "1"; IndexRequest request = new IndexRequest(index, type, id); String jsonString = "{" + "\"uid\":\"1234\","+ "\"phone\":\"12345678909\","+ "\"msgcode\":\"1\"," + "\"sendtime\":\"2019-03-14 01:57:04\"," + "\"message\":\"xuwujing study Elasticsearch\"" + "}"; request.source(jsonString, XContentType.JSON); IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
新增數據代碼示例二,經過map建立,會自動轉換成json的數據:異步
String index = "test1"; String type = "_doc"; // 惟一編號 String id = "1"; IndexRequest request = new IndexRequest(index, type, id); Map<String, Object> jsonMap = new HashMap<>(); jsonMap.put("uid", 1234); jsonMap.put("phone", 12345678909L); jsonMap.put("msgcode", 1); jsonMap.put("sendtime", "2019-03-14 01:57:04"); jsonMap.put("message", "xuwujing study Elasticsearch"); request.source(jsonMap); IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
新增數據代碼示例三,經過XContentBuilder對象進行建立:elasticsearch
String index = "test1"; String type = "_doc"; // 惟一編號 String id = "1"; IndexRequest request = new IndexRequest(index, type, id); XContentBuilder builder = XContentFactory.jsonBuilder(); builder.startObject(); { builder.field("uid", 1234); builder.field("phone", 12345678909L); builder.field("msgcode", 1); builder.timeField("sendtime", "2019-03-14 01:57:04"); builder.field("message", "xuwujing study Elasticsearch"); } builder.endObject(); request.source(builder); IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
上述三種方法中,我的推薦第二種,比較容易理解和使用。
在上述示例中,咱們經過直接經過建立數據從而建立了索引庫,可是沒有建立索引庫而經過ES自身生成的這種並不友好,由於它會使用默認的配置,字段結構都是text(text的數據會分詞,在存儲的時候也會額外的佔用空間),分片和索引副本採用默認值,默認是5和1,ES的分片數在建立以後就不能修改,除非reindex,因此這裏咱們仍是指定數據模板進行建立。
使用JAVA API 建立索引庫的方法和上述中新增數據的同樣,有三種方式,不過這裏就只介紹一種。
新增索引庫的代碼示例:
private static void createIndex() throws IOException { String type = "_doc"; String index = "test1"; // setting 的值 Map<String, Object> setmapping = new HashMap<>(); // 分區數、副本數、緩存刷新時間 setmapping.put("number_of_shards", 10); setmapping.put("number_of_replicas", 1); setmapping.put("refresh_interval", "5s"); Map<String, Object> keyword = new HashMap<>(); //設置類型 keyword.put("type", "keyword"); Map<String, Object> lon = new HashMap<>(); //設置類型 lon.put("type", "long"); Map<String, Object> date = new HashMap<>(); //設置類型 date.put("type", "date"); date.put("format", "yyyy-MM-dd HH:mm:ss"); Map<String, Object> jsonMap2 = new HashMap<>(); Map<String, Object> properties = new HashMap<>(); //設置字段message信息 properties.put("uid", lon); properties.put("phone", lon); properties.put("msgcode", lon); properties.put("message", keyword); properties.put("sendtime", date); Map<String, Object> mapping = new HashMap<>(); mapping.put("properties", properties); jsonMap2.put(type, mapping); GetIndexRequest getRequest = new GetIndexRequest(); getRequest.indices(index); getRequest.local(false); getRequest.humanReadable(true); boolean exists2 = client.indices().exists(getRequest, RequestOptions.DEFAULT); //若是存在就不建立了 if(exists2) { System.out.println(index+"索引庫已經存在!"); return; } // 開始建立庫 CreateIndexRequest request = new CreateIndexRequest(index); try { // 加載數據類型 request.settings(setmapping); //設置mapping參數 request.mapping(type, jsonMap2); //設置別名 request.alias(new Alias("pancm_alias")); CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT); boolean falg = createIndexResponse.isAcknowledged(); if(falg){ System.out.println("建立索引庫:"+index+"成功!" ); } } catch (IOException e) { e.printStackTrace(); } }
注:建立索引庫的時候,必定要先判斷索引庫是否存在!!!
這裏建立索引庫的時候順便也指定了別名(alias),這個別名是一個好東西,使用恰當能夠提高查詢性能,這裏咱們留着下次在講。
ES提供修改API的時候,有兩種方式,一種是直接修改,可是若數據不存在會拋出異常,另外一種則是存在更新,不存着就插入。相比第一種,第二種會更加好用一些,不過在寫入速度上是不如第一種的。
ES修改的代碼示例:
private static void update() throws IOException { String type = "_doc"; String index = "test1"; // 惟一編號 String id = "1"; UpdateRequest upateRequest = new UpdateRequest(); upateRequest.id(id); upateRequest.index(index); upateRequest.type(type); // 依舊可使用Map這種集合做爲更新條件 Map<String, Object> jsonMap = new HashMap<>(); jsonMap.put("uid", 12345); jsonMap.put("phone", 123456789019L); jsonMap.put("msgcode", 2); jsonMap.put("sendtime", "2019-03-14 01:57:04"); jsonMap.put("message", "xuwujing study Elasticsearch"); upateRequest.doc(jsonMap); // upsert 方法表示若是數據不存在,那麼就新增一條 upateRequest.docAsUpsert(true); client.update(upateRequest, RequestOptions.DEFAULT); System.out.println("更新成功!"); }
注:upsert 方法表示若是數據不存在,那麼就新增一條,默認是false。
根據上述的幾個操做,想必不用多說,已經知道了是DELETE方法了,那咱們就直接開始吧。
ES根據ID刪除代碼示例:
private static void delete() throws IOException { String type = "_doc"; String index = "test1"; // 惟一編號 String id = "1"; DeleteRequest deleteRequest = new DeleteRequest(); deleteRequest.id(id); deleteRequest.index(index); deleteRequest.type(type); // 設置超時時間 deleteRequest.timeout(TimeValue.timeValueMinutes(2)); // 設置刷新策略"wait_for" // 保持此請求打開,直到刷新使此請求的內容能夠搜索爲止。此刷新策略與高索引和搜索吞吐量兼容,但它會致使請求等待響應,直到發生刷新 deleteRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL); // 同步刪除 DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT); }
ES根據條件進行刪除:
private static void deleteByQuery() throws IOException { String type = "_doc"; String index = "test1"; DeleteByQueryRequest request = new DeleteByQueryRequest(index,type); // 設置查詢條件 request.setQuery(QueryBuilders.termsQuery("uid",1234)); // 同步執行 BulkByScrollResponse bulkResponse = client.deleteByQuery(request, RequestOptions.DEFAULT); }
示例圖:
幾個經常使用的查詢API這裏就簡單的介紹下用法,而後再直接給出全部的查詢語句代碼。
private static void allSearch() throws IOException { SearchRequest searchRequestAll = new SearchRequest(); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(QueryBuilders.matchAllQuery()); searchRequestAll.source(searchSourceBuilder); // 同步查詢 SearchResponse searchResponseAll = client.search(searchRequestAll, RequestOptions.DEFAULT); System.out.println("全部查詢總數:" + searchResponseAll.getHits().getTotalHits()); }
其實就是等值查詢,只不過在裏面加入了分頁、排序、超時、路由等等設置,而且在查詢結果裏面增長了一些處理。
private static void genSearch() throws IOException { String type = "_doc"; String index = "test1"; // 查詢指定的索引庫 SearchRequest searchRequest = new SearchRequest(index); searchRequest.types(type); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); // 設置查詢條件 sourceBuilder.query(QueryBuilders.termQuery("uid", "1234")); // 設置起止和結束 sourceBuilder.from(0); sourceBuilder.size(5); sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS)); // 設置路由 // searchRequest.routing("routing"); // 設置索引庫表達式 searchRequest.indicesOptions(IndicesOptions.lenientExpandOpen()); // 查詢選擇本地分片,默認是集羣分片 searchRequest.preference("_local"); // 排序 // 根據默認值進行降序排序 // sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC)); // 根據字段進行升序排序 // sourceBuilder.sort(new FieldSortBuilder("id").order(SortOrder.ASC)); // 關閉suorce查詢 // sourceBuilder.fetchSource(false); String[] includeFields = new String[]{"title", "user", "innerObject.*"}; String[] excludeFields = new String[]{"_type"}; // 包含或排除字段 // sourceBuilder.fetchSource(includeFields, excludeFields); searchRequest.source(sourceBuilder); System.out.println("普通查詢的DSL語句:"+sourceBuilder.toString()); // 同步查詢 SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); // HTTP狀態代碼、執行時間或請求是否提早終止或超時 RestStatus status = searchResponse.status(); TimeValue took = searchResponse.getTook(); Boolean terminatedEarly = searchResponse.isTerminatedEarly(); boolean timedOut = searchResponse.isTimedOut(); // 供關於受搜索影響的切分總數的統計信息,以及成功和失敗的切分 int totalShards = searchResponse.getTotalShards(); int successfulShards = searchResponse.getSuccessfulShards(); int failedShards = searchResponse.getFailedShards(); // 失敗的緣由 for (ShardSearchFailure failure : searchResponse.getShardFailures()) { // failures should be handled here } // 結果 searchResponse.getHits().forEach(hit -> { Map<String, Object> map = hit.getSourceAsMap(); System.out.println("普通查詢的結果:" + map); }); System.out.println("\n=================\n"); }
其實這個或查詢也是bool查詢中的一種,這裏的查詢語句至關於SQL語句中的
SELECT * FROM test1 where (uid = 1 or uid =2) and phone = 12345678919
代碼示例:
private static void orSearch() throws IOException { SearchRequest searchRequest = new SearchRequest(); searchRequest.indices("test1"); searchRequest.types("_doc"); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder(); BoolQueryBuilder boolQueryBuilder2 = new BoolQueryBuilder(); /** * SELECT * FROM test1 where (uid = 1234 or uid =12345) and phone = 12345678909 * */ boolQueryBuilder2.should(QueryBuilders.termQuery("uid", 1234)); boolQueryBuilder2.should(QueryBuilders.termQuery("uid", 12345)); boolQueryBuilder.must(boolQueryBuilder2); boolQueryBuilder.must(QueryBuilders.termQuery("phone", "12345678909")); searchSourceBuilder.query(boolQueryBuilder); System.out.println("或查詢語句:" + searchSourceBuilder.toString()); searchRequest.source(searchSourceBuilder); // 同步查詢 SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); searchResponse.getHits().forEach(documentFields -> { System.out.println("查詢結果:" + documentFields.getSourceAsMap()); }); }
至關於SQL語句中的like查詢。
private static void likeSearch() throws IOException { String type = "_doc"; String index = "test1"; SearchRequest searchRequest = new SearchRequest(); searchRequest.indices(index); searchRequest.types(type); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder(); /** * SELECT * FROM p_test where message like '%xu%'; * */ boolQueryBuilder.must(QueryBuilders.wildcardQuery("message", "*xu*")); searchSourceBuilder.query(boolQueryBuilder); System.out.println("模糊查詢語句:" + searchSourceBuilder.toString()); searchRequest.source(searchSourceBuilder); // 同步查詢 SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); searchResponse.getHits().forEach(documentFields -> { System.out.println("模糊查詢結果:" + documentFields.getSourceAsMap()); }); System.out.println("\n=================\n"); }
也就是至關於SQL語句中的in查詢。
private static void inSearch() throws IOException { String type = "_doc"; String index = "test1"; // 查詢指定的索引庫 SearchRequest searchRequest = new SearchRequest(index,type); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); /** * SELECT * FROM p_test where uid in (1,2) * */ // 設置查詢條件 sourceBuilder.query(QueryBuilders.termsQuery("uid", 1, 2)); searchRequest.source(sourceBuilder); System.out.println("in查詢的DSL語句:"+sourceBuilder.toString()); // 同步查詢 SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); // 結果 searchResponse.getHits().forEach(hit -> { Map<String, Object> map = hit.getSourceAsMap(); String string = hit.getSourceAsString(); System.out.println("in查詢的Map結果:" + map); System.out.println("in查詢的String結果:" + string); }); System.out.println("\n=================\n"); }
判斷是否存在該字段,用法和SQL語句中的exist相似。
private static void existSearch() throws IOException { String type = "_doc"; String index = "test1"; // 查詢指定的索引庫 SearchRequest searchRequest = new SearchRequest(index); searchRequest.types(type); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); // 設置查詢條件 sourceBuilder.query(QueryBuilders.existsQuery("msgcode")); searchRequest.source(sourceBuilder); System.out.println("存在查詢的DSL語句:"+sourceBuilder.toString()); // 同步查詢 SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); // 結果 searchResponse.getHits().forEach(hit -> { Map<String, Object> map = hit.getSourceAsMap(); String string = hit.getSourceAsString(); System.out.println("存在查詢的Map結果:" + map); System.out.println("存在查詢的String結果:" + string); }); System.out.println("\n=================\n"); }
和SQL語句中<>使用方法同樣,其中gt是大於,lt是小於,gte是大於等於,lte是小於等於。
private static void rangeSearch() throws IOException{ String type = "_doc"; String index = "test1"; SearchRequest searchRequest = new SearchRequest(index); searchRequest.types(type); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); // 設置查詢條件 sourceBuilder.query(QueryBuilders.rangeQuery("sendtime").gte("2019-01-01 00:00:00").lte("2019-12-31 23:59:59")); searchRequest.source(sourceBuilder); System.out.println("範圍查詢的DSL語句:"+sourceBuilder.toString()); // 同步查詢 SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); // 結果 searchResponse.getHits().forEach(hit -> { String string = hit.getSourceAsString(); System.out.println("範圍查詢的String結果:" + string); }); System.out.println("\n=================\n"); }
ES可使用正則進行查詢,查詢方式也很是的簡單,代碼示例以下:
private static void regexpSearch() throws IOException{ String type = "_doc"; String index = "test1"; // 查詢指定的索引庫 SearchRequest searchRequest = new SearchRequest(index); searchRequest.types(type); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); // 設置查詢條件 sourceBuilder.query(QueryBuilders.regexpQuery("message","xu[0-9]")); searchRequest.source(sourceBuilder); System.out.println("正則查詢的DSL語句:"+sourceBuilder.toString()); // 同步查詢 SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); // 結果 searchResponse.getHits().forEach(hit -> { Map<String, Object> map = hit.getSourceAsMap(); String string = hit.getSourceAsString(); System.out.println("正則查詢的Map結果:" + map); System.out.println("正則查詢的String結果:" + string); }); System.out.println("\n=================\n"); }
全部查詢總數:6
普通查詢的DSL語句:{"from":0,"size":5,"timeout":"60s","query":{"term":{"uid":{"value":"1234","boost":1.0}}}}=================
或查詢語句:{"query":{"bool":{"must":[{"bool":{"should":[{"term":{"uid":{"value":1234,"boost":1.0}}},{"term":{"uid":{"value":12345,"boost":1.0}}}],"adjust_pure_negative":true,"boost":1.0}},{"term":{"phone":{"value":"12345678909","boost":1.0}}}],"adjust_pure_negative":true,"boost":1.0}}}
或查詢結果:{msgcode=1, uid=12345, phone=12345678909, message=qq, sendtime=2019-03-14 01:57:04}=================
模糊查詢語句:{"query":{"bool":{"must":[{"wildcard":{"message":{"wildcard":"xu","boost":1.0}}}],"adjust_pure_negative":true,"boost":1.0}}}
模糊查詢結果:{msgcode=2, uid=12345, phone=123456789019, sendtime=2019-03-14 01:57:04, message=xuwujing study Elasticsearch}
模糊查詢結果:{uid=123456, phone=12345678909, message=xu1, sendtime=2019-03-14 01:57:04}=================
存在查詢的DSL語句:{"query":{"exists":{"field":"msgcode","boost":1.0}}}
存在查詢的Map結果:{msgcode=2, uid=12345, phone=123456789019, sendtime=2019-03-14 01:57:04, message=xuwujing study Elasticsearch}
存在查詢的String結果:{"uid":12345,"phone":123456789019,"msgcode":2,"sendtime":"2019-03-14 01:57:04","message":"xuwujing study Elasticsearch"}
存在查詢的Map結果:{msgcode=1, uid=12345, phone=12345678909, message=qq, sendtime=2019-03-14 01:57:04}
存在查詢的String結果:{"uid":"12345","phone":"12345678909","message":"qq","msgcode":"1","sendtime":"2019-03-14 01:57:04"}=================
範圍查詢的DSL語句:{"query":{"range":{"sendtime":{"from":"2019-01-01 00:00:00","to":"2019-12-31 23:59:59","include_lower":true,"include_upper":true,"boost":1.0}}}}
範圍查詢的String結果:{"uid":12345,"phone":123456789019,"msgcode":2,"sendtime":"2019-03-14 01:57:04","message":"xuwujing study Elasticsearch"}
範圍查詢的String結果:{"uid":"123456","phone":"12345678909","message":"xu1","sendtime":"2019-03-14 01:57:04"}
範圍查詢的String結果:{"uid":"12345","phone":"12345678909","message":"qq","msgcode":"1","sendtime":"2019-03-14 01:57:04"}=================
正則查詢的DSL語句:{"query":{"regexp":{"message":{"value":"xu[0-9]","flags_value":65535,"max_determinized_states":10000,"boost":1.0}}}}
正則查詢的Map結果:{uid=123456, phone=12345678909, message=xu1, sendtime=2019-03-14 01:57:04}
正則查詢的String結果:{"uid":"123456","phone":"12345678909","message":"xu1","sendtime":"2019-03-14 01:57:04"}=================
組合查詢的DSL語句:{"query":{"bool":{"must":[{"term":{"uid":{"value":12345,"boost":1.0}}},{"term":{"msgcode":{"value":1,"boost":1.0}}}],"adjust_pure_negative":true,"boost":1.0}}}
組合查詢的String結果:{"uid":"12345","phone":"12345678909","message":"qq","msgcode":"1","sendtime":"2019-03-14 01:57:04"}=================
參考ES官方文檔:
https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high.html
關於SpringBoot集成ElasticSearch和JestClient的使用能夠查看這篇文章:SpringBoot整合ElasticSearch實現多版本的兼容
關於ElasticSearch Java API的選擇,若是ElasticSearch版本在6.x之前的話,推薦使用JestClient。若是是6.x以後而且有意升級到7.x的話,那麼直接使用ES官方的Java High Level REST Client
,由於在7.x以後將直接會捨棄Transport client
的鏈接方式,目前Spring和SpringBoot集成的ES就是使用該方式(不知後續是否會作調整)。
本篇文章的代碼已收錄在本人的java-study項目中,如有興趣,歡迎star、fork和issues。
項目地址:https://github.com/xuwujing/java-study
ElasticSearch實戰系列:
ElasticSearch實戰系列一: ElasticSearch集羣+Kinaba安裝教程
ElasticSearch實戰系列二: ElasticSearch的DSL語句使用教程---圖文詳解
原創不易,若是感受不錯,但願給個推薦!您的支持是我寫做的最大動力!
版權聲明:
做者:虛無境
博客園出處:http://www.cnblogs.com/xuwujing
CSDN出處:http://blog.csdn.net/qazwsxpcm
我的博客出處:http://www.panchengming.com