JAVA API調用elasticsearch實現基本增刪改查

elasticsearch支持不少api的操做,這邊先簡單的介紹一下Java增刪改查的API操做。
想要學習更多的操做,能夠閱讀官網api文檔。官網地址: https://www.elastic.co/guide/en/elasticsearch/client/index.html
1.下面開始具體的內容介紹,首先是建立索引,具體代碼以下:
/**
* 建立索引庫
* @return void
* 索引庫的名稱必須爲小寫
* @throws IOException
* @Title: addIndex1
*/
@Test
public void addIndex1() throws IOException {
IndexResponse response = client .prepareIndex( "msg" , "tweet" , "1" ).setSource(XContentFactory. jsonBuilder ()
.startObject().field( "name" , "linzhiqiang" )
.field( "date" , new Date())
.field( "msg" , "hello world" )
.endObject()).get();
System. out .println( "索引名稱:" + response.getIndex() + " \n 類型:" + response.getType()
+ " \n 文檔ID:" + response.getId() + " \n 當前實例狀態:" + response.status());
}
 
/**
* 添加索引:傳入json字符串
* @return void
* @Title: addIndex2
*/
@Test
public void addIndex2() {
String jsonStr = "{" +
" \" userName \" : \" 張三 \" ," +
" \" sendDate \" : \" 2017-11-30 \" ," +
" \" msg \" : \" 你好李四 \" " +
"}" ;
IndexResponse response = client .prepareIndex( "weixin" , "tweet" ).setSource(jsonStr, XContentType. JSON ).get();
System. out .println( "json索引名稱:" + response.getIndex() + " \n json類型:" + response.getType()
+ " \n json文檔ID:" + response.getId() + " \n 當前實例json狀態:" + response.status());
 
}
 
/**
* 建立索引-傳入Map對象
* @return void
* @Title: addIndex3
*/
@Test
public void addIndex3() {
Map<String, Object> map = new HashMap<String, Object>();
map.put( "name" , "小妹" );
map.put( "age" , 18 );
map.put( "sex" , "女" );
map.put( "address" , "廣東省廣州市天河區上社" );
map.put( "phone" , "15521202233" );
map.put( "height" , "175" );
map.put( "weight" , "60" );
IndexResponse response = client .prepareIndex( "species" , "person" ).setSource(map).get();
System. out .println( "map索引名稱:" + response.getIndex() + " \n map類型:" + response.getType()
+ " \n map文檔ID:" + response.getId() + " \n 當前實例map狀態:" + response.status());
}
 
/**
* 傳遞json對象
* 須要添加依賴:gson
* @return void
* @Title: addIndex4
*/
@Test
public void addIndex4() {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty( "userName" , "張三" );
jsonObject.addProperty( "sendDate" , "2017-11-23" );
jsonObject.addProperty( "msg" , "你好李四" );
IndexResponse response = client .prepareIndex( "qq" , "tweet" ).setSource(jsonObject, XContentType. JSON ).get();
System. out .println( "jsonObject索引名稱:" + response.getIndex() + " \n jsonObject類型:" + response.getType()
+ " \n jsonObject文檔ID:" + response.getId() + " \n 當前實例jsonObject狀態:" + response.status());
}
 
咱們能夠建立Json、Map、JsonObject、自定義字段等,建立好以後能夠去ES系統中查看是否添加成功。若是不進行分片設置每次建立默認有五個分片數量,具體狀況以下圖所示。
2.建立好以後,咱們能夠經過ES提供的API進行相應的查詢操做,具體代碼以下所示:
/**
* 從索引庫獲取數據
*
* @return void
* @Title: query
*/
@Test
public void query() {
GetResponse getResponse = client .prepareGet( "species" , "person" , "AWNtYjiVjqSYg4HhYcQZ" ).get();
System. out .println( "索引庫的數據:" + getResponse.getSourceAsString());
}
查詢的結果以下所示:
ES上面對應索引的數據以下所示:
3.下面咱們將上面查詢到的id(AWNtYjiVjqSYg4HhYcQZ)進行相應的修改,修改索引的API操做以下代碼所示:
/**
* 更新索引庫數據
* @Title: updateData
* @return void
*/
@Test
public void updateData() {
Map<String, Object> map = new HashMap<String, Object>();
map.put( "name" , "大妹" );
map.put( "age" , 20 );
map.put( "sex" , "女" );
map.put( "address" , "廣東省廣州市天河區上社" );
map.put( "phone" , "15521202233" );
map.put( "height" , "180" );
map.put( "weight" , "70" );
UpdateResponse updateResponse = client .prepareUpdate( "species" , "person" , "AWNtYjiVjqSYg4HhYcQZ" )
.setDoc(map).get();
System. out .println( "updateResponse索引名稱:" + updateResponse.getIndex() + " \n updateResponse類型:" + updateResponse.getType()
+ " \n updateResponse文檔ID:" + updateResponse.getId() + " \n 當前實例updateResponse狀態:" + updateResponse.status());
}
咱們在從新查詢一下id爲: AWNtYjiVjqSYg4HhYcQZ 的索引文檔,看一下數據是否已經修改。結果以下所示:
咱們從上面的截圖能夠看出數據確實已經修改完畢了,證實修改的API操做是成功的。
4.最後咱們看一下最後的刪除索引操做,具體的代碼以下所示:
/**
* 根據索引名稱,類別,文檔ID 刪除索引庫的數據
* @Title: deleteData
* @return void
*/
@Test
public void deleteData() {
DeleteResponse deleteResponse = client .prepareDelete( "species" , "person" , "AWNtYjiVjqSYg4HhYcQZ" ).get();
System. out .println( "deleteResponse索引名稱:" + deleteResponse.getIndex() + " \n deleteResponse類型:" + deleteResponse.getType()
+ " \n deleteResponse文檔ID:" + deleteResponse.getId() + " \n 當前實例deleteResponse狀態:" + deleteResponse.status());
}
下面咱們從新執行一下查詢id爲: AWNtYjiVjqSYg4HhYcQZ 看看是否還能夠查詢到對應的數據。結果以下所示:
 
能夠看到無論是查詢結果仍是ES數據都沒有那條對應的數據了,這證實咱們刪除的API操做是成功的。
你們可能會有疑問,問什麼都沒有看到鏈接ES系統的代碼,由於我這邊代碼是寫在Test測試用例中,因此每個操做都須要鏈接到ES系統這個操做。因此這邊我抽象出來變成父類,具體代碼以下所示:
public class ESTest {
/**
* 192.168.11.24 測試IP地址
* 47.106.165.2 外網IP地址
*/
public final static String HOST = "192.168.11.24" ;
 
public TransportClient client = null ;
 
public final static int PORT = 9300 ;
 
/**
* 獲取客戶端鏈接信息
*
* @return void
* @throws UnknownHostException
* @Title: getConnect
*/
@SuppressWarnings ({ "resource" , "unchecked" })
@Before
public void getConnect() {
try {
Settings settings = Settings. builder ()
.put( "cluster.name" , "my-application" ).build();
client = new PreBuiltTransportClient(settings).addTransportAddress(
new InetSocketTransportAddress(InetAddress. getByName ( HOST ), PORT ));
} catch (Exception e) {
e.printStackTrace();
}
}
 
/**
* 關閉鏈接
*
* @return void
* @Title: closeConnect
*/
@After
public void closeConnect() {
if ( null != client ) {
client .close();
}
}
}
上面的IP地址是對應測試和生產的IP地址,無論是生產仍是測試的ES系統,Java對應的API操做的端口都是 9300,記住是9300!!!
這樣簡單的JAVA API調用elasticsearch實現基本增刪改查 就完成了,固然這個是基礎的不能再基礎的東西,後面還有關鍵的查詢操做、多索引聚合操做、批量操做等等之類的操做。

對文章有什麼疑問或者想要看更多文章能夠加我訂閱號,歡迎你們的踩踩~html

相關文章
相關標籤/搜索