Compare commits
No commits in common. 'b78c0054769a3ed3a54f632004bd2ded399d3af5' and 'a747c8cb652d3358a62cdc36dd9f993728d07d85' have entirely different histories.
b78c005476
...
a747c8cb65
@ -1,30 +0,0 @@
|
|||||||
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,58 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@ -1,22 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
@ -1,28 +0,0 @@
|
|||||||
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