上一篇文章我们已经在springboot中集成了Es,这次我们来继续使用java来给es发请求
//测试存储到es
@Test
public void indexData() throws IOException {
//指定索引
IndexRequest indexRequest = new IndexRequest("users");
indexRequest.id("1");
User user = new User();
String string = JSON.toJSONString(user);
indexRequest.source(string, XContentType.JSON);
//执行操作
IndexResponse index = client.index(indexRequest, GulimallElasticSearchCongfig.COMMON_OPIONS);
//提取有用的响应数据
System.out.println(index);
}
@Data
class User{
private String uesrname;
private String gander;
private Integer age;
}
我们先看一下es中是否存在users这个索引