hanker

ElasticSearch - Spring Boot에서 Elasticsearch 인덱스 정보 조회 (총 개수, 인덱스 상태 및 메타 정보 조회) 본문

DATABASE/ElasticSearch

ElasticSearch - Spring Boot에서 Elasticsearch 인덱스 정보 조회 (총 개수, 인덱스 상태 및 메타 정보 조회)

hanker 2025. 4. 5. 01:13
반응형

https://hanke-r.tistory.com/entry/ElasticSearch-Springjava%EC%97%90%EC%84%9C-%EC%9D%B8%EB%8D%B1%EC%8A%A4-%EB%AA%A9%EB%A1%9D-%EC%A0%84%EC%B2%B4-%EC%A1%B0%ED%9A%8C

 

ElasticSearch - Spring(java)에서 인덱스 목록 전체 조회

Spring에서 ElasticSearch 내 인덱스 전체 목록을 가져와보자. 엘라스틱 서치 7 버전과 8 버전의 코드가 조금 다르다.아래 코드로 알아보자  0. 의존성 추가 co.elastic.clients elasticsearch-java 8.12.0  1. 엘라

hanke-r.tistory.com

이전 글에서 ElasticSearch 내 인덱스 목록을 가져왔었는데, 이번 글에서는 인덱스의 메타 정보를 조회해서 가져와보자.

 


 

Java Code 구현
    @Autowired
    private final RestHighLevelClient client;
    
    @PostMapping("/mng/indiciesGetKey")
    public String indiciesGetKey(@RequestParam Map<String, Object> params, Model model) throws IOException {
        // 인덱스 명
        String indexName = params.get("index").toString();

        // 인덱스 상태 및 메타정보
        // _cat/indices/<index> 호출 → 인덱스 정보를 JSON 형식으로 받아옴
        // format=json 이 없으면 문자열로 오기 때문에 반드시 설정
        Request request = new Request("GET", "/_cat/indices/" + indexName + "?format=json");
        Response response = client.getLowLevelClient().performRequest(request);

        // 응답 본문을 문자열로 변환 → Jackson으로 JSON 파싱
        // _cat API는 배열로 응답하므로 .get(0)로 첫 번째 인덱스 정보만 추출
        String body = EntityUtils.toString(response.getEntity());
        ObjectMapper mapper = new ObjectMapper();
        JsonNode node = mapper.readTree(body).get(0);

        // 각 항목 추출:
        // health: green / yellow / red
        // store.size: 예: "1.3mb"
        // docs.count: 문서 수 (문자열 형태)
        String health = node.get("health").asText();
        String storeSize = node.get("store.size").asText();
        String docsCount = node.get("docs.count").asText();

        Map<String, Object> result = new HashMap<>();
        result.put("health", health);
        result.put("storeSize", storeSize);
        result.put("docsCount", docsCount);

        System.out.println(result.toString());

        model.addAttribute("data", result);

        return "jsonView";
    }

결과 값

 

ElasticSearch health에 대해서 간단하게 알아보자.

green / yellow / red 로 상태가 나오는데 각 상태에 대한 의미는

green : 모든 샤드가 할당 된 상태

yellow : 기본 샤드는 할당되었지만 복제된 샤드(replica shard)는 할당되지 않은 상태

red : 클러스터에 할당되지 않음

 

 

반응형