好程序員Java學(xué)習(xí)路線分享創(chuàng)建Java class,首先通過Transport Client獲取ES的連接
開平ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!
private Client client;
//通過Transport Client獲取ES的連接
@Before
public void getClient() throws Exception{
????//ES服務(wù)的JavaAPI的port為9300
????//注意:如果請求一個ES集群,可以多添幾個節(jié)點
????//為了避免在一個節(jié)點出現(xiàn)網(wǎng)絡(luò)問題導(dǎo)致的請求失敗問題,可以自動切換另外一個節(jié)點
????client = TransportClient.builder().build()
????????????.addTransportAddress(new InetSocketTransportAddress(
????????????????????InetAddress.getByName("localhost"),9300));
????????????//.addTransportAddress(...);
}
操作命令
----------------------------------------新建文檔------------------------------------
@Test
public void createDocument1(){
????// json格式的數(shù)據(jù)
????//json "需要轉(zhuǎn)義 -> \
????String source = "{" +
????????????"\"id\":\"1\"," +
????????????"\"title\":\"Lucene是一套用于全文檢索和搜尋的開源程式庫\"," +
????????????"\"content\":\"Lucene提供了一個簡單卻強大的應(yīng)用程式接口,能夠做全文索引和搜尋\"" +
????????????"}";
????//創(chuàng)建文檔, 定義索引名稱,文檔類型,主鍵唯一標(biāo)識ID
????//execute().actionGet() == get() ?代表立刻執(zhí)行
????IndexResponse indexResponse =
????????????client.prepareIndex("blog", "article", "1")
????????????????????.setSource(source).get(); //加載數(shù)據(jù)并觸發(fā)
????this.getResponse(indexResponse); ?//對應(yīng)下面封裝信息
????client.close();
}
將打印信息封裝成類,方便this調(diào)用
//獲取響應(yīng)信息
private void getResponse(IndexResponse indexResponse) {
????System.out.println("索引名稱: " + indexResponse.getIndex());
????System.out.println("文檔類型: "+indexResponse.getType());
????System.out.println("ID: "+indexResponse.getId());
????System.out.println("版本: "+indexResponse.getVersion());
????System.out.println("是否創(chuàng)建成功: "+indexResponse.isCreated());
}
@Test
public void createDocument2(){
????// map類型的數(shù)據(jù)
????Map<String,Object> source = new HashMap<>();
????source.put("id","2");
????source.put("title","ElasticSearch");
????source.put("content","是一個分布式的 RESTful 風(fēng)格的搜索和數(shù)據(jù)分析引擎");
????//創(chuàng)建文檔
????IndexResponse indexResponse = client.prepareIndex("blog", "article", "2")
????????????.setSource(source).get();
????this.getResponse(indexResponse);
????client.close();
}
使用ES幫助類(執(zhí)行類),創(chuàng)建文檔
@Test
public void createDocument3() throws Exception{
????XContentBuilder source = XContentFactory.jsonBuilder()
????????????.startObject() //封裝數(shù)據(jù)
????????????????.field("id","3")
????????????????.field("title","ES的核心")
????????????????.field("content","集中的是巴拉巴拉")
????????????.endObject();
????System.out.println(source.toString());
????//創(chuàng)建文檔
????IndexResponse indexResponse = client.prepareIndex("blog", "article", "3")
????????????.setSource(source).get();
????this.getResponse(indexResponse);
????client.close();
}
----------------------------------------搜索文檔------------------------------------
輸出格式為json格式
搜索文檔數(shù)據(jù) -- 單個索引 ?prepareGet
@Test
public void testGetData1(){
????GetResponse getResponse = client.prepareGet("blog", "article", "1").get();
????System.out.println(getResponse.getSourceAsString());
????client.close(); ??//輸出格式為json格式
}
搜索文檔數(shù)據(jù) -- 多個索引 ?prepareMultiGet
@Test
public void testGetData2(){
????MultiGetResponse multiGetResponse = client.prepareMultiGet()
????????????.add("blog", "article", "1")
????????????.add("blog", "article", "2", "3")
????????????.get();
????for (MultiGetItemResponse itemResponse : multiGetResponse){
????????GetResponse response = itemResponse.getResponse();
????????if (response.isExists()){
????????????System.out.println(response.getSourceAsString());
????????}
????}
????client.close();
}
----------------------------------------更新文檔------------------------------------
doc更新
創(chuàng)建更新對象
@Test
public void testUpdate1() throws Exception{
????UpdateRequest request = new UpdateRequest();
????request.index("blog");
????request.type("article");
????request.id("1");
????request.doc(XContentFactory.jsonBuilder() //doc 更新方法
????.startObject()
????????????.field("id","1")
????????????.field("title","更新:1")
????????????.field("content","更新:1")
????.endObject());
????UpdateResponse updateResponse = client.update(request).get();
????System.out.println("索引名稱: " + updateResponse.getIndex());
????System.out.println("文檔類型: "+updateResponse.getType());
????System.out.println("ID: "+updateResponse.getId());
????System.out.println("版本: "+updateResponse.getVersion());
????System.out.println("是否創(chuàng)建成功: "+updateResponse.isCreated()); //false
????client.close();
}
直接調(diào)用client.update
@Test
public void testUpdate2() throws Exception{
????UpdateResponse updateResponse =
????????????client.update(new UpdateRequest("blog", "article", "2")
????????????????????.doc(XContentFactory.jsonBuilder() //doc 更新方法
????????????????????????.startObject()
????????????????????????????.field("id", "2")
????????????????????????????.field("title", "更新:2")
????????????????????????????.field("content", "更新:2")
????????????????????????.endObject()))
????????????????????.get();
????System.out.println("索引名稱: " + updateResponse.getIndex());
????System.out.println("文檔類型: "+updateResponse.getType());
????System.out.println("ID: "+updateResponse.getId());
????System.out.println("版本: "+updateResponse.getVersion());
????System.out.println("是否創(chuàng)建成功: "+updateResponse.isCreated()); //false
????client.close();
}
創(chuàng)建并更新
@Test
public void testUpdate3() throws Exception{
????// 設(shè)置一個查詢的條件,使用ID查詢,如果查不到數(shù)據(jù),則添加IndexRequest的文檔數(shù)據(jù)
????IndexRequest indexRequest = new IndexRequest("blog1", "article", "4")
????????????.source(XContentFactory.jsonBuilder()
????????????????.startObject()
????????????????????.field("id", "4")
????????????????????.field("title", "李雪靜加油")
????????????????????.field("content", "Fighting!")
????????????????.endObject());
????//設(shè)置更新的數(shù)據(jù),使用ID查詢,如果能查到,則更新UpdateRequest的數(shù)據(jù)
????UpdateRequest updateRequest = new UpdateRequest("blog1", "article", "4")
????????????.doc(XContentFactory.jsonBuilder()
????????????????????.startObject()
????????????????????.field("title", "new fighting~")
????????????????????.endObject())
????????????.upsert(indexRequest);
????UpdateResponse updateResponse = client.update(updateRequest).get();
????System.out.println("索引名稱: " + updateResponse.getIndex());
????System.out.println("文檔類型: "+updateResponse.getType());
????System.out.println("ID: "+updateResponse.getId());
????System.out.println("版本: "+updateResponse.getVersion());
????System.out.println("是否創(chuàng)建成功: "+updateResponse.isCreated()); //false
????client.close();
}
----------------------------------------刪除文檔------------------------------------
prepareDelete
@Test
public void deleteData(){
????DeleteResponse deleteResponse = client.prepareDelete("blog", "article", "3").get();
????System.out.println("索引名稱: " + deleteResponse.getIndex());
????System.out.println("文檔類型: "+deleteResponse.getType());
????System.out.println("ID: "+deleteResponse.getId());
????System.out.println("版本: "+deleteResponse.getVersion());
????System.out.println("是否刪除成功: "+deleteResponse.isFound());
????client.close();
}
----------------------------------------查詢文檔------------------------------------
安裝分詞器之前的查詢
es提供了queryStringQuery查詢
針對多字短的query_string查詢
@Test
public void testSearch(){
????SearchResponse searchResponse = client.prepareSearch("blog")
????????????.setTypes("article")
//默認分詞器只能查找單個字,并沒有把中文進行分詞,需要我們安裝一個分詞器,IK分詞器
????????????.setQuery(QueryBuilders.queryStringQuery("更新"))
????????????.get();
????//獲取數(shù)據(jù)的結(jié)果對象
????SearchHits hits = searchResponse.getHits();
????//獲取命中次數(shù)
????System.out.println("查詢的結(jié)果數(shù)據(jù)有"+hits.getTotalHits()+"條");
????//遍歷所有數(shù)據(jù)
????Iterator<SearchHit> iterator = hits.iterator();
????while (iterator.hasNext()){
????????SearchHit hit = iterator.next();
????????//獲取整條數(shù)據(jù)
????????System.out.println(hit.getSourceAsString());
????????//獲取單個字段
????????System.out.println("id: "+ hit.getSource().get("id"));
????????System.out.println("title: "+ hit.getSource().get("title"));
????????System.out.println("content: "+ hit.getSource().get("content"));
????}
????client.close();
}
網(wǎng)站名稱:好程序員Java學(xué)習(xí)路線分享創(chuàng)建Javaclass
轉(zhuǎn)載注明:http://aaarwkj.com/article46/psophg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、品牌網(wǎng)站制作、靜態(tài)網(wǎng)站、網(wǎng)站設(shè)計、手機網(wǎng)站建設(shè)、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)