๐Ÿ”

Elasticsearch ์น˜ํŠธ์‹œํŠธ

Docker & Linux ์„ค์น˜, Nori ํ•œ๊ธ€ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ, ์ธ๋ฑ์Šค ๋งคํ•‘, CRUD & Bulk API, Query DSL(Full Text, Term, Bool, Range), ์ง‘๊ณ„(Aggregations), ํด๋Ÿฌ์Šคํ„ฐ ํŠœ๋‹ ๋ฐ Spring/Python ์—ฐ๋™ ์‹ค๋ฌด ๊ฐ€์ด๋“œ

--views
๋‹จ์ˆ˜:
#elasticsearch#elastic#search-engine#nosql#nori#query-dsl#docker#kibana#backend์ตœ์ข… ์—…๋ฐ์ดํŠธ: 2026-10-04

1. Elasticsearch ํ•ต์‹ฌ ์•„ํ‚คํ…์ฒ˜ & ๋™์ž‘ ์›๋ฆฌ (Architecture)

graph LR
  subgraph Client ["Client / App"]
    Req[REST API Request<br/>JSON Query DSL]
  end

  subgraph Cluster ["Elasticsearch Cluster"]
    subgraph Node1 ["Master-Eligible / Data Node 1"]
      P0["Index: products<br/>Primary Shard 0"]
      R1["Index: products<br/>Replica Shard 1"]
    end
    subgraph Node2 ["Data Node 2"]
      P1["Index: products<br/>Primary Shard 1"]
      R0["Index: products<br/>Replica Shard 0"]
    end
  end

  Req --> Node1
  Req --> Node2
  P0 -.->|Replication| R0
  P1 -.->|Replication| R1
โ–  RDBMS vs Elasticsearch ํ•ต์‹ฌ ๊ฐœ๋… ๋งคํ•‘
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
  RDBMS               Elasticsearch            ์„ค๋ช…
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
  Database (DB)   ->  Index                    ๋ฐ์ดํ„ฐ ๋…ผ๋ฆฌ์  ์ €์žฅ์†Œ ๋‹จ์œ„
  Table           ->  Index (๊ณผ๊ฑฐ Type ํ์ง€)   7.x+๋ถ€ํ„ฐ ์ธ๋ฑ์Šค ๋‹น 1๊ฐœ์˜ ๋งคํ•‘ ํƒ€์ž…๋งŒ ํ—ˆ์šฉ
  Row             ->  Document                 JSON ํฌ๋งท ๋‹จ์ผ ๋ฐ์ดํ„ฐ ํ–‰
  Column          ->  Field                    JSON ์†์„ฑ (text, keyword, integer ๋“ฑ)
  Schema          ->  Mapping                  ํ•„๋“œ ๋ฐ์ดํ„ฐ ํƒ€์ž…๊ณผ ๋ถ„์„๊ธฐ(Analyzer) ์ •์˜
  SQL             ->  Query DSL                JSON ๊ธฐ๋ฐ˜ ๊ฒ€์ƒ‰ ๋ฐ ์ง‘๊ณ„ ์งˆ์˜ ์–ธ์–ด
  B-Tree Index    ->  Inverted Index (์—ญ์ƒ‰์ธ)  ๋‹จ์–ด(Term)๊ฐ€ ์œ„์น˜ํ•œ ๋ฌธ์„œ ID๋ฅผ ๋งคํ•‘ํ•˜๋Š” ๊ตฌ์กฐ
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
โ€ข ์—ญ์ƒ‰์ธ(Inverted Index): ํ…์ŠคํŠธ๋ฅผ ํ† ํฐํ™”ํ•˜์—ฌ ๋‹จ์–ด ์‚ฌ์ „์„ ๋งŒ๋“ค๊ณ , ํ•ด๋‹น ๋‹จ์–ด๊ฐ€ ํฌํ•จ๋œ Document ID ๋ชฉ๋ก์„ ๋ณด๊ด€ํ•˜์—ฌ ์ดˆ๊ณ ์† ๊ฒ€์ƒ‰ ์ œ๊ณต
โ€ข Shard: ์ธ๋ฑ์Šค๋ฅผ ๋ฌผ๋ฆฌ์ ์œผ๋กœ ๋ถ„ํ• ํ•œ ๋‹จ์œ„ (Primary Shard: ์›๋ณธ ๋ฐ์ดํ„ฐ, Replica Shard: ์žฅ์•  ๋ณต๊ตฌ ๋ฐ ๊ฒ€์ƒ‰ ๋ถ„์‚ฐ์šฉ ๋ณต์ œ๋ณธ)

2. Elasticsearch & Kibana ์„ค์น˜ & ์‹คํ–‰ ๊ฐ€์ด๋“œ (Installation)

๐Ÿ’ป ์‹œ์Šคํ…œ ์„ค์น˜ ๋ฐ ์‹คํ–‰ ์š”๊ตฌ์‚ฌ์–‘ (System Requirements)

ํ•ญ๋ชฉ์ตœ์†Œ ์‚ฌ์–‘ (Minimum)๊ถŒ์žฅ ์‚ฌ์–‘ (Recommended)์ฐธ๊ณ  ๋ฐ ๋น„๊ณ 
์šด์˜์ฒด์ œ (OS)Linux 64-bit (Ubuntu, RHEL, Debian), macOS, Windows (WSL2/Docker)Ubuntu 22.04 LTS, RHEL 9 (Linux ๊ถŒ์žฅ)์šด์˜ ํ™˜๊ฒฝ์€ POSIX Linux ํ™˜๊ฒฝ ๊ฐ•๋ ฅ ๊ถŒ์žฅ
CPU2 ์ฝ”์–ด ์ด์ƒ (x86_64, ARM64)4 ์ฝ”์–ด ~ 8 ์ฝ”์–ด ์ด์ƒ๋Œ€๋Ÿ‰ ์ƒ‰์ธ ๋ฐ Query DSL Full-text ๋ถ„์‚ฐ ์—ฐ์‚ฐ
๋ฉ”๋ชจ๋ฆฌ (RAM)์ตœ์†Œ 4 GB (JVM ํž™ 2 GB ํ• ๋‹น)8 GB ~ 32 GB (์‹œ์Šคํ…œ ๋ฉ”๋ชจ๋ฆฌ์˜ 50%๋ฅผ JVM ํž™์— ํ• ๋‹น)๋‚˜๋จธ์ง€ 50%๋Š” OS ํŒŒ์ผ์‹œ์Šคํ…œ ์บ์‹œ(Lucene)๋กœ ํ™œ์šฉ
๋””์Šคํฌ (Storage)10 GB ์ด์ƒ์˜ ์—ฌ์œ  ๊ณต๊ฐ„50 GB ~ 200 GB ์ด์ƒ์˜ ๊ณ ์† NVMe/SSD์ƒค๋“œ ๋ฐ์ดํ„ฐ ์ €์žฅ์†Œ, ํŠธ๋žœ์žญ์…˜ ๋กœ๊ทธ(Translog)
๋„คํŠธ์›Œํฌ / ๋Ÿฐํƒ€์ž„OpenJDK 21 LTS+ (Elasticsearch 8.17+ / 9.x ๋ฒˆ๋“ค ํฌํ•จ)ํฌํŠธ 9200(HTTP REST), 9300(Node ํ†ต์‹ )vm.max_map_count >= 262144 ์ปค๋„ ํŒŒ๋ผ๋ฏธํ„ฐ ํ•„์ˆ˜
# ๐Ÿณ [1] Docker ๋‹จ์ผ ์ปจํ…Œ์ด๋„ˆ ๋น ๋ฅธ ์‹คํ–‰ (๊ฐœ๋ฐœ & ๋กœ์ปฌ ํ…Œ์ŠคํŠธ์šฉ)
# Elasticsearch 8.x๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ณด์•ˆ(HTTPS/๋น„๋ฐ€๋ฒˆํ˜ธ)์ด ๊ฐ•์ œ๋˜๋ฏ€๋กœ, ๋กœ์ปฌ ํ…Œ์ŠคํŠธ ์‹œ xpack.security.enabled=false ์˜ต์…˜์œผ๋กœ ๊ฐ„ํŽธ ์‹คํ–‰ ๊ฐ€๋Šฅ
docker run -d \
  --name elasticsearch \
  -p 9200:9200 -p 9300:9300 \
  -e "discovery.type=single-node" \
  -e "xpack.security.enabled=false" \
  -e "ES_JAVA_OPTS=-Xms1g -Xmx1g" \
  -v es_data:/usr/share/elasticsearch/data \
  docker.elastic.co/elasticsearch/elasticsearch:8.17.0
# ๐Ÿณ [2] Docker Compose: Elasticsearch + Kibana + Nori ํ•œ๊ธ€ ๋ถ„์„๊ธฐ
# docker-compose.yml
version: '3.8'

services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:8.17.0
    container_name: elasticsearch
    environment:
      - node.name=es01
      - cluster.name=es-docker-cluster
      - discovery.type=single-node
      - xpack.security.enabled=false
      - "ES_JAVA_OPTS=-Xms2g -Xmx2g"
    volumes:
      - es_data:/usr/share/elasticsearch/data
    ports:
      - "9200:9200"
      - "9300:9300"
    ulimits:
      memlock:
        soft: -1
        hard: -1
      nofile:
        soft: 65536
        hard: 65536

  kibana:
    image: docker.elastic.co/kibana/kibana:8.17.0
    container_name: kibana
    environment:
      - ELASTICSEARCH_HOSTS=http://elasticsearch:9200
      - I18N_LOCALE=ko-KR
    ports:
      - "5601:5601"
    depends_on:
      - elasticsearch

volumes:
  es_data:
# Nori ํ•œ๊ธ€ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ ํ”Œ๋Ÿฌ๊ทธ์ธ ์„ค์น˜ (์ปจํ…Œ์ด๋„ˆ ์‹คํ–‰ ํ›„ 1ํšŒ ์‹คํ–‰)
docker exec -it elasticsearch bin/elasticsearch-plugin install analysis-nori
docker restart elasticsearch

# ๐Ÿง [3] Linux (Ubuntu / Debian) APT ํŒจํ‚ค์ง€ ์„ค์น˜
# GPG Key ๋“ฑ๋ก ๋ฐ APT Repository ์ถ”๊ฐ€
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elasticsearch-keyring.gpg
sudo apt install -y apt-transport-https
echo "deb [signed-by=/usr/share/keyrings/elasticsearch-keyring.gpg] https://artifacts.elastic.co/packages/8.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elastic-8.x.list

sudo apt update && sudo apt install -y elasticsearch
sudo /usr/share/elasticsearch/bin/elasticsearch-plugin install analysis-nori

# ์„œ๋น„์Šค ๋“ฑ๋ก ๋ฐ ์‹œ์ž‘
sudo systemctl daemon-reload
sudo systemctl enable --now elasticsearch
sudo systemctl status elasticsearch

# ๐Ÿ [4] macOS (Homebrew ์„ค์น˜)
brew tap elastic/tap
brew install elastic/tap/elasticsearch-full
brew services start elastic/tap/elasticsearch-full

# ๐ŸชŸ [5] Windows ํ™˜๊ฒฝ
# WSL2(Ubuntu) ํ„ฐ๋ฏธ๋„์—์„œ ์œ„ Linux APT ๋ฐฉ์‹์œผ๋กœ ์„ค์น˜ํ•˜๊ฑฐ๋‚˜ Docker Desktop ์‹คํ–‰์„ ์ ๊ทน ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค.
# ๐Ÿ” [6] ํด๋Ÿฌ์Šคํ„ฐ ์ƒํƒœ ํ™•์ธ & ํ—ฌ์Šค์ฒดํฌ (Cluster Health & Cat APIs)
# 1. ํด๋Ÿฌ์Šคํ„ฐ ํ—ฌ์Šค (status: green / yellow / red)
curl -X GET "http://localhost:9200/_cluster/health?pretty"

# 2. ๋…ธ๋“œ ๋ชฉ๋ก ๋ฐ ๋ฆฌ์†Œ์Šค ์ ์œ  ์ƒํƒœ
curl -X GET "http://localhost:9200/_cat/nodes?v&h=ip,name,role,master,heap.percent,cpu"

# 3. ์ „์ฒด ์ธ๋ฑ์Šค ๋ชฉ๋ก ๋ฐ ๋ฌธ์„œ ์ˆ˜/์šฉ๋Ÿ‰ ํ™•์ธ
curl -X GET "http://localhost:9200/_cat/indices?v&s=index"

# 4. ์ƒค๋“œ ๋ถ„์‚ฐ ์ƒํƒœ ์ ๊ฒ€
curl -X GET "http://localhost:9200/_cat/shards?v"

3. ์ธ๋ฑ์Šค ์ƒ์„ฑ, ๋งคํ•‘ & Nori ํ•œ๊ธ€ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ (Index & Mapping)

# [1] Nori ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ ๊ธฐ๋ฐ˜ ์ธ๋ฑ์Šค ๋ฐ ๋งคํ•‘ ์ƒ์„ฑ (products ์ธ๋ฑ์Šค)
curl -X PUT "http://localhost:9200/products" -H 'Content-Type: application/json' -d'
{
  "settings": {
    "number_of_shards": 2,
    "number_of_replicas": 1,
    "index.refresh_interval": "1s",
    "analysis": {
      "tokenizer": {
        "nori_user_dict_tokenizer": {
          "type": "nori_tokenizer",
          "decompound_mode": "mixed",
          "user_dictionary_rules": [
            "๋งฅ๋ถํ”„๋กœ",
            "์•„์ดํฐ"
          ]
        }
      },
      "analyzer": {
        "korean_analyzer": {
          "type": "custom",
          "tokenizer": "nori_user_dict_tokenizer",
          "filter": [
            "nori_readingform",
            "lowercase",
            "nori_part_of_speech_filter"
          ]
        }
      },
      "filter": {
        "nori_part_of_speech_filter": {
          "type": "nori_part_of_speech",
          "stoptags": [
            "E", "IC", "J", "MAG", "MAJ", "MM", "SP", "SSC", "SSO", "SC", "SE", "XPN", "XSA", "XSN", "XSV", "UNA", "NA", "VSV"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "id": { "type": "keyword" },
      "title": {
        "type": "text",
        "analyzer": "korean_analyzer",
        "fields": {
          "keyword": { "type": "keyword", "ignore_above": 256 }
        }
      },
      "description": {
        "type": "text",
        "analyzer": "korean_analyzer"
      },
      "category": { "type": "keyword" },
      "tags": { "type": "keyword" },
      "price": { "type": "long" },
      "stock": { "type": "integer" },
      "is_active": { "type": "boolean" },
      "created_at": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
      }
    }
  }
}'
# [2] ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ ๋™์ž‘ ํ…Œ์ŠคํŠธ (_analyze API)
curl -X POST "http://localhost:9200/products/_analyze" -H 'Content-Type: application/json' -d'
{
  "analyzer": "korean_analyzer",
  "text": "์ตœ์‹  ๋งฅ๋ถํ”„๋กœ์™€ ์• ํ”Œ ๋ฌด์„  ์ด์–ดํฐ์„ ๊ตฌ๋งคํ–ˆ์Šต๋‹ˆ๋‹ค."
}
'

# [3] ์ธ๋ฑ์Šค ๋งคํ•‘ ํ™•์ธ ๋ฐ ์ธ๋ฑ์Šค ์‚ญ์ œ
curl -X GET "http://localhost:9200/products/_mapping?pretty"
curl -X DELETE "http://localhost:9200/products"

[!TIP] text vs keyword ํƒ€์ž…์˜ ์ฐจ์ด:

  • text: ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ(Analyzer)์— ์˜ํ•ด ํ† ํฐ์œผ๋กœ ์ชผ๊ฐœ์ ธ ์ „๋ฌธ ๊ฒ€์ƒ‰(Full-text Search, match ์ฟผ๋ฆฌ)์— ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • keyword: ์ชผ๊ฐœ์ง€ ์•Š๊ณ  ์›๋ฌธ ๊ทธ๋Œ€๋กœ ์—ญ์ƒ‰์ธ๋˜์–ด ์™„์ „ ์ผ์น˜ ๊ฒ€์ƒ‰(term ์ฟผ๋ฆฌ), ์ •๋ ฌ(sort), ์ง‘๊ณ„(aggregations, group by)์— ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

4. ๋„ํ๋จผํŠธ CRUD & Bulk ๋Œ€๋Ÿ‰ ์ฒ˜๋ฆฌ (Document CRUD & Bulk)

# [1] ๋‹จ๊ฑด ๋„ํ๋จผํŠธ ๋“ฑ๋ก (POST: ID ์ž๋™์ƒ์„ฑ / PUT: ID ์ง€์ •)
# ID 1๋ฒˆ ๋„ํ๋จผํŠธ ๋ช…์‹œ์  ์ƒ์„ฑ
curl -X PUT "http://localhost:9200/products/_doc/1" -H 'Content-Type: application/json' -d'
{
  "id": "PROD-001",
  "title": "์• ํ”Œ M3 ๋งฅ๋ถํ”„๋กœ 16์ธ์น˜ ์ŠคํŽ˜์ด์Šค ๋ธ”๋ž™",
  "description": "์ตœ๊ณ ์˜ ์„ฑ๋Šฅ์„ ์ž๋ž‘ํ•˜๋Š” ์ „๋ฌธ ์ž‘์—…์šฉ ๋งฅ๋ถํ”„๋กœ ๋…ธํŠธ๋ถ์ž…๋‹ˆ๋‹ค.",
  "category": "๋…ธํŠธ๋ถ",
  "tags": ["์• ํ”Œ", "๋…ธํŠธ๋ถ", "M3", "๋งฅ๋ถ"],
  "price": 3690000,
  "stock": 25,
  "is_active": true,
  "created_at": "2026-10-04 10:00:00"
}'

# [2] ๋„ํ๋จผํŠธ ๋‹จ๊ฑด ์กฐํšŒ (GET)
curl -X GET "http://localhost:9200/products/_doc/1?pretty"

# [3] ๋„ํ๋จผํŠธ ๋ถ€๋ถ„ ์ˆ˜์ • (_update API)
curl -X POST "http://localhost:9200/products/_update/1" -H 'Content-Type: application/json' -d'
{
  "doc": {
    "price": 3590000,
    "stock": 20
  }
}'

# [4] ๋„ํ๋จผํŠธ ์‚ญ์ œ (DELETE)
curl -X DELETE "http://localhost:9200/products/_doc/1"
# [5] Bulk API ๋Œ€๋Ÿ‰ ์ธ๋ฑ์‹ฑ (NDJSON ํฌ๋งท - ๋ช…๋ นํ–‰๊ณผ ๋ฐ์ดํ„ฐํ–‰์ด ๋ฒˆ๊ฐˆ์•„ ๋ฐฐ์น˜๋˜๋ฉฐ ๋์— ๋ฐ˜๋“œ์‹œ ๊ฐœํ–‰ ํฌํ•จ)
curl -X POST "http://localhost:9200/_bulk" -H 'Content-Type: application/x-ndjson' -d'
{ "index" : { "_index" : "products", "_id" : "2" } }
{ "id": "PROD-002", "title": "์‚ผ์„ฑ ๊ฐค๋Ÿญ์‹œ๋ถ4 ํ”„๋กœ 14์ธ์น˜", "description": "๊ฐ€๋ณ๊ณ  ํœด๋Œ€์„ฑ์ด ๋›ฐ์–ด๋‚œ ๊ณ ์„ฑ๋Šฅ ์ธํ…” ์šธํŠธ๋ผ ๋…ธํŠธ๋ถ", "category": "๋…ธํŠธ๋ถ", "tags": ["์‚ผ์„ฑ", "๋…ธํŠธ๋ถ", "๊ฐค๋Ÿญ์‹œ"], "price": 1850000, "stock": 40, "is_active": true, "created_at": "2026-10-04 11:00:00" }
{ "index" : { "_index" : "products", "_id" : "3" } }
{ "id": "PROD-003", "title": "LG ๊ทธ๋žจ ํ”„๋กœ 16 ์šธํŠธ๋ผ", "description": "์ดˆ๊ฒฝ๋Ÿ‰ ๋Œ€ํ™”๋ฉด ์ดˆ์Šฌ๋ฆผ ๋…ธํŠธ๋ถ", "category": "๋…ธํŠธ๋ถ", "tags": ["LG", "๋…ธํŠธ๋ถ", "๊ทธ๋žจ"], "price": 2100000, "stock": 15, "is_active": true, "created_at": "2026-10-04 12:00:00" }
{ "index" : { "_index" : "products", "_id" : "4" } }
{ "id": "PROD-004", "title": "์• ํ”Œ ๋งค์ง ๋งˆ์šฐ์Šค ํ™”์ดํŠธ", "description": "๋ธ”๋ฃจํˆฌ์Šค ๋ฉ€ํ‹ฐ ํ„ฐ์น˜ ์ œ์Šค์ฒ˜ ์ง€์› ๋ฌด์„  ๋งˆ์šฐ์Šค", "category": "์ฃผ๋ณ€๊ธฐ๊ธฐ", "tags": ["์• ํ”Œ", "๋งˆ์šฐ์Šค", "๋ฌด์„ "], "price": 119000, "stock": 100, "is_active": true, "created_at": "2026-10-04 13:00:00" }
{ "index" : { "_index" : "products", "_id" : "5" } }
{ "id": "PROD-005", "title": "๋กœ์ง€ํ… MX Master 3S ๋งˆ์šฐ์Šค", "description": "๋ฌด์†Œ์Œ ํด๋ฆญ๊ณผ ์ดˆ๊ณ ์† ํœ ์„ ํƒ‘์žฌํ•œ ์—…๋ฌด์šฉ ๋ํŒ์™• ๋งˆ์šฐ์Šค", "category": "์ฃผ๋ณ€๊ธฐ๊ธฐ", "tags": ["๋กœ์ง€ํ…", "๋งˆ์šฐ์Šค", "๋ธ”๋ฃจํˆฌ์Šค"], "price": 139000, "stock": 80, "is_active": true, "created_at": "2026-10-04 14:00:00" }

'

5. ์ฟผ๋ฆฌ DSL ํ•ต์‹ฌ ๊ฒ€์ƒ‰ (Query DSL: Match, Term, Bool, Range)

# [1] match ๊ฒ€์ƒ‰ (์ „๋ฌธ ํ˜•ํƒœ์†Œ ๋ถ„์„ ๊ฒ€์ƒ‰: '๋งฅ๋ถ' ๋˜๋Š” '๋…ธํŠธ๋ถ' ๊ฒ€์ƒ‰)
curl -X GET "http://localhost:9200/products/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "match": {
      "title": "๋งฅ๋ถ ๋…ธํŠธ๋ถ"
    }
  }
}'

# [2] term ๊ฒ€์ƒ‰ (์ •ํ™•ํ•œ ์™„์ „ ์ผ์น˜ ๊ฒ€์ƒ‰ - keyword ํ•„๋“œ์— ์ฃผ๋กœ ์‚ฌ์šฉ)
curl -X GET "http://localhost:9200/products/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "term": {
      "category": "๋…ธํŠธ๋ถ"
    }
  }
}'

# [3] multi_match ๊ฒ€์ƒ‰ (์—ฌ๋Ÿฌ ํ•„๋“œ์— ๊ฑธ์ณ ๊ฒ€์ƒ‰ + ๊ฐ€์ค‘์น˜ boost ๋ถ€์—ฌ)
# title ํ•„๋“œ ์ผ์น˜ ์‹œ ์ ์ˆ˜๋ฅผ 3๋ฐฐ(^3) ๊ฐ€์ค‘์น˜ ๋ถ€์—ฌ
curl -X GET "http://localhost:9200/products/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "multi_match": {
      "query": "์• ํ”Œ",
      "fields": ["title^3", "description", "tags"]
    }
  }
}'
# [4] ์‹ค๋ฌด ํ‘œ์ค€: bool ์ฟผ๋ฆฌ (must, filter, should, must_not ์กฐํ•ฉ)
# โ€ข must: ์ ์ˆ˜ ๊ณ„์‚ฐ ๋ฐ˜์˜ + ํ•„์ˆ˜ ์ผ์น˜
# โ€ข filter: ์ ์ˆ˜ ๊ณ„์‚ฐ ์•ˆ ํ•จ (์บ์‹ฑ๋˜์–ด ์„ฑ๋Šฅ ๋งค์šฐ ๋น ๋ฆ„) + ์กฐ๊ฑด ํ•„ํ„ฐ๋ง
# โ€ข should: ํ•„์ˆ˜ ์•„๋‹˜ (๋งค์นญ ์‹œ ๊ฒ€์ƒ‰ ์ˆœ์œ„ ์ ์ˆ˜ ์ƒ์Šน)
# โ€ข must_not: ๋ฐ˜๋“œ์‹œ ์ œ์™ธ
curl -X GET "http://localhost:9200/products/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool": {
      "must": [
        { "match": { "title": "๋…ธํŠธ๋ถ" } }
      ],
      "filter": [
        { "term": { "is_active": true } },
        { "range": { "price": { "gte": 1500000, "lte": 4000000 } } }
      ],
      "should": [
        { "match_phrase": { "title": "๋งฅ๋ถํ”„๋กœ" } }
      ],
      "must_not": [
        { "term": { "category": "์ค‘๊ณ " } }
      ]
    }
  },
  "sort": [
    { "price": { "order": "desc" } },
    { "_score": { "order": "desc" } }
  ],
  "from": 0,
  "size": 10,
  "highlight": {
    "pre_tags": ["<mark class=\"hl\">"],
    "post_tags": ["</mark>"],
    "fields": {
      "title": {}
    }
  }
}'
# [5] ๋Œ€์šฉ๋Ÿ‰ ๋”ฅ ํŽ˜์ด์ง• (Search After ๊ธฐ๋ฒ•: from/size 10,000๊ฑด ์ดˆ๊ณผ ์‹œ ๊ถŒ์žฅ)
# ์ฒซ ํŽ˜์ด์ง€ ํ˜ธ์ถœ (์ •๋ ฌ ๊ธฐ์ค€ ๊ฐ’ _sort ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธ)
curl -X GET "http://localhost:9200/products/_search" -H 'Content-Type: application/json' -d'
{
  "size": 10,
  "query": { "match_all": {} },
  "sort": [
    { "created_at": "desc" },
    { "id": "asc" }
  ]
}'

# ๋‹ค์Œ ํŽ˜์ด์ง€ ํ˜ธ์ถœ (์ด์ „ ํŽ˜์ด์ง€ ๋งˆ์ง€๋ง‰ ํ•ญ๋ชฉ์˜ sort ๋ฐฐ์—ด ๊ฐ’์„ search_after์— ์ „๋‹ฌ)
curl -X GET "http://localhost:9200/products/_search" -H 'Content-Type: application/json' -d'
{
  "size": 10,
  "query": { "match_all": {} },
  "search_after": [1791111600000, "PROD-004"],
  "sort": [
    { "created_at": "desc" },
    { "id": "asc" }
  ]
}'

6. ํ†ต๊ณ„ ๋ฐ ๋ฐ์ดํ„ฐ ๋ถ„์„ ์ง‘๊ณ„ (Aggregations)

# [1] ๋ฉ”ํŠธ๋ฆญ ์ง‘๊ณ„ (Metrics: ํ‰๊ท , ์ตœ์†Œ, ์ตœ๋Œ€, ํ•ฉ๊ณ„, ๊ณ ์œ ๊ฐ’ ์นด์šดํŠธ)
curl -X GET "http://localhost:9200/products/_search" -H 'Content-Type: application/json' -d'
{
  "size": 0,
  "aggs": {
    "avg_product_price": {
      "avg": { "field": "price" }
    },
    "price_stats": {
      "stats": { "field": "price" }
    },
    "unique_category_count": {
      "cardinality": { "field": "category" }
    }
  }
}'

# [2] ๋ฒ„ํ‚ท ์ง‘๊ณ„ & ์„œ๋ธŒ ์ง‘๊ณ„ (Category๋ณ„ ์ƒํ’ˆ ์ˆ˜ ๊ทธ๋ฃนํ™” + ์นดํ…Œ๊ณ ๋ฆฌ๋ณ„ ํ‰๊ท  ๊ฐ€๊ฒฉ)
curl -X GET "http://localhost:9200/products/_search" -H 'Content-Type: application/json' -d'
{
  "size": 0,
  "aggs": {
    "by_category": {
      "terms": {
        "field": "category",
        "size": 10,
        "order": { "_count": "desc" }
      },
      "aggs": {
        "avg_price": {
          "avg": { "field": "price" }
        },
        "total_stock": {
          "sum": { "field": "stock" }
        }
      }
    }
  }
}'

# [3] ์‹œ๊ณ„์—ด ๋‚ ์งœ ํžˆ์Šคํ† ๊ทธ๋žจ ์ง‘๊ณ„ (Date Histogram: ์ผ์ž๋ณ„ ๋“ฑ๋ก ์ถ”์ด)
curl -X GET "http://localhost:9200/products/_search" -H 'Content-Type: application/json' -d'
{
  "size": 0,
  "aggs": {
    "daily_trends": {
      "date_histogram": {
        "field": "created_at",
        "calendar_interval": "1d",
        "format": "yyyy-MM-dd"
      }
    }
  }
}'

7. ์‹ค๋ฌด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์—ฐ๋™ ์˜ˆ์ œ (Python & Spring Boot)

# ๐Ÿ [1] Python (elasticsearch ๊ณต์‹ ํด๋ผ์ด์–ธํŠธ)
# pip install elasticsearch
from elasticsearch import Elasticsearch

es = Elasticsearch("http://localhost:9200")

# 1. ํ•‘ ํ…Œ์ŠคํŠธ
if not es.ping():
    raise ValueError("Elasticsearch ์„œ๋ฒ„ ์—ฐ๊ฒฐ ์‹คํŒจ")

# 2. ๋„ํ๋จผํŠธ ์ƒ‰์ธ
doc = {
    "id": "PROD-010",
    "title": "์• ํ”Œ ์—์–ดํŒŸ ํ”„๋กœ 2์„ธ๋Œ€ USB-C",
    "category": "์Œํ–ฅ๊ธฐ๊ธฐ",
    "price": 359000,
    "stock": 50,
    "is_active": True
}
res = es.index(index="products", id="10", document=doc)
print(f"์ธ๋ฑ์‹ฑ ๊ฒฐ๊ณผ: {res['result']}")

# 3. ๋ณตํ•ฉ ๊ฒ€์ƒ‰ ์ฟผ๋ฆฌ ์‹คํ–‰
query = {
    "bool": {
      "must": [{"match": {"title": "์—์–ดํŒŸ"}}],
      "filter": [{"term": {"is_active": True}}]
    }
}
response = es.search(index="products", query=query, size=5)
for hit in response["hits"]["hits"]:
    source = hit["_source"]
    print(f"[{hit['_score']:.2f}] {source['title']} - {source['price']:,}์›")
// ๐Ÿƒ [2] Spring Boot (Spring Data Elasticsearch)
// build.gradle: implementation 'org.springframework.boot:spring-boot-starter-data-elasticsearch'

@Document(indexName = "products")
@Setting(replicas = 1, shards = 2)
public class ProductDocument {
    @Id
    private String id;

    @Field(type = FieldType.Text, analyzer = "korean_analyzer")
    private String title;

    @Field(type = FieldType.Keyword)
    private String category;

    @Field(type = FieldType.Long)
    private Long price;

    @Field(type = FieldType.Boolean)
    private Boolean isActive;
    
    // getters and setters...
}

public interface ProductRepository extends ElasticsearchRepository<ProductDocument, String> {
    // ๋ฉ”์„œ๋“œ ์ฟผ๋ฆฌ ์ƒ์„ฑ
    List<ProductDocument> findByCategoryAndIsActiveTrue(String category);
    
    // ๋„ค์ดํ‹ฐ๋ธŒ ๊ฒ€์ƒ‰ ์ฟผ๋ฆฌ
    @Query("{\"bool\": {\"must\": [{\"match\": {\"title\": \"?0\"}}], \"filter\": [{\"term\": {\"isActive\": true}}]}}")
    List<ProductDocument> searchByTitle(String keyword);
}

8. ์‹ค๋ฌด ์šด์˜ ํŠœ๋‹ & ํด๋Ÿฌ์Šคํ„ฐ ํŠธ๋Ÿฌ๋ธ”์ŠˆํŒ… (Performance & Ops)

โ–  JVM Heap Memory ์„ค์ • ์›์น™
1. ๋ฌผ๋ฆฌ ์‹œ์Šคํ…œ RAM์˜ 50%๋ฅผ ์ดˆ๊ณผํ•˜์ง€ ๋ง ๊ฒƒ:
   - ๋‚˜๋จธ์ง€ 50% ๋ฉ”๋ชจ๋ฆฌ๋Š” OS์˜ ํŒŒ์ผ ์‹œ์Šคํ…œ ๋ฃจ์”ฌ ์บ์‹œ(Page Cache)๋กœ ํ•„์ˆ˜ ํ™œ์šฉ๋ฉ๋‹ˆ๋‹ค.
2. ์ตœ๋Œ€ 31GB ์ดํ•˜๋กœ ์ œํ•œํ•  ๊ฒƒ:
   - 32GB ์ด์ƒ ์„ค์ • ์‹œ JVM์ด ์••์ถ• ๊ฐ์ฒด ํฌ์ธํ„ฐ(Compressed OOPs)๋ฅผ ๋น„ํ™œ์„ฑํ™”ํ•˜์—ฌ ๋ฉ”๋ชจ๋ฆฌ ๋‚ญ๋น„์™€ GC ์˜ค๋ฒ„ํ—ค๋“œ๊ฐ€ ๊ธ‰์ฆํ•ฉ๋‹ˆ๋‹ค.
   - ์˜ˆ: /etc/elasticsearch/jvm.options -> -Xms16g -Xmx16g
# [1] ๋Œ€๋Ÿ‰ ๋ฐ์ดํ„ฐ(Bulk) ์ ์žฌ ์‹œ ์†๋„ ๊ทน๋Œ€ํ™” ์„ค์ •
# ์ž„์‹œ๋กœ ๋ฆฌํ”„๋ ˆ์‹œ์™€ ๋ณต์ œ๋ณธ์„ ๋น„ํ™œ์„ฑํ™”ํ•œ ๋’ค ๋Œ€๋Ÿ‰ ์ ์žฌ -> ์™„๋ฃŒ ํ›„ ๋ณต๊ตฌ
curl -X PUT "http://localhost:9200/products/_settings" -H 'Content-Type: application/json' -d'
{
  "index": {
    "refresh_interval": "-1",
    "number_of_replicas": 0
  }
}'

# ๋Œ€๋Ÿ‰ ์ ์žฌ ์™„๋ฃŒ ํ›„ ์›๋ณต ๋ฐ ๊ฐ•์ œ ๋ณ‘ํ•ฉ(Force Merge)
curl -X PUT "http://localhost:9200/products/_settings" -H 'Content-Type: application/json' -d'
{
  "index": {
    "refresh_interval": "1s",
    "number_of_replicas": 1
  }
}'
curl -X POST "http://localhost:9200/products/_forcemerge?max_num_segments=1"
# [2] ํด๋Ÿฌ์Šคํ„ฐ ์ƒํƒœ๊ฐ€ Yellow ๋˜๋Š” Red์ผ ๋•Œ ์›์ธ ๋ถ„์„ (Allocation Explain API)
# ์ƒค๋“œ๊ฐ€ ํŠน์ • ๋…ธ๋“œ์— ํ• ๋‹น๋˜์ง€ ๋ชปํ•˜๋Š” ์ด์œ ๋ฅผ ์ •ํ™•ํžˆ ์ง„๋‹จ
curl -X GET "http://localhost:9200/_cluster/allocation/explain?pretty"

# [3] ๋””์Šคํฌ ์šฉ๋Ÿ‰ ํ•œ๋„(Disk Watermark) ์ดˆ๊ณผ๋กœ ์ฝ๊ธฐ ์ „์šฉ ๋ฝ์ด ๊ฑธ๋ ธ์„ ๋•Œ ํ•ด์ œ
# ๊ธฐ๋ณธ ์ž„๊ณ„์น˜: Low 85%, High 90%, Flood Stage 95%
curl -X PUT "http://localhost:9200/products/_settings" -H 'Content-Type: application/json' -d'
{
  "index.blocks.read_only_allow_delete": null
}
'
๐Ÿ’ฌ

์˜๊ฒฌ ๋ฐ ์งˆ๋ฌธ

0
0 / 1500