新增文档
创建数据模型
public class User {
private String name;
private Integer age;
private String sex;
public User(){}
public User(String name,Integer age,String sex){
this.name = name;
this.age = age;
this.sex = sex;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getSex() {
return sex;
}
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public void setSex(String sex) {
this.sex = sex;
}
}
创建数据,添加到文档中
// 新增文档 - 请求对象
IndexRequest request = new IndexRequest();
// 设置索引及唯一性标识
request.index("user").id("1001");
// 创建数据对象
User user = new User();
user.setName("大欢欢");
user.setAge(18);
user.setSex("女");
ObjectMapper objectMapper = new ObjectMapper();
String productJson = objectMapper.writeValueAsString(user);
// 添加文档数据,数据格式为JSON格式
request.source(productJson,XContentType.JSON);
// 客户端发送请求,获取响应对象
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
3.打印结果信息
System.out.println("_index:" + response.getIndex());
System.out.println("_id:" + response.getId());
System.out.println("_result:" + response.getResult());
全代码:
public class ESTest_Client_doc_Create {
public static void main(String[] args) throws Exception{
final String hostname = "localhost";
final int port = 9200;
final String scheme = "http";
HttpHost httphost = new HttpHost(hostname, port,scheme);
//创建环境
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder( httphost )
);
//插入数据
IndexRequest indexRequest = new IndexRequest();
//添加文档的索引和文档的id
indexRequest.index("user").id("1001");
User user = new User();
user.setName( "大欢欢" );
user.setAge( 18 );
user.setSex( "女" );
//因为ES是json格式,必须把他们转换成 json 格式
ObjectMapper mappers = new ObjectMapper();
String userJSON = mappers.writeValueAsString( user );
indexRequest.source( userJSON, XContentType.JSON );
//创建文档
IndexResponse indexResponse = client.index( indexRequest, RequestOptions.DEFAULT );
System.out.println( indexResponse.getResult() );
client.close();
}
}
修改文档
举例代码:
// 修改文档 - 请求对象
UpdateRequest request = new UpdateRequest();
// 配置修改参数
request.index("user").id("1001");
// 设置请求体,对数据进行修改
request.doc(XContentType.JSON, "age", "20");
// 客户端发送请求,获取响应对象
UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
System.out.println("_index:" + response.getIndex());
System.out.println("_id:" + response.getId());
System.out.println("_result:" + response.getResult());
全代码:
public class ESTest_Client_doc_Update {
public static void main(String[] args) throws Exception{
final String hostname = "localhost";
final int port = 9200;
final String scheme = "http";
HttpHost httphost = new HttpHost(hostname, port,scheme);
//创建环境
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder( httphost )
);
//修改数据
UpdateRequest updateRequest = new UpdateRequest();
//添加文档的索引和文档的id
updateRequest.index("user").id("1001");
updateRequest.doc(XContentType.JSON,"age","18");
//修改文档
UpdateResponse updateResponse = client.update( updateRequest, RequestOptions.DEFAULT );
System.out.println( updateResponse.getResult() );
client.close();
}
}
查询文档
举例代码:
//1.创建请求对象
GetRequest request = new GetRequest().index("user").id("1001");
//2.客户端发送请求,获取响应对象
GetResponse response = client.get(request, RequestOptions.DEFAULT);
3.打印结果信息
System.out.println("_index:" + response.getIndex());
System.out.println("_type:" + response.getType());
System.out.println("_id:" + response.getId());
System.out.println("source:" + response.getSourceAsString());
全代码:
public class ESTest_Client_doc_Get {
public static void main(String[] args) throws Exception{
final String hostname = "localhost";
final int port = 9200;
final String scheme = "http";
HttpHost httphost = new HttpHost(hostname, port,scheme);
//创建环境
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder( httphost )
);
//查询数据
GetRequest request = new GetRequest();
request.index("user").id("1001");
GetResponse response = client.get( request, RequestOptions.DEFAULT );
System.out.println(response.getSourceAsString());
client.close();
}
}
删除文档
举例代码:
//创建请求对象
DeleteRequest request = new DeleteRequest().index("user").id("1001");
//客户端发送请求,获取响应对象
DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
//打印信息
System.out.println(response.toString());
全代码:
public class ESTest_Client_doc_Delete {
public static void main(String[] args) throws Exception{
final String hostname = "localhost";
final int port = 9200;
final String scheme = "http";
HttpHost httphost = new HttpHost(hostname, port,scheme);
//创建环境
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder( httphost )
);
//删除数据
DeleteRequest request = new DeleteRequest();
request.index("user").id("1001");
DeleteResponse response = client.delete( request, RequestOptions.DEFAULT );
System.out.println(response.toString());
client.close();
}
}
批量操作
批量新增
//创建批量新增请求对象
BulkRequest request = new BulkRequest();
request.add(new IndexRequest().index("user").id("1001").source(XContentType.JSON, "name", "zhangsan"));
request.add(new IndexRequest().index("user").id("1002").source(XContentType.JSON, "name", "lisi"));
request.add(new IndexRequest().index("user").id("1003").source(XContentType.JSON, "name", "wangwu"));
//客户端发送请求,获取响应对象
BulkResponse responses = client.bulk(request, RequestOptions.DEFAULT);
//打印结果信息
System.out.println("took:" + responses.getTook());
System.out.println("items:" + responses.getItems());
全代码:
public class ESTest_Client_doc_Insert_Batch {
public static void main(String[] args) throws Exception{
//1.创建客户端对象
//创建服务主机名 和 Web 通信端口
final String hostname = "localhost";
final int port = 9200;
final String scheme = "http";
HttpHost httpHost = new HttpHost( hostname, port, scheme );
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(httpHost)
);
//批量插入
BulkRequest request = new BulkRequest();
request.add( new IndexRequest().index("user").id("1002").source( XContentType.JSON,"name","zhangsan" ) );
request.add( new IndexRequest().index("user").id("1003").source( XContentType.JSON,"name","lisi" ) );
request.add( new IndexRequest().index("user").id("1004").source( XContentType.JSON,"name","wangwu" ) );
BulkResponse bulk = client.bulk( request, RequestOptions.DEFAULT );
System.out.println( bulk.getTook() );
System.out.println( bulk.getItems() );
//关闭ES 资源
client.close();
}
}
批量删除
//创建批量删除请求对象
BulkRequest request = new BulkRequest();
request.add(new DeleteRequest().index("user").id("1001"));
request.add(new DeleteRequest().index("user").id("1002"));
request.add(new DeleteRequest().index("user").id("1003"));
//客户端发送请求,获取响应对象
BulkResponse responses = client.bulk(request, RequestOptions.DEFAULT);
//打印结果信息
System.out.println("took:" + responses.getTook());
System.out.println("items:" + responses.getItems());
全代码:
public class ESTest_Client_doc_Delete_Batch {
public static void main(String[] args) throws Exception{
//1.创建客户端对象
//创建服务主机名 和 Web 通信端口
final String hostname = "localhost";
final int port = 9200;
final String scheme = "http";
HttpHost httpHost = new HttpHost( hostname, port, scheme );
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(httpHost)
);
//批量插入
BulkRequest request = new BulkRequest();
request.add( new DeleteRequest().index("user").id("1002"));
request.add( new DeleteRequest().index("user").id("1003"));
request.add( new DeleteRequest().index("user").id("1004"));
BulkResponse bulk = client.bulk( request, RequestOptions.DEFAULT );
System.out.println( bulk.getTook() );
System.out.println( bulk.getItems() );
//关闭ES 资源
client.close();
}
}