链接方式参考官网
传统的方式是JEST。但是我建议用官网的API比较方便
建立一个congif类
package com.test.es.yang.config;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author: 杨丰源
* @date: 2021-06-29 14:48
* @description:
*/
@Configuration
public class ESConfiguration {
@Value("${elasticsearch.hostname}")
String hostname;
@Value("${elasticsearch.port}")
int port;
@Bean
public RestHighLevelClient restHighLevelClient() {
// 如果有多个从节点可以持续在内部new多个HttpHost,参数1是IP,参数2是端口,参数3是通信协议
return new RestHighLevelClient(RestClient.builder(new HttpHost(hostname, port, "http")));
}
}
这里配置文件的配置如下
elasticsearch:
hostname: localhost
port: 9200
建立接口:
IRestHighLevelClientService 实现创建删除索引
package com.test.es.yang.service;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.indices.CreateIndexResponse;
import java.io.IOException;
/**
* @author: 杨丰源
* @date: 2021-06-29 16:00
* @description:
*/
public interface IRestHighLevelClientService {
CreateIndexResponse createIndex(String indexName) throws IOException;
AcknowledgedResponse deleteIndex(String indexName) throws IOException;
}
建立service实现类实现接口
package com.test.es.yang.service.impl;
import com.test.es.yang.service.IRestHighLevelClientService;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.settings.Settings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
/**
* @author: 杨丰源
* @date: 2021-06-29 16:05
* @description:
*/
@Service
public class RestHighLevelClientServiceImpl implements IRestHighLevelClientService {
@Autowired
private RestHighLevelClient restHighLevelClient;
@Override
public CreateIndexResponse createIndex(String indexName) throws IOException {
CreateIndexRequest request = new CreateIndexRequest(indexName.toLowerCase());
request.settings(Settings.builder()
.put("index.number_of_shards", 5)
.put("index.number_of_replicas", 0)
);
CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
return createIndexResponse;
}
@Override
public AcknowledgedResponse deleteIndex(String indexName) throws IOException {
DeleteIndexRequest indexRequest = new DeleteIndexRequest(indexName);
AcknowledgedResponse delete = restHighLevelClient.indices().delete(indexRequest, RequestOptions.DEFAULT);
return delete;
}
}
建立controller
package com.test.es.yang.controller;
import com.test.es.yang.service.IRestHighLevelClientService;
import common.CommonResult;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
/**
* @author: 杨丰源
* @date: 2021-06-29 15:58
* @description:
*/
@RestController
@RequestMapping("/es")
public class ESController {
@Autowired
private IRestHighLevelClientService restHighLevelClientService;
/**
* 创建索引
* @return
* @throws IOException
*/
@PostMapping("/createIndex")
public CommonResult createIndex(@RequestParam String indexName) throws IOException {
CreateIndexResponse index = restHighLevelClientService.createIndex(indexName);
String success="1";
return new CommonResult(200,"success",index);
}
/**
* 删除索引
* @return
* @throws IOException
*/
@PostMapping("/deleteIndex")
public CommonResult deleteIndex(@RequestParam String indexName) throws IOException {
AcknowledgedResponse acknowledgedResponse = restHighLevelClientService.deleteIndex(indexName);
return new CommonResult(200,"success",acknowledgedResponse);
}
}
最简单的建立索引搞定