- 新建springboot项目
- 引入的es依赖jar包
-
<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.14.0</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-client</artifactId> <version>7.14.0</version> </dependency> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>7.14.0</version> </dependency>
在测试类中写代码
索引相关操作:
1、创建客户端对象
- 方法1
@Test void contextLoads() { //创建es客户端对象 RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost( "192.168.19.128", 9200, "http" ))); System.out.println(client); }
-
方法2
-
新建一个配置类
@Configuration @ConfigurationProperties(prefix = "elasticsearch") public class ElasticSearchConfig { private String host; private int port; public String getHost() { return host; } public void setHost(String host) { this.host = host; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } @Bean public RestHighLevelClient client() { //创建es客户端对象 return new RestHighLevelClient(RestClient.builder(new HttpHost( host, port, "http" ))); } }
- 新增一个application.yaml文件 存放ip和端口
elasticsearch: host: 192.168.19.128 port: 9200
- 在测试类中使用@Autowired注解,获取es客户端的对像
@Autowired private RestHighLevelClient client;
- 项目结构
-
2、添加索引
//添加索引
@Test
public void addIndex() throws IOException {
//1、使用client获取操作索引的对象
IndicesClient indicesClient = client.indices();
//2、具体操作、获取返回值
CreateIndexRequest createRequest = new CreateIndexRequest("itck");
CreateIndexResponse createResponse = indicesClient.create(createRequest, RequestOptions.DEFAULT);
//3、根据返回值判断结果
System.out.println(createResponse.isAcknowledged());
}
2.2、添加索引及映射
//添加索引并添加映射
@Test
public void addIndexAndMapping() throws IOException {
//1、使用client获取操作索引的对象
IndicesClient indicesClient = client.indices();
//2、具体操作、获取返回值
CreateIndexRequest createRequest = new CreateIndexRequest("ck");
String mapping ="{\n" +
" \"properties\" : {\n" +
" \"address\" : {\n" +
" \"type\" : \"text\",\n" +
" \"analyzer\" : \"ik_max_word\"\n" +
" },\n" +
" \"name\" : {\n" +
" \"type\" : \"keyword\"\n" +
" }\n" +
" }\n" +
" }";
createRequest.mapping(mapping, XContentType.JSON);
CreateIndexResponse createResponse = indicesClient.create(createRequest, RequestOptions.DEFAULT);
//3、根据返回值判断结果
System.out.println(createResponse.isAcknowledged());
}
3、查询索引
//查询索引
@Test
public void getIndex() throws IOException {
//获取操作对象
IndicesClient indices = client.indices();
//执行操作查询索引
GetIndexRequest request = new GetIndexRequest("ck");
GetIndexResponse getResponse = indices.get(request, RequestOptions.DEFAULT);
//打印索引对应的映射
Map<String, MappingMetadata> mappings = getResponse.getMappings();
for (String key:mappings.keySet()){
System.out.println(key+":"+mappings.get(key).getSourceAsMap());
}
}
4、删除索引
//删除索引
@Test
public void deleteIndex() throws IOException {
//获取操作对象
IndicesClient indices = client.indices();
//执行操作删除索引
DeleteIndexRequest deleteRequest = new DeleteIndexRequest("itck");
AcknowledgedResponse response = indices.delete(deleteRequest, RequestOptions.DEFAULT);
//打印是否删除成功
System.out.println(response.isAcknowledged());
}
5、判断索引是否存在
//判断索引是否存在
@Test
public void existIndex() throws IOException {
//获取操作对象
IndicesClient indices = client.indices();
GetIndexRequest getRequest = new GetIndexRequest("ck");
boolean exists = indices.exists(getRequest, RequestOptions.DEFAULT);
System.out.println(exists);
}
文档操作
-
添加文档 方式1 使用map作为数据
//map的方式添加文档 @Test public void addDoc() throws IOException { //文档数据 Map data = new HashMap(); data.put("name","张三"); data.put("age",20); data.put("address","北京昌平"); //获取操作文档的对象 在为ck的索引上添加文档,文档的id为1,内容为data IndexRequest requset = new IndexRequest("ck").id("1").source(data); IndexResponse index = client.index(requset, RequestOptions.DEFAULT); System.out.println(index.getId()); }
-
添加文档 方式2 使用对象作为数据
//添加文档,使用对象作为数据 @Test public void addDoc2() throws IOException { //文档数据 java对象 Person person = new Person(); person.setId("2"); person.setName("李四"); person.setAge(21); person.setAddress("上海浦东"); //对象转json 需要引用fastjson的依赖包 String data = JSONObject.toJSONString(person); //获取操作文档的对象 在为ck的索引上添加文档 IndexRequest requset = new IndexRequest("ck").id(person.getId()).source(data,XContentType.JSON); IndexResponse index = client.index(requset, RequestOptions.DEFAULT); System.out.println(index.getId()); }
-
修改文档:添加时如果id存在就修改,否则添加
//修改文档,添加时如果id存在 修改,否则添加 @Test public void updateDoc() throws IOException { //文档数据 java对象 Person person = new Person(); person.setId("2"); person.setName("王五"); person.setAge(21); person.setAddress("上海浦东"); //对象转json String data = JSONObject.toJSONString(person); //获取操作文档的对象 在为ck的索引上添加文档 IndexRequest requset = new IndexRequest("ck").id(person.getId()).source(data,XContentType.JSON); IndexResponse index = client.index(requset, RequestOptions.DEFAULT); System.out.println(index.getId()); }
-
查询文档
//根据id查询文档 @Test public void getDoc() throws IOException { GetRequest request = new GetRequest("ck","2"); GetResponse getResponse = client.get(request, RequestOptions.DEFAULT); System.out.println(getResponse.getSourceAsString()); }
-
删除文档
//根据id删除文档 @Test public void delDoc() throws IOException { DeleteRequest request = new DeleteRequest("ck","1"); DeleteResponse delResonse = client.delete(request, RequestOptions.DEFAULT); System.out.println(delResonse.getId()); }
以上为api的一些简单操作,后续会更新比较复杂的操作