当前位置:   article > 正文

docker 方式 elasticsearch 8.13 简单例子_docker 搭建es8.13容器

docker 搭建es8.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
  • 1
  • 2
  • 3
  • 4

如果安装不成功,报如下的错误

ERROR: Elasticsearch exited unexpectedly, with exit code 78
  • 1

修改 /etc/sysctl.conf
在文件最后添加一行
vm.max_map_count=262144
使配置生效

sysctl -p
  • 1

访问 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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

配置 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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

编写测试用例

@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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/656470
推荐阅读
相关标签
  

闽ICP备14008679号