您好,登錄后才能下訂單哦!
好程序員Java學習路線分享創建Java class,首先通過Transport Client獲取ES的連接
private Client client;
//通過Transport Client獲取ES的連接
@Before
public void getClient() throws Exception{
????//ES服務的JavaAPI的port為9300
????//注意:如果請求一個ES集群,可以多添幾個節點
????//為了避免在一個節點出現網絡問題導致的請求失敗問題,可以自動切換另外一個節點
????client = TransportClient.builder().build()
????????????.addTransportAddress(new InetSocketTransportAddress(
????????????????????InetAddress.getByName("localhost"),9300));
????????????//.addTransportAddress(...);
}
操作命令
----------------------------------------新建文檔------------------------------------
@Test
public void createDocument1(){
????// json格式的數據
????//json "需要轉義 -> \
????String source = "{" +
????????????"\"id\":\"1\"," +
????????????"\"title\":\"Lucene是一套用于全文檢索和搜尋的開源程式庫\"," +
????????????"\"content\":\"Lucene提供了一個簡單卻強大的應用程式接口,能夠做全文索引和搜尋\"" +
????????????"}";
????//創建文檔, 定義索引名稱,文檔類型,主鍵唯一標識ID
????//execute().actionGet() == get() ?代表立刻執行
????IndexResponse indexResponse =
????????????client.prepareIndex("blog", "article", "1")
????????????????????.setSource(source).get(); //加載數據并觸發
????this.getResponse(indexResponse); ?//對應下面封裝信息
????client.close();
}
將打印信息封裝成類,方便this調用
//獲取響應信息
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("是否創建成功: "+indexResponse.isCreated());
}
@Test
public void createDocument2(){
????// map類型的數據
????Map<String,Object> source = new HashMap<>();
????source.put("id","2");
????source.put("title","ElasticSearch");
????source.put("content","是一個分布式的 RESTful 風格的搜索和數據分析引擎");
????//創建文檔
????IndexResponse indexResponse = client.prepareIndex("blog", "article", "2")
????????????.setSource(source).get();
????this.getResponse(indexResponse);
????client.close();
}
使用ES幫助類(執行類),創建文檔
@Test
public void createDocument3() throws Exception{
????XContentBuilder source = XContentFactory.jsonBuilder()
????????????.startObject() //封裝數據
????????????????.field("id","3")
????????????????.field("title","ES的核心")
????????????????.field("content","集中的是巴拉巴拉")
????????????.endObject();
????System.out.println(source.toString());
????//創建文檔
????IndexResponse indexResponse = client.prepareIndex("blog", "article", "3")
????????????.setSource(source).get();
????this.getResponse(indexResponse);
????client.close();
}
----------------------------------------搜索文檔------------------------------------
輸出格式為json格式
搜索文檔數據 -- 單個索引 ?prepareGet
@Test
public void testGetData1(){
????GetResponse getResponse = client.prepareGet("blog", "article", "1").get();
????System.out.println(getResponse.getSourceAsString());
????client.close(); ??//輸出格式為json格式
}
搜索文檔數據 -- 多個索引 ?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更新
創建更新對象
@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("是否創建成功: "+updateResponse.isCreated()); //false
????client.close();
}
直接調用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("是否創建成功: "+updateResponse.isCreated()); //false
????client.close();
}
創建并更新
@Test
public void testUpdate3() throws Exception{
????// 設置一個查詢的條件,使用ID查詢,如果查不到數據,則添加IndexRequest的文檔數據
????IndexRequest indexRequest = new IndexRequest("blog1", "article", "4")
????????????.source(XContentFactory.jsonBuilder()
????????????????.startObject()
????????????????????.field("id", "4")
????????????????????.field("title", "李雪靜加油")
????????????????????.field("content", "Fighting!")
????????????????.endObject());
????//設置更新的數據,使用ID查詢,如果能查到,則更新UpdateRequest的數據
????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("是否創建成功: "+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();
????//獲取數據的結果對象
????SearchHits hits = searchResponse.getHits();
????//獲取命中次數
????System.out.println("查詢的結果數據有"+hits.getTotalHits()+"條");
????//遍歷所有數據
????Iterator<SearchHit> iterator = hits.iterator();
????while (iterator.hasNext()){
????????SearchHit hit = iterator.next();
????????//獲取整條數據
????????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();
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。