- Java对es进行添加操作
//插入数据
@Test
public void test2() throws IOException {
//1、指定es集群 cluster.name 是固定的key值,my-application是ES集群的名称
Settings settings = Settings.builder().put("cluster.name", "my-application").build();
//2.创建访问ES服务器的客户端
TransportClient client = new PreBuiltTransportClient(settings)
//获取es主机中节点的ip地址及端口号(以下是单个节点案例)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.94"), 9300));
//将数据转换成文档的格式(后期可以使用java对象,将数据转换成json对象就可以了)
XContentBuilder doContentBuilder=XContentFactory.jsonBuilder()
.startObject()
.field("id", "1") //字段名 : 值
.field("title", "java设计模式之装饰模式")
.field("content", "在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能")
.field("postdate", "2018-05-20")
.field("url", "https://www.cnblogs.com/chenyuanbo/")
.endObject();
//添加文档 index1:索引名 blog:类型 10:id
//.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) 代表插入成功后立即刷新,因为ES中插入数据默认分片要1秒钟后再刷新
IndexResponse response = client.prepareIndex("index1","blog","10")
.setSource(doContentBuilder).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get();
System.out.println(response.status());
//打印出CREATED 表示添加成功
}
- java实现删除
//删除文档
@Test
public void test3() throws UnknownHostException {
Settings settings = Settings.builder().put("cluster.name", "my-application").build();
//2.创建访问ES服务器的客户端
TransportClient client = new PreBuiltTransportClient(settings)
//获取es主机中节点的ip地址及端口号(以下是单个节点案例)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.94"), 9300));
DeleteResponse response = client.prepareDelete("index1","blog","10").get();
System.out.println(response.status());
//控制台打印出OK代表删除成功
}
- 对指定字段进行修改
//修改数据(指定字段进行修改)
@Test
public void test4() throws IOException, InterruptedException, ExecutionException {
Settings settings = Settings.builder().put("cluster.name", "my-application").build();
//2.创建访问ES服务器的客户端
TransportClient client = new PreBuiltTransportClient(settings)
//获取es主机中节点的ip地址及端口号(以下是单个节点案例)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.94"), 9300));
UpdateRequest request = new UpdateRequest();
request.index("index1") //索引名
.type("blog") //类型
.id("10")//id
.doc(
XContentFactory.jsonBuilder()
.startObject()
.field("title", "单例设计模式")//要修改的字段 及字段值
.endObject()
);
UpdateResponse response= client.update(request).get();
System.out.println(response.status());
//控制台出现OK 代表更新成功
}
- upsert修改用法:修改文章存在,执行修改,不存在则执行插入
//upsert 修改用法:修改文章存在,执行修改,不存在则执行插入
@Test
public void test5() throws IOException, InterruptedException, ExecutionException {
Settings settings = Settings.builder().put("cluster.name", "my-application").build();
//2.创建访问ES服务器的客户端
TransportClient client = new PreBuiltTransportClient(settings)
//获取es主机中节点的ip地址及端口号(以下是单个节点案例)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.94"), 9300));
IndexRequest request1 = new IndexRequest("index1","blog","8").source(
XContentFactory.jsonBuilder()
.startObject()
.field("id", "2") //字段名 : 值
.field("title", "工厂模式")
.field("content", "静态工厂,实例工厂")
.field("postdate", "2018-05-20")
.field("url", "https://www.cnblogs.com/chenyuanbo/")
.endObject()
);
UpdateRequest request2 = new UpdateRequest("index1","blog","8").doc(
XContentFactory.jsonBuilder().startObject()
.field("title", "设计模式")
.endObject()
).upsert(request1);
UpdateResponse response = client.update(request2).get();
System.out.println(response.status());
}
文章来源地址https://www.toymoban.com/news/detail-522361.html
文章来源:https://www.toymoban.com/news/detail-522361.html
到了这里,关于Java对es进行简单操作的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!