/엔지니어/데이터베이스/Elasticsearch 입문 — 설치·인덱스·쿼리
데이터베이스중급UbuntuDebianCentOSelasticsearchkibana검색엔진

Elasticsearch 입문 — 설치·인덱스·쿼리·Kibana 연결

Elasticsearch 설치, 인덱스 생성·문서 CRUD, match/term/range 쿼리, 집계, Kibana 연결까지 검색 엔진 기초를 실습 중심으로 설명합니다.

설치 (Ubuntu, Docker 권장)

Bash
# Docker Compose로 빠른 시작
cat > docker-compose.yml << 'EOF'
version: '3.8'
services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:8.13.0
    environment:
      - discovery.type=single-node
      - xpack.security.enabled=false
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ports:
      - "9200:9200"
    volumes:
      - esdata:/usr/share/elasticsearch/data

  kibana:
    image: docker.elastic.co/kibana/kibana:8.13.0
    ports:
      - "5601:5601"
    environment:
      - ELASTICSEARCH_HOSTS=http://elasticsearch:9200

volumes:
  esdata:
EOF

docker compose up -d

# 상태 확인
curl http://localhost:9200

인덱스 관리

Bash
# 인덱스 생성 (매핑 명시)
curl -X PUT http://localhost:9200/products \
  -H 'Content-Type: application/json' -d '{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 0
  },
  "mappings": {
    "properties": {
      "name":        { "type": "text", "analyzer": "standard" },
      "price":       { "type": "float" },
      "category":    { "type": "keyword" },
      "stock":       { "type": "integer" },
      "createdAt":   { "type": "date" },
      "description": { "type": "text" }
    }
  }
}'

# 인덱스 목록
curl http://localhost:9200/_cat/indices?v

# 인덱스 삭제
curl -X DELETE http://localhost:9200/products

문서 CRUD

Bash
# 문서 추가
curl -X POST http://localhost:9200/products/_doc \
  -H 'Content-Type: application/json' -d '{
  "name": "노트북 Pro 15",
  "price": 1500000,
  "category": "electronics",
  "stock": 50,
  "createdAt": "2025-05-27"
}'

# ID 지정하여 추가/업데이트
curl -X PUT http://localhost:9200/products/_doc/1 \
  -H 'Content-Type: application/json' -d '{
  "name": "무선 마우스",
  "price": 35000,
  "category": "accessories",
  "stock": 200
}'

# 문서 조회
curl http://localhost:9200/products/_doc/1

# 부분 업데이트
curl -X POST http://localhost:9200/products/_update/1 \
  -H 'Content-Type: application/json' -d '{
  "doc": { "price": 32000, "stock": 180 }
}'

# 문서 삭제
curl -X DELETE http://localhost:9200/products/_doc/1

검색 쿼리

Bash
# match — 전문 검색 (형태소 분석)
curl -X GET http://localhost:9200/products/_search \
  -H 'Content-Type: application/json' -d '{
  "query": {
    "match": { "name": "노트북" }
  }
}'

# term — 정확한 값 매칭 (keyword 타입)
curl -X GET http://localhost:9200/products/_search \
  -H 'Content-Type: application/json' -d '{
  "query": {
    "term": { "category": "electronics" }
  }
}'

# range — 범위 검색
curl -X GET http://localhost:9200/products/_search \
  -H 'Content-Type: application/json' -d '{
  "query": {
    "range": {
      "price": { "gte": 100000, "lte": 2000000 }
    }
  }
}'

# bool — 복합 조건
curl -X GET http://localhost:9200/products/_search \
  -H 'Content-Type: application/json' -d '{
  "query": {
    "bool": {
      "must":   [{ "match": { "name": "노트북" } }],
      "filter": [{ "term":  { "category": "electronics" } }],
      "must_not": [{ "range": { "stock": { "lte": 0 } } }]
    }
  },
  "sort": [{ "price": "asc" }],
  "from": 0,
  "size": 10
}'

집계 (Aggregation)

Bash
curl -X GET http://localhost:9200/products/_search \
  -H 'Content-Type: application/json' -d '{
  "size": 0,
  "aggs": {
    "by_category": {
      "terms": { "field": "category", "size": 10 }
    },
    "avg_price": {
      "avg": { "field": "price" }
    },
    "price_range": {
      "range": {
        "field": "price",
        "ranges": [
          { "to": 50000 },
          { "from": 50000, "to": 500000 },
          { "from": 500000 }
        ]
      }
    }
  }
}'

한국어 분석기 설정

Bash
# 노리(nori) 플러그인 설치 (Docker)
docker exec -it elasticsearch \
  bin/elasticsearch-plugin install analysis-nori

# 노리 분석기로 인덱스 생성
curl -X PUT http://localhost:9200/korean-products \
  -H 'Content-Type: application/json' -d '{
  "settings": {
    "analysis": {
      "analyzer": {
        "korean": {
          "type": "nori"
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "name": { "type": "text", "analyzer": "korean" }
    }
  }
}'
#elasticsearch#kibana#검색엔진#인덱스#쿼리
편집 안내 · Editorial Note

이 가이드는 AI 도구를 활용해 초안을 구성하고 사람이 명령어·문맥을 검토해 발행했습니다. 운영체제와 도구 버전에 따라 결과가 달라질 수 있으므로 적용 전 공식 문서를 함께 확인하세요. 오류를 발견하시면 이메일로 제보해 주세요.

질문 & 답변 (Q&A)

이 가이드에 대해 궁금한 점을 질문해보세요. 확인 후 답변드립니다.