目录
依赖添加
在Maven的pom.xml文件中添加elasticsearch的依赖:
<!--ElasticSearch-->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.3.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.3.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.3.0</version>
</dependency>
代码实现
package org.es;
import org.apache.http.HttpHost;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* @author CYY
* @date 2022/5/17 9:28
*/
public class EsTest {
public static void main(String[] args) {
batchAddDocuments();
}
private static int port = 9200;
private static String host = "127.0.0.1";
private static RestHighLevelClient getClient() {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost(host, port, "http")));
return client;
}
/**
* bulk批量添加
*/
public static void batchAddDocuments() {
try {
RestHighLevelClient client = getClient();
Map<String, Object> jsonMap = new HashMap<String, Object>();
jsonMap.put("id", "01209");
jsonMap.put("title", "紅樓夢");
jsonMap.put("author", "曹雪芹");
jsonMap.put("classification", "小說");
IndexRequest request = new IndexRequest("book")
.id("2").source(jsonMap);
jsonMap.put("id", "01210");
jsonMap.put("title", "水滸傳");
jsonMap.put("author", "施耐庵");
jsonMap.put("classification", "小說");
IndexRequest request1 = new IndexRequest("book")
.id("3").source(jsonMap);
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.add(request);
bulkRequest.add(request1);
BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println("新增文档成功!");
client.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 根据索引id更新
*/
public static void batchUpdateDocuments() {
try {
RestHighLevelClient client = getClient();
Map<String, Object> updateMap = new HashMap<String, Object>();
updateMap.put("id", "99999");
updateMap.put("title", "西游记");
updateMap.put("author", "吴承恩");
updateMap.put("classification", "名著");
UpdateRequest request = new UpdateRequest("book", "2").doc(updateMap);
UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
System.out.println("updated response id: " + updateResponse.getId());
client.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 根据索引id删除
*/
public static void deleteDocument() {
try {
RestHighLevelClient client = getClient();
DeleteRequest request = new DeleteRequest("book", "2");
DeleteResponse deleteResponse = client.delete(
request, RequestOptions.DEFAULT);
System.out.println(deleteResponse.getResult());
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 删除索引
*/
public static void deleteIndex() {
try {
RestHighLevelClient client = getClient();
//Delete an index
DeleteIndexRequest requestDeleteIndex = new DeleteIndexRequest("book");
client.indices().delete(requestDeleteIndex, RequestOptions.DEFAULT);
System.out.println("Index is deleted ");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 根据索引id获取
*/
public static void getIndex() {
try {
RestHighLevelClient client = getClient();
GetRequest getRequest = new GetRequest("book", "2");
GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
String index = getResponse.getIndex();
String id = getResponse.getId();
if (getResponse.isExists()) {
long version = getResponse.getVersion();
String sourceAsString = getResponse.getSourceAsString();
Map<String, Object> sourceAsMap = getResponse.getSourceAsMap();
byte[] sourceAsBytes = getResponse.getSourceAsBytes();
System.out.println("index:" + index);
System.out.println("id:" + id);
System.out.println("version:" + version);
System.out.println("sourceAsString:" + sourceAsString);
System.out.println("sourceAsMap:" + sourceAsMap);
System.out.println("sourceAsBytes:" + sourceAsBytes);
} else {
}
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}