ElasticSearch ‐ Spring Boot, ElasticSearch로 CRUD 구현 - dnwls16071/Backend_Study_TIL GitHub Wiki

📚 ElasticSearch - Spring Boot, ElasticSearch로 CRUD 구현

// Document 클래스 구현하기
package com.example.es;

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

@Document(indexName = "users") // users 인덱스의 Document임을 명시
public class UserDocument {

	@Id
	private String id; // Elasticsearch에서는 Document ID를 문자열(String)으로 다룸

	@Field(type = FieldType.Keyword) // 매핑 정의 : keyword 타입
	private String name;

	@Field(type = FieldType.Long) // 매핑 정의 : long 타입
	private Long age;

	@Field(type = FieldType.Boolean) // 매핑 정의 : boolean 타입
	private Boolean isActive;

	public UserDocument(String id, String name, Long age, Boolean isActive) {
		this.id = id;
		this.name = name;
		this.age = age;
		this.isActive = isActive;
	}

	public String getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public Long getAge() {
		return age;
	}

	public Boolean getIsActive() {
		return isActive;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setAge(Long age) {
		this.age = age;
	}

	public void setIsActive(Boolean isActive) {
		this.isActive = isActive;
	}
}

[Kibana에서 users 인덱스 조회하기]

> GET /users

{
  "users": {
    "aliases": {},
    "mappings": {
      "properties": {
        "_class": {
          "type": "keyword",
          "index": false,
          "doc_values": false
        },
        "age": {
          "type": "long"
        },
        "isActive": {
          "type": "boolean"
        },
        "name": {
          "type": "keyword"
        }
      }
    },
    "settings": {
      "index": {
        "routing": {
          "allocation": {
            "include": {
              "_tier_preference": "data_content"
            }
          }
        },
        "refresh_interval": "1s",
        "number_of_shards": "1",
        "provided_name": "users",
        "creation_date": "1747331723446",
        "number_of_replicas": "1",
        "uuid": "DbBnn9ZMQ1Sol1p0xz80Tg",
        "version": {
          "created": "8521000"
        }
      }
    }
  }
}

실제 현업에서는 ElasticSearch만 단독으로 사용하지 않고, 주로 MySQL과 같은 RDB와 ElasticSearch를 같이 사용하는 편이다.