
新建MAVEN工程
导入依赖:
org.elasticsearch
elasticsearch
6.5.4
org.elasticsearch.client
elasticsearch-rest-high-level-client
6.5.4
提供静态方法返回rest高级客户端对象,就能连上ES服务器进行通信:
public class ESClient {
public static RestHighLevelClient getClient(){
// 创建HttpHost对象
HttpHost httpHost = new HttpHost("192.168.200.129",9200);//连接Linux服务器安装的es和ip和端口
// 创建RestClientBuilder
RestClientBuilder clientBuilder = RestClient.builder(httpHost);
// 创建RestHighLevelClient
RestHighLevelClient client = new RestHighLevelClient(clientBuilder);
// 返回
return client;
}
测试连接问题:
@Test
public void testConnect() {
RestHighLevelClient client = ESClient.getClient();//.var,org.elasticsearch.client.RestHighLevelClient@61009542
System.out.println(client);
System.out.println("测试类调用工具类方法,如果没有报错,表示连上了es服务器");
}
Java *** 作ElasticSearch 索引
public class Demo2 {
RestHighLevelClient client = ESClient.getClient();
String index = "person";
String type = "man";
@Test
public void createIndex() throws IOException {
//1. 准备关于索引的settings
Settings.Builder settings = Settings.builder()
.put("number_of_shards", 3)
.put("number_of_replicas", 1);
//2. 准备关于索引的结构mappings
XContentBuilder mappings = JsonXContent.contentBuilder()
.startObject()
.startObject("properties")
.startObject("name")
.field("type","text")
.endObject()
.startObject("age")
.field("type","integer")
.endObject()
.startObject("birthday")
.field("type","date")
.field("format","yyyy-MM-dd")
.endObject()
.endObject()
.endObject();
//3. 将settings和mappings封装到一个Request对象
CreateIndexRequest request = new CreateIndexRequest(index)
.settings(settings)
.mapping(type,mappings);
//4. 通过client对象去连接ES并执行创建索引
CreateIndexResponse resp = client.indices().create(request, RequestOptions.DEFAULT);
//5. 输出
System.out.println("resp:" + resp.toString());
//resp:org.elasticsearch.action.admin.indices.create.CreateIndexResponse@c4f729f4
}
}
检查索引是否存在(exists方法)
@Test
public void exists() throws IOException {
//1. 准备request对象
GetIndexRequest request = new GetIndexRequest();
request.indices(index);
//2. 通过client去 *** 作
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
//3. 输出
System.out.println(exists);//索引index存在返回true,不存在返回false
}
删除索引
@Test
public void delete() throws IOException {
//1. 准备request对象
DeleteIndexRequest request = new DeleteIndexRequest();
request.indices(index);
//2. 通过client对象执行
AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);
//3. 获取返回结果
System.out.println(delete.isAcknowledged());//true
}
Java *** 作ElasticSearch 文档
添加文档 *** 作
数据通过json数据传输,因此要导入相关依赖方便数据的转换 *** 作:
com.fasterxml.jackson.core jackson-databind2.10.3
配置实体类:
public class Person {
@JsonIgnore//id要忽略转换为json,通过其他Java代码来搞定id生成
private Integer id;
private String name;
private Integer age;
@JsonFormat(pattern = "yyyy-MM-dd")//转为json时指定格式
private Date birthday;
}
添加文档数据:
public class Demo3 {
ObjectMapper mapper = new ObjectMapper();//这里用到上面提供的jackson依赖
RestHighLevelClient client = ESClient.getClient();
String index = "person";
String type = "man";
@Test
public void createDoc() throws IOException {
//1. 准备一个json数据
Person person = new Person(1,"张三",23,new Date());
String json = mapper.writevalueAsString(person);
//2. 准备一个request对象(其他Java代码来搞定id生成,即手动指定id↓)
IndexRequest request = new IndexRequest(index,type,person.getId().toString());
request.source(json, XContentType.JSON);
//3. 通过client对象执行添加
IndexResponse resp = client.index(request, RequestOptions.DEFAULT);
//4. 输出返回结果
System.out.println(resp.getResult().toString());
//添加成功返回的第一次是CREATED,再运行一次是更新是UPDATED
}
}
修改文档 *** 作
@Test
public void updateDoc() throws IOException {
//1. 创建一个Map,指定需要修改的内容
Map doc = new HashMap();
doc.put("name","张大三"); // 如果id放在map中会把id属性设置到_source里面
String docId = "1";
//2. 创建request对象,封装数据
UpdateRequest request = new UpdateRequest(index,type,docId);
request.doc(doc);
//3. 通过client对象执行
UpdateResponse update = client.update(request, RequestOptions.DEFAULT);
//4. 输出返回结果
System.out.println(update.getResult().toString());//UPDATED
}
删除文档 *** 作
@Test
public void deleteDoc() throws IOException {
//1. 封装Request对象
DeleteRequest request = new DeleteRequest(index,type,"1");
//2. client执行
DeleteResponse resp = client.delete(request, RequestOptions.DEFAULT);
//3. 输出结果
System.out.println(resp.getResult().toString());
//删除成功返回DELETED,再删没有数据返回NOT_FOUND
}
Java批量 *** 作ElasticSearch 文档(bulk)
批量添加
@Test
public void bulkCreateDoc() throws IOException {
//1. 准备多个json数据
Person p1 = new Person(1,"张三",23,new Date());
Person p2 = new Person(2,"李四",24,new Date());
Person p3 = new Person(3,"王五",25,new Date());
String json1 = mapper.writevalueAsString(p1);
String json2 = mapper.writevalueAsString(p2);
String json3 = mapper.writevalueAsString(p3);
//2. 创建Request,将准备好的数据封装进去
BulkRequest request = new BulkRequest();
request.add(new IndexRequest(index,type,p1.getId().toString()).source(json1,XContentType.JSON));
request.add(new IndexRequest(index,type,p2.getId().toString()).source(json2,XContentType.JSON));
request.add(new IndexRequest(index,type,p3.getId().toString()).source(json3,XContentType.JSON));
//3. 用client执行
BulkResponse resp = client.bulk(request, RequestOptions.DEFAULT);
//4. 输出结果
System.out.println(resp.toString());//org.elasticsearch.action.bulk.BulkResponse@3aa078fd
}
批量删除
@Test
public void bulkDeleteDoc() throws IOException {
//1. 封装Request对象
BulkRequest request = new BulkRequest();
request.add(new DeleteRequest(index,type,"1"));
request.add(new DeleteRequest(index,type,"2"));
request.add(new DeleteRequest(index,type,"3"));
//2. client执行
BulkResponse resp = client.bulk(request, RequestOptions.DEFAULT);
//3. 输出
System.out.println(resp);//org.elasticsearch.action.bulk.BulkResponse@196a42c3
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)