package com.example.es8.controller;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.mapping.DynamicMapping;
import co.elastic.clients.elasticsearch.core.IndexResponse;
import co.elastic.clients.elasticsearch.indices.CreateIndexResponse;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * @author 喜欢番茄火锅
 * @date 2023/12/4 18:30
 */
@RestController
@Slf4j
@RequestMapping("/test")
public class Test {
    @Resource
    private ElasticsearchClient elasticsearchClient;

    @GetMapping("/run1")
    @SneakyThrows
    public String run1(String index) {
        CreateIndexResponse createIndexResponse = elasticsearchClient.indices()
                .create(createIndexRequest ->
                        createIndexRequest.index(index).settings(s -> s.numberOfReplicas("0").numberOfShards("3"))
                );
        log.info("== {} 索引创建是否成功: {}", index, createIndexResponse.acknowledged());
        return "jieshu";

    }
    @GetMapping("/run2")
    @SneakyThrows
    public String run2(String index) {
        CreateIndexResponse createIndexResponse = elasticsearchClient.indices()
                .create(createIndexRequest ->
                        createIndexRequest.index(index).settings(s -> s.numberOfReplicas("0").numberOfShards("3"))
                );
        log.info("== {} 索引创建是否成功: {}", index, createIndexResponse.acknowledged());

        IndexResponse indexResponse = elasticsearchClient.index(indexRequest -> {
//                indexRequest.index(INDEX_NAME).document(new AhaIndex().setName("wangWu").setAge(21))
            HashMap<String, String> map = new HashMap<>();
            map.put("index", "index");
            map.put("gongsi", "gongsi" + "finalI");
            indexRequest.index(index).document(map);
            return indexRequest;
        });
        log.info("== response: {}, responseStatus: {}", indexResponse, indexResponse.result());
        return "jieshu";
    }
    @GetMapping("/run3")
    @SneakyThrows
    public String run3(String index) {
        HashMap<String, String> map = new HashMap<>();
        map.put("index", "index");
        map.put("gongsi", "gongsi" + "finalI");

        CreateIndexResponse createIndexResponse = elasticsearchClient.indices()
                .create(createIndexRequest ->
                        createIndexRequest
                                .index(index)
                                .settings(s -> s.numberOfReplicas("0").numberOfShards("3")
                                ).mappings(m->m.dynamic(DynamicMapping.True))
                );
        log.info("== {} 索引创建是否成功: {}", index, createIndexResponse.acknowledged());

        IndexResponse indexResponse = elasticsearchClient.index(indexRequest -> {
//                indexRequest.index(INDEX_NAME).document(new AhaIndex().setName("wangWu").setAge(21))
            indexRequest.index(index).document(map);
            return indexRequest;
        });
        log.info("== response: {}, responseStatus: {}", indexResponse, indexResponse.result());
        return "jieshu";
    }
}
