IDEA创建springboot项目
pom.xml依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<!--需要引入transport-netty3-client,否则会启动报错-->
<dependency>
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>transport-netty3-client</artifactId>
<version>5.6.10</version>
</dependency>
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Slf4j
@Configuration
public class ESConfig {
@Value("${es.host}")
private String host;
@Value("${es.port}")
private String port;
@Value("${es.scheme}")
private String scheme;
@Value("${es.username}")
private String username;
@Value("${es.pass}")
private String password;
@Bean
public RestHighLevelClient restHighLevelClient() {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(username, password)); //es账号密码
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost(host, Integer.parseInt(port), scheme)
).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
httpClientBuilder.disableAuthCaching();
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
})
);
return client;
}
}
@Slf4j
@Component
public class EsUtil {
@Autowired
private ESConfig esConfig;
public static final String INDEX_NAME = "index_name";
public static String CREATE_INDEX;
private static RestHighLevelClient restHighLevelClient = null;
public static final RequestOptions COMMON_OPTIONS;
static {
RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
COMMON_OPTIONS = builder.build();
}
private static String readFileToString(String filePath){
File file = new File(filePath);
log.info("filepath: {}", file.getAbsolutePath());
try (FileReader reader = new FileReader(file)){
BufferedReader bufferedReader = new BufferedReader(reader);
StringBuilder stringBuilder = new StringBuilder();
String s = "";
while ((s = bufferedReader.readLine()) != null) {
stringBuilder.append(s + "\n");
}
return stringBuilder.toString();
} catch (IOException e){
e.printStackTrace();
}
return "";
}
@PostConstruct
public void init(){
CREATE_INDEX = readFileToString("src/main/resources/es.txt");
log.info("CREATE_INDEX: {}", CREATE_INDEX);
try {
if(restHighLevelClient != null){
restHighLevelClient.close();
}
restHighLevelClient = esConfig.restHighLevelClient();
if(this.indexExist(INDEX_NAME)){
return;
}
CreateIndexRequest request = new CreateIndexRequest(INDEX_NAME);
request.settings(Settings.builder().put("index.number_of_shards", 3).put("index.number_of_replicas", 2));
request.mapping(CREATE_INDEX, XContentType.JSON);
CreateIndexResponse res = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
if (!res.isAcknowledged()) {
throw new RuntimeException("初始化失败");
}
} catch (Exception e){
e.printStackTrace();
}
}
public boolean indexExist(String index) throws Exception {
GetIndexRequest request = new GetIndexRequest(index);
request.local(false);
request.humanReadable(true);
request.includeDefaults(false);
return restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
}
public IndexResponse insertOrUpdateOne(String index, EsEntity entity) {
IndexRequest request = new IndexRequest(index);
request.id(entity.getId());
request.source(JSON.toJSONString(entity.getData()), XContentType.JSON);
try {
return restHighLevelClient.index(request, RequestOptions.DEFAULT);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public BulkResponse insertBatch(String index, List<EsEntity> list) {
BulkRequest request = new BulkRequest();
for (EsEntity item : list) {
String _json = JSON.toJSONString(item.getData());
String _id = item.getId();
IndexRequest indexRequest = new IndexRequest(index).id(_id).source(_json, XContentType.JSON);
request.add(indexRequest);
}
try {
return restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public <T> List<T> search(String index, SearchSourceBuilder searchSourceBuilder, Class<T> resultClass) {
SearchRequest request = new SearchRequest(index);
request.source(searchSourceBuilder);
try {
SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
SearchHits hits1 = response.getHits();
SearchHit[] hits2 = hits1.getHits();
List<T> retList = new ArrayList<>(hits2.length);
for (SearchHit hit : hits2) {
String strJson = hit.getSourceAsString();
retList.add(JSON.parseObject(strJson, resultClass));
}
return retList;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public AcknowledgedResponse deleteIndex(String index) {
try {
IndicesClient indicesClient = restHighLevelClient.indices();
DeleteIndexRequest request = new DeleteIndexRequest(index);
AcknowledgedResponse response = indicesClient.delete(request, RequestOptions.DEFAULT);
return response;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public BulkByScrollResponse deleteByQuery(String index, QueryBuilder builder) {
DeleteByQueryRequest request = new DeleteByQueryRequest(index);
request.setQuery(builder);
request.setBatchSize(10000);
request.setConflicts("proceed");
try {
BulkByScrollResponse response = restHighLevelClient.deleteByQuery(request, RequestOptions.DEFAULT);
return response;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public <T> BulkResponse deleteBatch(String index, Collection<T> idList) {
BulkRequest request = new BulkRequest();
for (T t : idList) {
request.add(new DeleteRequest(index, t.toString()));
}
try {
BulkResponse response = restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
return response;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public boolean update(PushData pushData){
Map<String, Object> map = new HashMap<>();
map.put("id", pushData.getPushId());
map.put("pushId", pushData.getPushId());
map.put("requestBody", pushData.getRequestBody());
map.put("url", pushData.getUrl());
map.put("params", pushData.getParams());
map.put("requestHeader", pushData.getRequestHeader());
map.put("pushTime", pushData.getPushTime());
map.put("remark", pushData.getRemark());
map.put("reserved", pushData.getReserved());
UpdateRequest request = new UpdateRequest(INDEX_NAME, map.get("id").toString()).doc(map);
boolean status = false;
try {
UpdateResponse update = restHighLevelClient.update(request, EsUtil.COMMON_OPTIONS);
status = update.status().getStatus() == 200;
} catch (IOException e) {
e.printStackTrace();
}
return status;
}
resources下创建相应文件,创建索引时需要
{ "properties":{ "id":{ "type":"integer" }, "pushId":{ "type":"integer" }, "requestBody":{ "type":"text" }, "url":{ "type":"text" }, "params":{ "type":"text" }, "requestHeader":{ "type":"text" }, "pushTime":{ "type":"text" }, "remark":{ "type":"text" }, "reserved":{ "type":"text" } } }
@Slf4j
@Service
public class EsServiceImpl implements EsService{
@Autowired
private EsUtil esUtil;
@Override
public List<PushData> getAll() {
return esUtil.search(EsUtil.CREATE_INDEX, new SearchSourceBuilder(), PushData.class);
}
@Override
public PushData getById(Integer id) {
SearchSourceBuilder builder = new SearchSourceBuilder();
builder.query(new TermQueryBuilder("pushId", id));
List<PushData> res = esUtil.search(EsUtil.INDEX_NAME, builder, PushData.class);
if(res.size() > 0){
return res.get(0);
} else{
return null;
}
}
@Override
public List<PushData> searchListByKey(String keyName, String key) {
BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
boolQueryBuilder.must(QueryBuilders.matchQuery(keyName, key));
SearchSourceBuilder builder = new SearchSourceBuilder();
builder.size(10).query(boolQueryBuilder);
return esUtil.search(EsUtil.INDEX_NAME, builder, PushData.class);
}
@Override
public IndexResponse putOne(PushData pushData) {
long currentTime = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
EsEntity<PushData> esEntity = new EsEntity<>(currentTime + "", pushData);
return esUtil.insertOrUpdateOne(EsUtil.INDEX_NAME, esEntity);
}
@Override
public BulkResponse putBatch(List<PushData> pushDataList) {
List<EsEntity> list = new ArrayList<>();
pushDataList.forEach( item -> list.add(new EsEntity<>(item.getPushId() + "", item)));
return esUtil.insertBatch(EsUtil.INDEX_NAME, list);
}
@Override
public BulkByScrollResponse deleteById(Integer id) {
return esUtil.deleteByQuery(EsUtil.INDEX_NAME, new TermQueryBuilder("pushId", id));
}
@Override
public BulkResponse deleteBatch(List<Integer> ids) {
return esUtil.deleteBatch(EsUtil.INDEX_NAME, ids);
}
@Override
public List<PushData> searchConditionInterval(String start, String end) {
BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
boolQueryBuilder.must(QueryBuilders.rangeQuery("pushTime").gte(start));
boolQueryBuilder.must(QueryBuilders.rangeQuery("pushTime").lte(end));
SearchSourceBuilder builder = new SearchSourceBuilder();
builder.query(boolQueryBuilder);
return esUtil.search(EsUtil.INDEX_NAME, builder, PushData.class);
}
@Override
public boolean update(PushData pushData) {
return esUtil.update(pushData);
}
}
测试方法
@RunWith(SpringRunner.class)
@SpringBootTest
public class EsUtilTest {
@Autowired
IEsService IEsService;
@Test
public void t1AddOne(){
IndexResponse putOne = IEsService.putOne(PushData.builder()
.pushId(889)
.requestBody("requestBody")
.url("url")
.params("params")
.requestHeader("requestHeader")
.pushTime("98374945").remark("remark").reserved("reserved").build());
System.out.println(putOne);
}
@Test
public void addBatch(){
List<PushData> list = new ArrayList<>();
list.add(PushData.builder().pushId(99).requestBody("99body").url("99url").params("99param").requestHeader("99header").pushTime("9999").build());
list.add(PushData.builder().pushId(88).requestBody("88body").url("88url").params("88param").requestHeader("88header").pushTime("8888").build());
list.add(PushData.builder().pushId(77).requestBody("99body").url("77url").params("77param").requestHeader("77header").pushTime("7777").build());
BulkResponse bulkItemResponses = IEsService.putBatch(list);
System.out.println(bulkItemResponses.status());
}
@Test
public void findAll(){
List<PushData> all = IEsService.getAll();
all.forEach(System.out::println);
}
@Test
public void search(){
List<PushData> pushData = IEsService.searchListByKey("url", "99url");
pushData.forEach(System.out::println);
System.out.println("===============");
PushData byId = IEsService.getById(88);
System.out.println(byId);
}
@Test
public void deleteBatch(){
List<Integer> ids = new ArrayList<>();
ids.add(99);
//ids.add(779);
BulkResponse deleteBatch = IEsService.deleteBatch(ids);
System.out.println(deleteBatch);
}
@Test
public void deleteOne(){
BulkByScrollResponse bulkByScrollResponse = IEsService.deleteById(123123123);
System.out.println(bulkByScrollResponse);
}
@Test
public void update(){
IEsService.update(PushData.builder().build());
}
}