docker 方式 elasticsearch 8.13 简单例子

ops/2024/10/21 7:47:27/

docker_0">安装 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);}}
}

应该都是可以执行的,问题不大


http://www.ppmy.cn/ops/37616.html

相关文章

【零基础】system generator①设置卡解析

1.在matlab中我们输入的是双精度浮点型数据&#xff0c;经过gateway后变成定点型。十六位十四个小数位&#xff0c;整个数据有十六位&#xff0c;其中十四位给了小数 2.fixed-point定点型&#xff1b;signed有符号&#xff1b;2’s comp补码 3.量化误差 truncate&#xff0c;舍…

【BUUCTF】Crypto_RSA(铜锁/openssl使用系列)

【BUUCTF】Crypto_RSA&#xff08;铜锁/openssl使用系列&#xff09; 1、题目 在一次RSA密钥对生成中&#xff0c;假设p473398607161&#xff0c;q4511491&#xff0c;e17 求解出d作为flga提交 2、解析 RSA加密过程&#xff1a; 1&#xff09;选择素数&#xff1a;选择两个不…

24上软考5月新通知:今年可能是最简单的一次

备考24上软考的小伙伴注意啦&#xff01;软考新变动&#xff0c;今年软考高级科目通过率&#xff0c;可能会大增!!! 根据辽宁省软考办最新通知&#xff1a; 一、考试时间&#xff1a;5月25日-26日 解读&#xff1a;早前官方公告2024上半年软考考试时间为5月25日-28日&#xff…

八股Day1 集合

1.List Map Set的区别 2.说下集合继承关系 3.Iterator是什么&#xff1f;基本方法&#xff1f; 4.哪些集合线程不安全&#xff1f;怎么解决的&#xff1f; 5.ArrlyList和Vector的区别 6.ArrlyList和LinedList的区别 7.说下ArrayList的扩容机制 8.Set接口中 Comparator和Compara…

用HAL库改写江科大的stm32入门例子_9-1 串口发送

设置串口&#xff1a; 选项说明&#xff1a; 写一个串口发送函数&#xff1a; // serial send string function void serial_send_string(char *str) {HAL_UART_Transmit(&huart1, (uint8_t *)str, strlen(str), 1000); } main函数中调用发送信息&#xff1a; uint8_t dat…

ROS服务器通信

目录 一、角色 二、流程 注意 三、例子描述 四、srv文件 编译配置文件 vscode配置 五、Server.cpp编写例子 编写CMakeList 六、观察server的效果 七、Client编写例子 编写CMakeList 八、观察Client的结果 九、Client优化&#xff08;动态输入&#xff09; 了解argc…

技术分享 | i.MX8M Mini适配MIPI转eDP芯片

1.方案概述 此方案使用HD-8MMN-CORE的核心板搭配TI公司的芯片SN65DSI86转换芯片实现。 SN65DSI86作为一款MIPI DSI转eDP的芯片&#xff0c;支持双通道DSI输入&#xff0c;最大四通道显示输出&#xff0c;最大支持4K60fps输出&#xff0c;WUXGA 1080P。本方案中将采用单通道DS…

LINUX 入门 5.1

LINUX 入门 5.1 day8 20240503 课程链接地址 第5章 数据库mysql项目实战 mysql基础 第四节&#xff1a; mysql_init(&mysql) NULL失败 mysql_real_connect(&mysql, KING_DB_SERVER_IP, KING_DB_USERNAME, KING_DB_PASSWORD, KING_DB_DEFAULTDB, KING_DB_SERVER_PO…