安装 docker
虚拟机安装 elastic search
安装本地
# 创建 elastic 的网络
docker network create elastic
# 用镜像的方式创建并启动容器
docker run -d --name es --net elastic -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e "xpack.security.enabled=false" -t docker.elastic.co/elasticsearch/elasticsearch:8.13.3
如果安装不成功,报如下的错误
ERROR: Elasticsearch exited unexpectedly, with exit code 78
修改 /etc/sysctl.conf
在文件最后添加一行
vm.max_map_count=262144
使配置生效
sysctl -p
访问 9200,如果出现如下界面,表示成功
spring boot 的例子
依赖引入
<dependency><groupId>co.elastic.clients</groupId><artifactId>elasticsearch-java</artifactId><version>8.13.3</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.17.0</version></dependency>
配置 es config
@Configuration
public class ESConfig {private String serverUrl = "http://192.168.236.128:9200";private String apiKey = "11";@Beanpublic ElasticsearchClient getClient(){// Create the low-level clientRestClient restClient = RestClient.builder(HttpHost.create(serverUrl)).setDefaultHeaders(new Header[]{new BasicHeader("Authorization", "ApiKey " + apiKey)}).build();// Create the transport with a Jackson mapperElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());// And create the API clientElasticsearchClient esClient = new ElasticsearchClient(transport);return esClient;}
}
编写测试用例
@SpringBootTest
class DemoApplicationTests {private Logger logger = LoggerFactory.getLogger(DemoApplicationTests.class);@Autowiredprivate ElasticsearchClient esClient;private String index = "request_log";@Testpublic void test() throws IOException {
// esClient.indices().create(c -> c.index("request_log"));}@Testpublic void addData() throws IOException {for (int j = 0; j < 1000; j++) {RequestLog log = createRandomLog();esClient.index(i -> i.index(index).id(log.getId()).document(log));}}@Testpublic void searchId() throws IOException {GetResponse<RequestLog> response = esClient.get(r -> r.index(index).id("75e18fb1-7efb-4ff7-b9eb-18de4eefea02"), RequestLog.class);logger.info("response {}",response);if(response.found()){RequestLog source = response.source();logger.info("response source {}",source);}else{logger.info("not get source");}}private RequestLog createRandomLog() {Random random = new Random();String[] appKey= {"a1111","b2222","c3333","c4444"};String[] name = {"中国公司","美国公司","法国公司","印度公司"};String[] msg = {"success","error","warn"};RequestLog log = new RequestLog();log.setAppKey(appKey[random.nextInt(appKey.length)]);log.setId(UUID.randomUUID().toString());log.setName(name[random.nextInt(name.length)]);log.setTtl(random.nextInt(1000));log.setResponse(msg[random.nextInt(msg.length)]);log.setCreateDate(new Date());return log;}@Testpublic void testSearchDocument() throws IOException {
// SearchRequest fuzzQuery= new SearchRequest.Builder().index(index).query(b -> b.fuzzy(t -> t.field("name").value("dd"))).build() ;SearchRequest matchQuery = new SearchRequest.Builder().index(index).from(3).size(2000).query(b -> b.match(t -> t.field("name").fuzziness("1").query("公司"))).build() ;SearchResponse<RequestLog> response = esClient.search(matchQuery, RequestLog.class);for (Hit<RequestLog> hit : response.hits().hits()) {RequestLog source = hit.source();System.out.println(source);}}
}
应该都是可以执行的,问题不大