Compare commits
5 Commits
a747c8cb65
...
b78c005476
Author | SHA1 | Date |
---|---|---|
|
b78c005476 | 3 months ago |
|
d23283da92 | 3 months ago |
|
89a22c85a8 | 3 months ago |
|
70f7a30dd5 | 3 months ago |
|
d513def487 | 3 months ago |
@ -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();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
package com.mashibing.search.config;
|
||||
|
||||
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.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;
|
||||
|
||||
/**
|
||||
* @author heqijun
|
||||
* @ClassName: RestHighLevelClient
|
||||
* @Description: elasticsearch客户端配置
|
||||
* @date 2025/6/12 17:09
|
||||
*/
|
||||
|
||||
@Configuration
|
||||
public class RestHighLevelClientConfig {
|
||||
|
||||
@Value("${elasticsearch.hostAndPorts}")
|
||||
private String[] hostAndPorts;
|
||||
|
||||
@Value("${elasticsearch.username:elastic}")
|
||||
private String username;
|
||||
|
||||
@Value("${elasticsearch.password}")
|
||||
private String password;
|
||||
|
||||
@Bean
|
||||
public RestHighLevelClient restHighLevelClient() {
|
||||
|
||||
HttpHost[] httpHost = new HttpHost[hostAndPorts.length];
|
||||
for (int i = 0; i < httpHost.length; i++) {
|
||||
String[] hostAndPort = hostAndPorts[i].split(":");
|
||||
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.setHttpClientConfigCallback(f -> f.setDefaultCredentialsProvider(credentialsProvider));
|
||||
|
||||
// 使用RestClientBuilder构建RestHighLevelClient对象
|
||||
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);
|
||||
|
||||
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,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…
Reference in new issue