인덱스(Index)란?

📌 Elasticsearch에서 인덱스(Index)는 데이터를 저장하는 논리적인 공간입니다.
관계형 데이터베이스(RDBMS)의 테이블(Table) 개념과 유사하며, 도큐먼트(Document)의 집합을 의미합니다.

(1) 인덱스의 특징

  • 도큐먼트(Document)를 저장하는 컨테이너 역할.
  • 샤드(Shard) 단위로 데이터를 분할하여 분산 저장.
  • 매핑(Mapping)을 이용해 데이터 유형을 정의.
  • 검색 시 역색인(Inverted Index) 구조를 활용하여 빠르게 조회 가능.

인덱스 생성 및 관리

(1) 인덱스 생성 (PUT /index_name)

PUT /products
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 1
  }
}
  • 프라이머리 샤드 3개, 복제본 1개로 products 인덱스를 생성.
  • number_of_shards: 데이터 저장을 위한 기본 샤드 개수.
  • number_of_replicas: 복제본 개수 (데이터 안정성 확보).

(2) 인덱스 조회 (GET /_cat/indices?v)

GET _cat/indices?v

🔹 응답 예시

health status index    uuid                   pri rep docs.count size
green  open   products  8vG7YxH0T9S9lF-4jL9H7g  3   1   10         50kb

✅ 현재 클러스터에 존재하는 모든 인덱스 정보를 확인할 수 있음.


(3) 인덱스 삭제 (DELETE /index_name)

DELETE /products

✅ products 인덱스를 삭제.


매핑(Mapping)과 데이터 타입

(1) 매핑(Mapping)이란?

  • Elasticsearch에서 데이터의 스키마를 정의하는 역할.
  • 각 필드의 데이터 타입을 지정 (text, keyword, integer, date, boolean 등).
  • RDBMS의 스키마(Schema)와 유사하지만, 동적으로 확장 가능.

(2) 주요 데이터 타입

데이터 타입 설명
text 전문 검색(Full-text search)에 최적화됨 (형태소 분석 가능)
keyword 정렬 및 필터링에 최적화됨 (형태소 분석 없음)
integer 정수 값 저장
double 실수 값 저장
boolean true 또는 false 값 저장
date 날짜 (YYYY-MM-DD, epoch_millis) 형식 저장
geo_point 위도/경도 좌표 저장 (위치 정보 검색 가능)

(3) 사용자 지정 매핑 예제

PUT /products
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 1
  },
  "mappings": {
    "properties": {
      "name": { "type": "text" },
      "category": { "type": "keyword" },
      "price": { "type": "integer" },
      "stock": { "type": "boolean" },
      "release_date": { "type": "date", "format": "yyyy-MM-dd" }
    }
  }
}

✅ products 인덱스의 매핑을 정의하여 각 필드의 데이터 유형을 지정.


(4) 매핑 정보 확인 (GET /index_name/_mapping)

GET /products/_mapping

🔹 응답 예시

{
  "products": {
    "mappings": {
      "properties": {
        "name": { "type": "text" },
        "category": { "type": "keyword" },
        "price": { "type": "integer" },
        "stock": { "type": "boolean" },
        "release_date": { "type": "date", "format": "yyyy-MM-dd" }
      }
    }
  }
}

✅ 현재 products 인덱스의 매핑 정보를 조회.


(5) 기존 인덱스에 필드 추가 (PUT /index_name/_mapping)

PUT /products/_mapping
{
  "properties": {
    "rating": { "type": "double" }
  }
}

✅ 기존 products 인덱스에 rating 필드를 추가.


다중 필드(Multi-field)

(1) Multi-field 개념

  • 하나의 필드를 여러 개의 타입으로 저장하여 서로 다른 용도로 사용 가능.
  • 예: text 타입으로 검색, keyword 타입으로 정렬/필터링.

(2) Multi-field 설정 예제

PUT /products
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text",
        "fields": {
          "raw": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

✅ name 필드는 기본적으로 **text 타입(검색용)**이며, "name.raw"는 **keyword 타입(정렬/필터링용)**으로 저장.


(3) Multi-field 검색 예제

GET /products/_search
{
  "query": {
    "match": {
      "name": "Elasticsearch 책"
    }
  }
}

✅ "name" 필드는 text 타입이므로 형태소 분석을 적용하여 검색.

GET /products/_search
{
  "query": {
    "term": {
      "name.raw": "Elasticsearch 책"
    }
  }
}

✅ "name.raw" 필드는 keyword 타입이므로 정확한 단어 일치 검색 가능.


동적 매핑(Dynamic Mapping)

(1) 동적 매핑이란?

  • Elasticsearch는 기본적으로 새로운 필드를 자동으로 감지하여 추가.
  • 사용자가 명시적으로 매핑을 지정하지 않아도 동적으로 필드가 생성됨.

(2) 동적 매핑 기본 예제

PUT /dynamic_test/_doc/1
{
  "product": "Laptop",
  "price": 1200,
  "in_stock": true,
  "release_date": "2023-07-01"
}

✅ 매핑을 지정하지 않았지만, Elasticsearch가 자동으로 필드 유형을 생성.


(3) 동적 매핑 확인

GET /dynamic_test/_mapping

🔹 응답 예시

{
  "dynamic_test": {
    "mappings": {
      "properties": {
        "product": { "type": "text" },
        "price": { "type": "long" },
        "in_stock": { "type": "boolean" },
        "release_date": { "type": "date" }
      }
    }
  }
}

✅ Elasticsearch가 자동으로 데이터 유형을 지정.


(4) 동적 매핑 비활성화

자동 필드 생성을 방지하려면 "dynamic": "strict" 설정을 추가해야 합니다.

PUT /strict_index
{
  "mappings": {
    "dynamic": "strict",
    "properties": {
      "name": { "type": "text" },
      "price": { "type": "integer" }
    }
  }
}

정의되지 않은 필드를 입력하면 오류가 발생하도록 설정.


결론

  1. 인덱스(Index)는 데이터를 저장하는 논리적 컨테이너.
  2. 매핑(Mapping)을 사용하여 각 필드의 데이터 유형을 정의.
  3. Multi-field를 이용해 하나의 필드를 여러 타입으로 저장 가능.
  4. 동적 매핑(Dynamic Mapping)은 자동으로 데이터 유형을 감지하여 필드를 추가.

 

+ Recent posts