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

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

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

목차
  1. 설치 (Ubuntu, Docker 권장)
  2. 인덱스 관리
  3. 문서 CRUD
  4. 검색 쿼리
  5. 집계 (Aggregation)
  6. 한국어 분석기 설정

설치 (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 도구가 초안 작성에 사용될 수 있습니다. 명령어의 실행 여부와 검증 환경은 각 가이드에 기재된 근거를 확인하세요. 적용 전 사용 중인 버전의 공식 문서를 확인하고, 오류는 이메일로 알려주세요. 이메일로 제보

주제 관련 공식 문서(자동 연결 · 문장별 근거 아님)Elasticsearch 공식 가이드 ↗

질문 & 답변 (Q&A)

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