搜索模块实现添加单条文档功能+测试功能

main
heqijun 3 months ago
parent 70f7a30dd5
commit 89a22c85a8

@ -27,8 +27,7 @@ public enum ExceptionEnums {
LIMIT_MINUTE(-16, "达到分钟限流阈值!!"), LIMIT_MINUTE(-16, "达到分钟限流阈值!!"),
LIMIT_HOUR(-17, "达到小时限流阈值!!"), LIMIT_HOUR(-17, "达到小时限流阈值!!"),
NO_CHANNEL(-18, "没有可用通道!!!"), NO_CHANNEL(-18, "没有可用通道!!!"),
SEARCH_INDEX_ERROR(-20, "es添加一行文档失败");
;
private final int code; private final int code;

@ -0,0 +1,30 @@
package com.mashibing.common.exception;
import com.mashibing.common.enums.ExceptionEnums;
import lombok.Getter;
/**
* @author heqijun
* @ClassName: SearchException
* @Description:
* @date 2025/6/12 18:13
*/
@Getter
public class SearchException extends RuntimeException {
private static final long serialVersionUID = 1L;
private final Integer code;
public SearchException(Integer code, String message) {
super(message);
this.code = code;
}
public SearchException(ExceptionEnums exceptionEnums) {
super(exceptionEnums.getMsg());
this.code = exceptionEnums.getCode();
}
}

@ -1,6 +1,10 @@
package com.mashibing.search.config; package com.mashibing.search.config;
import org.apache.http.HttpHost; 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.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder; import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.RestHighLevelClient;
@ -21,6 +25,12 @@ public class RestHighLevelClientConfig {
@Value("${elasticsearch.hostAndPorts}") @Value("${elasticsearch.hostAndPorts}")
private String[] hostAndPorts; private String[] hostAndPorts;
@Value("${elasticsearch.username:elastic}")
private String username;
@Value("${elasticsearch.password}")
private String password;
@Bean @Bean
public RestHighLevelClient restHighLevelClient() { public RestHighLevelClient restHighLevelClient() {
@ -30,8 +40,15 @@ public class RestHighLevelClientConfig {
httpHost[i] = new HttpHost(hostAndPort[0], Integer.parseInt(hostAndPort[1])); httpHost[i] = new HttpHost(hostAndPort[0], Integer.parseInt(hostAndPort[1]));
} }
// 设置认证信息
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
// 设置连接信息
RestClientBuilder restClientBuilder = RestClient.builder(httpHost); RestClientBuilder restClientBuilder = RestClient.builder(httpHost);
restClientBuilder.setHttpClientConfigCallback(f -> f.setDefaultCredentialsProvider(credentialsProvider));
// 使用RestClientBuilder构建RestHighLevelClient对象
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder); RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);
return restHighLevelClient; return restHighLevelClient;

@ -0,0 +1,22 @@
package com.mashibing.search.service;
import java.io.IOException;
/**
* @author heqijun
* @ClassName: SearchService
* @Description: service
* @date 2025/6/12 18:00
*/
public interface SearchService {
/**
* es
*
* @param index
* @param id id
* @param json
*/
void index(String index, String id, String json) throws IOException;
}

@ -0,0 +1,52 @@
package com.mashibing.search.service.impl;
import com.mashibing.common.enums.ExceptionEnums;
import com.mashibing.common.exception.SearchException;
import com.mashibing.search.service.SearchService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
/**
* @author heqijun
* @ClassName: ElasticSearchServiceImpl
* @Description: service
* @date 2025/6/12 18:03
*/
@Slf4j
@Service
public class ElasticSearchServiceImpl implements SearchService {
private static String CREATED = "created";
@Autowired
RestHighLevelClient client;
@Override
public void index(String index, String id, String json) throws IOException {
//构建request
IndexRequest request = new IndexRequest();
request.index(index).id(id).source(json, XContentType.JSON);
//发送插入请求
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
String result = response.getResult().getLowercase();
if (!CREATED.equals(result)) {
//插入失败
log.error("【搜索模块-写入数据】失败index = {},id = {},json = {},result = {}", index, id, json, result);
throw new SearchException(ExceptionEnums.SEARCH_INDEX_ERROR);
}
log.info("【搜索模块-写入数据】成功index = {},id = {},json = {},result = {}", index, id, json, result);
}
}

@ -0,0 +1,28 @@
package com.mashibing.search.service.impl;
import com.mashibing.search.service.SearchService;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
@RunWith(SpringRunner.class)
class ElasticSearchServiceImplTest {
@Autowired
private SearchService service;
@Test
void index() throws IOException {
String index = "sms_submit_log_2023";
String id = "1";
String json = "{\"clientId\": 1}";
service.index(index, id, json);
}
}
Loading…
Cancel
Save