docker 方式 elasticsearch 8.13 简单例子

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

    @Bean
    public ElasticsearchClient getClient(){
        // Create the low-level client
        RestClient restClient = RestClient
                .builder(HttpHost.create(serverUrl))
                .setDefaultHeaders(new Header[]{
                        new BasicHeader("Authorization", "ApiKey " + apiKey)
                })
                .build();

// Create the transport with a Jackson mapper
        ElasticsearchTransport transport = new RestClientTransport(
                restClient, new JacksonJsonpMapper());

// And create the API client
        ElasticsearchClient esClient = new ElasticsearchClient(transport);
        return  esClient;
    }
}

编写测试用例

@SpringBootTest
class DemoApplicationTests {

    private Logger logger = LoggerFactory.getLogger(DemoApplicationTests.class);

    @Autowired
    private ElasticsearchClient esClient;
    private String index = "request_log";
    @Test
    public void test() throws IOException {
//        esClient.indices().create(c -> c.index("request_log"));
    }

    @Test
    public 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));
        }
    }
    @Test
    public 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;
    }
    @Test
    public 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);
        }
    }
}

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

相关推荐

  1. store 简单引入例子

    2024-05-09 06:44:05       27 阅读
  2. iocp简单例子

    2024-05-09 06:44:05       32 阅读
  3. selenium XPATH 使用简单例子

    2024-05-09 06:44:05       28 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-09 06:44:05       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-09 06:44:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-09 06:44:05       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-09 06:44:05       20 阅读

热门阅读

  1. React 学习-3

    2024-05-09 06:44:05       7 阅读
  2. 001 websocket(评论功能demo)(消息推送)

    2024-05-09 06:44:05       10 阅读
  3. react 项目中使用 iconfont

    2024-05-09 06:44:05       11 阅读
  4. Kafka 环境搭建之伪分布式集群模式详细教程

    2024-05-09 06:44:05       12 阅读
  5. Jenkins的原理及应用详解(二)

    2024-05-09 06:44:05       10 阅读
  6. C++ Opencv之图像数据拷贝分析

    2024-05-09 06:44:05       11 阅读
  7. nodejs之log4js日志管理

    2024-05-09 06:44:05       13 阅读
  8. AR技术的那些事

    2024-05-09 06:44:05       10 阅读
  9. CUDA笔记

    2024-05-09 06:44:05       11 阅读
  10. uni-app 自定义tabbar

    2024-05-09 06:44:05       11 阅读