摘要:一依賴剛開(kāi)始少這個(gè)包創(chuàng)建索引失敗官方文檔并沒(méi)有給這個(gè)提示二開(kāi)始之前的準(zhǔn)備官方文檔連接操作所用到的實(shí)體類三關(guān)于索引的操作官方文檔新增索引索引名稱分片副本內(nèi)容查詢指定索引索引名稱刪除索引四關(guān)于文檔的操作官方文檔創(chuàng)建文檔索引名稱前
一、Maven依賴
二、開(kāi)始之前的準(zhǔn)備org.elasticsearch elasticsearch 7.1.0 org.elasticsearch.client elasticsearch-rest-high-level-client 7.1.0
官方文檔
/** * 連接ES * @return */ public RestHighLevelClient start() { RestHighLevelClient restHighLevelClient = new RestHighLevelClient( RestClient.builder( new HttpHost("192.168.100.151", 9201, "http"), new HttpHost("192.168.100.151", 9202, "http"), new HttpHost("192.168.100.151", 9203, "http"))); return restHighLevelClient; } /** * 操作所用到的實(shí)體類 */ @Data class Article{ private long id; private String title; public Article(long id, String title) { this.id = id; this.title = title; } }三、關(guān)于索引的操作
官方文檔
新增索引
public void createIndex(RestHighLevelClient client) { //索引名稱 CreateIndexRequest request = new CreateIndexRequest("hello"); //分片副本 request.settings(Settings.builder().put("index.number_of_shards", 5) .put("index.number_of_replicas", 1)); //內(nèi)容 Mapid = new HashMap <>(); id.put("type","text"); id.put("store",true); Map title = new HashMap <>(); title.put("type","text"); title.put("store",true); title.put("index",true); title.put("analyzer", "standard"); Map properties = new HashMap <>(); properties.put("id",id); properties.put("title",title); Map mapping = new HashMap <>(); mapping.put("properties",properties); request.mapping(mapping); try { CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT); System.out.println(response.toString()); } catch (IOException e) { e.printStackTrace(); } }
查詢指定索引
public void getIndex(RestHighLevelClient client) throws IOException { //索引名稱 GetIndexRequest request = new GetIndexRequest("hello"); try { boolean exists = client.indices().exists(request, RequestOptions.DEFAULT); System.out.println(exists); } catch (IOException e) { e.printStackTrace(); } }
刪除索引
public void delIndex(RestHighLevelClient client){ DeleteIndexRequest request = new DeleteIndexRequest("hello"); try { AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT); System.out.println(delete.toString()); } catch (IOException e) { e.printStackTrace(); } }四、關(guān)于文檔的操作
官方文檔
創(chuàng)建文檔
public void createDocument(RestHighLevelClient client){ //索引名稱 IndexRequest indexRequest = new IndexRequest("hello"); ObjectMapper mapper = new ObjectMapper(); Article article = new Article(3L, "web前端"); byte[] json = new byte[0]; try { json = mapper.writeValueAsBytes(article); //可以設(shè)置文章ID indexRequest.id("5"); indexRequest.source(json, XContentType.JSON); IndexResponse index = client.index(indexRequest, RequestOptions.DEFAULT); } catch (Exception e) { e.printStackTrace(); } }
根據(jù)文檔ID查詢文檔
public void getDocument(RestHighLevelClient client){ GetRequest getRequest = new GetRequest("hello", "1"); GetResponse documentFields = null; try { documentFields = client.get(getRequest, RequestOptions.DEFAULT); System.out.println(documentFields.toString()); } catch (IOException e) { e.printStackTrace(); } }
更新文檔
public void updateDocument(RestHighLevelClient client){ UpdateRequest updateRequest = new UpdateRequest("hello", "1"); Article article = new Article(2L, "java入門到放棄"); ObjectMapper mapper = new ObjectMapper(); byte[] json = new byte[0]; try { json = mapper.writeValueAsBytes(article); IndexRequest indexRequest = new IndexRequest("hello"); indexRequest.source(json, XContentType.JSON); updateRequest.doc(indexRequest); UpdateResponse updateResponse = client.update( updateRequest, RequestOptions.DEFAULT); System.out.println(updateResponse); } catch (Exception e) { e.printStackTrace(); } }
刪除文檔
public void delDocument(RestHighLevelClient client){ DeleteRequest request = new DeleteRequest("hello", "1"); try { DeleteResponse deleteResponse = client.delete( request, RequestOptions.DEFAULT); System.out.println(deleteResponse); } catch (IOException e) { e.printStackTrace(); } }
查詢文檔
public void searchDocument(RestHighLevelClient client){ SearchRequest searchRequest = new SearchRequest(); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(QueryBuilders.matchAllQuery()); searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = null; try { searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); System.out.println(searchResponse.toString()); } catch (IOException e) { e.printStackTrace(); } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/75357.html
摘要:時(shí)間年月日星期四說(shuō)明本文部分內(nèi)容均來(lái)自慕課網(wǎng)。那么里面的數(shù)據(jù)就可以分為各種各樣的索引,比如汽車索引圖書索引家具索引等等。圖書索引又可以細(xì)分為各種類型,比如科普類小說(shuō)類技術(shù)類等等。具體到每一本書籍,就是文檔,就是整個(gè)圖書里面最小的存儲(chǔ)單位。 時(shí)間:2017年09月14日星期四說(shuō)明:本文部分內(nèi)容均來(lái)自慕課網(wǎng)。@慕課網(wǎng):http://www.imooc.com教學(xué)源碼:無(wú)學(xué)習(xí)源碼:https...
摘要:當(dāng)時(shí)自己在本地測(cè)試搭建集群后,給分配了另外一個(gè)任務(wù)就是去了解中的自帶分詞英文分詞中文分詞的相同與差異以及自己建立分詞需要注意的點(diǎn)。還有就是官網(wǎng)的文檔了,非常非常詳細(xì),還有,版本的是有中文的官方文檔,可以湊合著看。 前提 人工智能、大數(shù)據(jù)快速發(fā)展的今天,對(duì)于 TB 甚至 PB 級(jí)大數(shù)據(jù)的快速檢索已然成為剛需,大型企業(yè)早已淹沒(méi)在系統(tǒng)生成的浩瀚數(shù)據(jù)流當(dāng)中。大數(shù)據(jù)技術(shù)業(yè)已集中在如何存儲(chǔ)和處理這...
閱讀 3054·2021-11-25 09:43
閱讀 1650·2021-11-24 11:15
閱讀 2370·2021-11-22 15:25
閱讀 3515·2021-11-11 16:55
閱讀 3253·2021-11-04 16:10
閱讀 2785·2021-09-14 18:02
閱讀 1697·2021-09-10 10:50
閱讀 1081·2019-08-29 15:39