[SK shieldus Rookies 19기] 클라우드 기반 취약점 진단 및 대응 실무 6일차

기록하는짱구·2024년 5월 2일
0

SK Shieldus Rookies 19기

목록 보기
39/43
post-thumbnail

📌 엘라스틱 서치와 키바나 실행

명령 프롬프트를 관리자 권한으로 실행

엘라스틱 서치 실행

c:\Users\r2com> cd c:\ELK\elasticsearch-8.13.3\bin

c:\ELK\elasticsearch-8.13.3\bin> elasticsearch.bat

키바나 실행

c:\Users\r2com> cd c:\ELK\kibana-8.13.3\bin

c:\ELK\kibana-8.13.3\bin> kibana.bat
:
[2024-05-03T09:14:08.275+09:00][INFO ][root] Kibana is starting
		:
[2024-05-03T09:19:09.350+09:00][INFO ][status] Kibana is now available (was degraded)
[2024-05-03T09:19:10.121+09:00][INFO ][plugins.observability] Installing SLO ingest pipeline [.slo-observability.sli.pipeline-v3]
[2024-05-03T09:19:14.952+09:00][INFO ][plugins.fleet] Fleet Usage: {"agents_enabled":true,"agents":{"total_enrolled":0,"healthy":0,"unhealthy":0,"offline":0,"inactive":0,"unenrolled":0,"total_all_statuses":0,"updating":0},"fleet_server":{"total_all_statuses":0,"total_enrolled":0,"healthy":0,"unhealthy":0,"offline":0,"updating":0,"num_host_urls":0}}

키바나와 엘라스틱 서치 종료 (Ctrl + C)

📌 보안 모드로 실행

엘라스틱 서치와 키바나 패키지를 다른 폴더(C:\Temp)에 압축해제

이때, 폴더 이름에 한글이 포함되지 않도록 유의

VSCode 실행

c:\ELK\elasticsearch-8.13.3\bin> cd c:\temp\

c:\Temp> code .

엘라스틱 서치 설정 파일 확인 (C:\Temp\elasticsearch-8.13.3\config\elasticsearch.yml) → 모두 주석처리되어 있음

# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
#       Before you set out to tweak and tune the configuration, make sure you
#       understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
#cluster.name: my-application
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
#node.name: node-1
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
#path.data: /path/to/data
#
# Path to log files:
#
#path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# By default Elasticsearch is only accessible on localhost. Set a different
# address here to expose this node on the network:
#
#network.host: 192.168.0.1
#
# By default Elasticsearch listens for HTTP traffic on the first free port it
# finds starting at 9200. Set a specific HTTP port here:
#
#http.port: 9200
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when this node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
#discovery.seed_hosts: ["host1", "host2"]
#
# Bootstrap the cluster using an initial set of master-eligible nodes:
#
#cluster.initial_master_nodes: ["node-1", "node-2"]
#
# For more information, consult the discovery and cluster formation module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Allow wildcard deletion of indices:
#
#action.destructive_requires_name: false

일반 사용자로 명령 프롬프트를 켜 엘라스틱 서치 실행

c:\Temp> cd elasticsearch-8.13.3\bin

c:\Temp\elasticsearch-8.13.3\bin> chcp
활성 코드 페이지: 949

# UTF-8로 변경 (한글이 깨져서 출력되는 것을 방지)
c:\Temp\elasticsearch-8.13.3\bin> chcp 65001		
Acive code page:65001 

c:\Temp\elasticsearch-8.13.3\bin> elasticsearch.bat
		:
[2024-05-03T09:48:47,066][INFO ][o.e.c.r.a.AllocationService] [DESKTOP-304U2MR] current.health="GREEN" message="Cluster health status changed from [YELLOW] to [GREEN] (reason: [shards started [[.security-7][0]]])." previous.health="YELLOW" reason="shards started [[.security-7][0]]"

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✅ Elasticsearch security features have been automatically configured!
→ 보안 설정을 비활성화하지 않고 엘라스틱 서치를 시작하면 다음과 같은 보안 구성을 자동으로 수행

  1. TLS용 인증서와 키 생성
  1. elasticsearch.yml 파일에 TLS 구성 설정
  1. elastic 사용자에 대한 비밀번호 생성

  2. Kibana 등록 토큰 생성

✅ Authentication is enabled and cluster connections are encrypted.

❎ Password for the elastic user (reset with bin/elasticsearch-reset-password -u elastic):

*MRl67mvSwPRo5GGTJd0
→ ③ elastic 사용자에 대한 비밀번호

❎ HTTP CA certificate SHA-256 fingerprint:
5ea919548c382b4d00bd25182c284ba701bf5e1130329fd04ae7711f68a182a2

❎ Configure Kibana to use this cluster:
• Run Kibana and click the configuration link in the terminal when Kibana starts.
• Copy the following enrollment token and paste it into Kibana in your browser (valid for the next 30 minutes):

eyJ2ZXIiOiI4LjEzLjMiLCJhZHIiOlsiMTkyLjE2OC4zNy4xOjkyMDAiXSwiZmdyIjoiNWVhOTE5NTQ4YzM4MmI0ZDAwYmQyNTE4MmMyODRiYTcwMWJmNWUxMTMwMzI5ZmQwNGFlNzcxMWY2OGExODJhMiIsImtleSI6Ijg2NGRUbzhCeE1xXzRuTC02NUVWOnBoNTBUUkJVUTNLbkpuMTlSbzJGV0EifQ==
→ ④ Kibana 등록 토큰

❎ Configure other nodes to join this cluster:
• On this node:
⁃ Create an enrollment token with bin/elasticsearch-create-enrollment-token -s node.
⁃ Uncomment the transport.host setting at the end of config/elasticsearch.yml.
⁃ Restart Elasticsearch.
• On other nodes:
⁃ Start Elasticsearch with bin/elasticsearch --enrollment-token <token>, using the enrollment token that you generated.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

엘라스틱 서치 동작 확인

관리자 모드로 명령 프롬프트를 켜 키바나 실행

C:\ELK\kibana-8.13.3\bin> cd c:\temp\kibana-8.13.3\bin

c:\Temp\kibana-8.13.3\bin> kibana.bat
		:
[2024-05-03T09:56:50.283+09:00][INFO ][root] Kibana is starting
[2024-05-03T09:56:50.361+09:00][INFO ][node] Kibana process configured with roles: [background_tasks, ui]
[2024-05-03T09:59:03.507+09:00][INFO ][plugins-service] The following plugins are disabled: "cloudChat,cloudExperiments,cloudFullStory,profilingDataAccess,profiling,securitySolutionServerless,serverless,serverlessObservability,serverlessSearch".
[2024-05-03T09:59:03.610+09:00][INFO ][http.server.Preboot] http server running at http://localhost:5601
[2024-05-03T09:59:03.896+09:00][INFO ][plugins-system.preboot] Setting up [1] plugins: [interactiveSetup]
[2024-05-03T09:59:04.250+09:00][INFO ][preboot] "interactiveSetup" plugin is holding setup: Validating Elasticsearch connection configuration…
[2024-05-03T09:59:04.288+09:00][INFO ][root] Holding setup until preboot stage is completed.


\u001b[97m\u001b[1m\u001b[96mi\u001b[39m\u001b[97m Kibana has not been configured.\u001b[22m\u001b[39m

Go to \u001b[96m\u001b[4mhttp://localhost:5601/?code=583491\u001b[24m\u001b[39m to get started.

키바나 웹 콘솔에 접속

키바나 설정 파일(C:\Temp\kibana-8.13.3\config\kibana.yml)에 설정이 자동으로 추가된 것을 확인 가능

엘라스틱 서치 동작을 CURL 명령으로 확인

# HTTP로 접근 불가
C:\Users\r2com> curl -XGET http://localhost:9200
curl: (52) Empty reply from server

C:\Users\r2com> curl -XGET https://localhost:9200
curl: (60) schannel: SEC_E_UNTRUSTED_ROOT (0x80090325) - 신뢰되지 않은 기관에서 인증서 체인을 발급했습니다.
More details here: https://curl.se/docs/sslcerts.html

curl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it. To learn more about this situation and
how to fix it, please visit the web page mentioned above.

# Allow insecure server connections
C:\Users\r2com> curl -XGET https://localhost:9200 -k
{"error":{"root_cause":[{"type":"security_exception","reason":"missing authentication credentials for REST request [/]","header":{"WWW-Authenticate":["Basic realm=\"security\" charset=\"UTF-8\"","Bearer realm=\"security\"","ApiKey"]}}],"type":"security_exception","reason":"missing authentication credentials for REST request [/]","header":{"WWW-Authenticate":["Basic realm=\"security\" charset=\"UTF-8\"","Bearer realm=\"security\"","ApiKey"]}},"status":401}

# 읽기 좋은 형태로 출력
C:\Users\r2com> curl -XGET https://localhost:9200?pretty -k
{
  "error" : {
    "root_cause" : [
      {
        "type" : "security_exception",
        "reason" : "missing authentication credentials for REST request [/?pretty]",
        "header" : {
          "WWW-Authenticate" : [
            "Basic realm=\"security\" charset=\"UTF-8\"",
            "Bearer realm=\"security\"",
            "ApiKey"
          ]
        }
      }
    ],
    "type" : "security_exception",
    "reason" : "missing authentication credentials for REST request [/?pretty]",
    "header" : {
      "WWW-Authenticate" : [
        "Basic realm=\"security\" charset=\"UTF-8\"",
        "Bearer realm=\"security\"",
        "ApiKey"
      ]
    }
  },
  "status" : 401
}
                                                        
C:\Users\r2com> curl -XGET https://localhost:9200 -k -u elastic
Enter host password for user 'elastic':
{
  "name" : "DESKTOP-304U2MR",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "4Bp2QTYXQdKASf40yxFfkQ",
  "version" : {
    "number" : "8.13.3",
    "build_flavor" : "default",
    "build_type" : "zip",
    "build_hash" : "617f7b76c4ebcb5a7f1e70d409a99c437c896aea",
    "build_date" : "2024-04-29T22:05:16.051731935Z",
    "build_snapshot" : false,
    "lucene_version" : "9.10.0",
    "minimum_wire_compatibility_version" : "7.17.0",
    "minimum_index_compatibility_version" : "7.0.0"
  },
  "tagline" : "You Know, for Search"
}

📌 실습은 비보안 환경에서 진행

보안 모드(C:\Temp)로 실행한 엘라스틱 서치와 키바나 종료(Ctl+C)

비보안 모드(C:\ELK)로 엘라스틱 서치와 키바나 실행

(일반 모드로 명령 프롬프트를 실행) 엘라스틱 서치 실행

c:\Temp\elasticsearch-8.13.3\bin> cd c:\ELK\elasticsearch-8.13.3\bin

c:\ELK\elasticsearch-8.13.3\bin> elasticsearch.bat

(관리자 모드로 명령 프롬프트를 실행) 키바나 실행

c:\Temp\kibana-8.13.3\bin> cd c:\ELK\kibana-8.13.3\bin

c:\ELK\kibana-8.13.3\bin> kibana.bat

📌 ELK 스택

Elasticsearch
→ 검색(색인) 엔진
→ 비정형화 된 데이터(문서, document)를 집어넣으면 빠르게 검색할 수 있는 형태로 데이터를 소유

Kibana
→ Elasticsearch가 가지고 있는 데이터를 쓰기 좋게 만들어주는 역할

💡 Elasticsearch와 Kibana
사용자들이 kibana를 통해 Elasticsearch에 인덱스되어 있는 문서들을 쉽게 조회할 수 있도록 제공
즉, Elasticsearch는 데이터베이스의 역할을 하고 kibana는 장고의 서버와 같은 역할 수행

Logstash
→ 데이터를 모아 원하는 형태로 가공해서 Elasticsearch 같은 곳에 쌓아두는 역할(ETL)

Beats
→ 다양한 종류의 머신, 플랫폼 등의 환경에서 Logstash가 데이터를 쉽게 수집할 수 있도록 도와주는 역할

💡 ELK(Elasticsearch, Logstash, Kibana)
분산된 클라우드 환경 속 개별 서버들의 데이터를 한곳으로 모아 수집하여 원하는 형태의 데이터로 가공해 대시보드로 만드는 것
→ 수집된 데이터를 모아 분석하는 것은 CloudWatch에서도 가능하지만 더 고도화된 분석과 시각화를 행할 수 있는 도구가 바로 ELK

📌 도서 데이터에 포함될 내용

title 			: 도서명
author			: 작가명
release_date	: 출판일
amazon_rating	: 아마존 평점
best_seller		: 베스트 셀러 여부 
prices			: 달러, 파운드, 유로 화폐 단위의 가격

📌 JSON 문서 형태로 책 표현

{
	# 키 : 값 형태로 데이터를 표현
	"title": "Effective Java", 		
    
    # 키는 문자열 형태
	"author": "Josuha Bloch", 	
    
    # 값은 문자열, 숫자, 참(true), 거짓(false), 또 다른 객체, 배열 등
      다양한 형태가 올 수 있음
	"release_date": "2001-06-01", 	
    
	"amazon_rating": 4.7, 		   
	"best_seller": true, 
	"prices": {
		"usd": 9.95, 
		"gbp": 7.95, 
		"eur": 8.95
	}
}

📌 문서 API

문서(document)의 생성(POST), 삭제(DELETE), 수정(PUT), 검색(GET)을 도와주는 API
API는 RESTful 형식으로 HTTP 전송을 통해서 액세스할 수 있음

https://velog.io/@younge/REST-API-%EC%84%B1%EC%88%99%EB%8F%84-%EB%AA%A8%EB%8D%B8-Maturity-Model-eqqyjqff

<HTTP_METHOD> <SERVER:PORT>/<INDEX_NAME>/_doc/<DOC_ID>
{
   요청 본문
}


HTTP_METHOD - GET, POST, PUT, DELETE 등 
SERVER:PORT - localhost:9200
INDEX_NAME  - 인덱스 이름 = 색인 명
_doc        - 문서 API의 엔드포인트 기본값

예) books 문서를 ID 1으로 인덱싱
PUT books/_doc/1
{
	"title": "Effective Java", 		
	"author": "Josuha Bloch", 		
	"release_date": "2001-06-01", 	
	"amazon_rating": 4.7, 		
	"best_seller": true, 
	"prices": {
		"usd": 9.95, 
		"gbp": 7.95, 
		"eur": 8.95
	}
}

cURL을 이용해 문서 인덱싱

c:\Users\r2com> curl -XPUT "localhost:9200/books/_doc/1" -H "Content-Type: application/json" -d "{ ... }"
                     ~~~~~  ~~~~~~~~~~~~~~ ~~~~~      ~  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~
                      |      |              |          |  |                                    |
                      |      |              |          |  |                                    +-- 요청 본문 = 데이터
                      |      |              |          |  +-- Content-Type 요청 헤더를 설정 → 본문 내용이 JSON 타입
                      |      |              |          +-- book ID
                      |      |              +-- 인덱스 이름
                      |      +-- 서버 호스트 이름과 포트 번호  
                      +-- 요청 방식을 지정

💡 요청 본문을 기술할 때 쌍따옴표를 이스케이프 처리해야 함
→ 쌍따옴표를 두번 연속 표기 또는 \" 형식으로 이스케이프

카바나 Dev Tools 콘솔을 이용해서 문서 인덱싱

문서 인덱싱(색인)
→ 사용자가 제공하는 데이터를 분석해 검색하기 용이하게 만들어 저장하고 Elasticsearch가 빠르게 검색할 수 있도록 등록하는 과정

💡 인덱스는 테이블의 역할을 한다고 생각하면 이해하기 편함

여러 문서를 인덱싱(색인)

PUT books/_doc/2
{
  "title":"Core Java Volume I - Fundamentals",
  "author":"Cay S. Horstmann",
  "release_date":"2018-08-27",
  "amazon_rating":4.8,
  "best_seller":true,
  "prices": {
    "usd":19.95,
    "gbp":17.95,
    "eur":18.95
  }
}
PUT books/_doc/4
{
  "title":"Java: A Beginner’s Guide",
  "author":"Herbert Schildt",
  "release_date":"2018-11-20",
  "amazon_rating":4.2,
  "best_seller":true,
  "prices": {
    "usd":19.99,
    "gbp":19.99,
    "eur":19.99
  }
}

데이터 검색

_count API - 해당 인덱스에 속하는 문서의 수 반환

c:\Users\r2com> curl -XGET "localhost:9200/books/_count"
{"count":3,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0}}

c:\Users\r2com> curl -XGET "localhost:9200/books/_count"?pretty
{
  "count" : 3,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  }
}

여러 인덱스의 문서 개수 동시에 조회

c:\Users\r2com> curl -XGET "localhost:9200/books,books2/_count"?pretty
{                                          ~~~~~~~~~~~~ 
  "error" : {                              동일한 API를 사용해 여러 인덱스에서
  										   동시에 가져오는 것도 가능   
    "root_cause" : [
      {
        # books2 인덱스가 존재하지 않기 때문에 오류 반환
        "type" : "index_not_found_exception",	
        
        "reason" : "no such index [books2]",
        "resource.type" : "index_or_alias",
        "resource.id" : "books2",
        "index_uuid" : "_na_",
        "index" : "books2"
      }
    ],
    "type" : "index_not_found_exception",
    "reason" : "no such index [books2]",
    "resource.type" : "index_or_alias",
    "resource.id" : "books2",
    "index_uuid" : "_na_",
    "index" : "books2"
  },
  "status" : 404
}

모든 인덱스의 문서 수를 가져올 경우

c:\Users\r2com> curl -XGET "localhost:9200/_count"?pretty
{
  # 시스템 및 숨겨진 인덱스를 포함하여 사용 가능한 모든 문서가 반환
  "count" : 3,				
  
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  }
}

문서 검색

단일 문서 검색

ID가 부연된 문서를 가져오기
GET <index>/_doc/<id>

c:\Users\r2com> curl -XGET http://localhost:9200/books/_doc/1
{"_index":"books","_id":"1","_version":1,"_seq_no":0,"_primary_term":1,"found":true,"_source":{ "title": "Effective Java", "author": "Josuha Bloch", "release_date": "2001-06-01", "amazon_rating": 4.7, "best_seller": true, "prices": { "usd": 9.95, "gbp": 7.95, "eur": 8.95 } }}

c:\Users\r2com> curl -XGET http://localhost:9200/books/_doc/1?pretty
{
  "_index" : "books",			--+ 메타 데이터 (내부적으로 관리하는 정보)
  "_id" : "1",				  	  |
  "_version" : 1,			  	  |
  "_seq_no" : 0,			  	  |
  "_primary_term" : 1,			--+
  "found" : true,
  "_source" : {					--+ 원본 문서 (소스 데이터)
    "title" : "Effective Java",	  | 
    "author" : "Josuha Bloch",	  |
    "release_date" : "2001-06-01",|
    "amazon_rating" : 4.7,	  	  |
    "best_seller" : true,		  |	
    "prices" : {				  |
      "usd" : 9.95,				  |
      "gbp" : 7.95,			  	  |
      "eur" : 8.95			  	  |
    }							--+
  }
}

No request selected 오류가 발생하는 경우

_search API - 일련의 ID를 부여해서 여러 문서 가져오기

query DSL(도메인 특화 언어) 문법 사용

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "books",
        "_id": "3",
        "_score": 1,
        "_source": {
          "title": "Effective Java",
          "author": "Josuha Bloch",
          "release_date": "2001-06-01",
          "amazon_rating": 4.7,
          "best_seller": true,
          "prices": {
            "usd": 9.95,
            "gbp": 7.95,
            "eur": 8.95
          }
        }
      },
      {
        "_index": "books",
        "_id": "2",
        "_score": 1,
        "_source": {
          "title": "Core Java Volume I - Fundamentals",
          "author": "Cay S. Horstmann",
          "release_date": "2018-08-27",
          "amazon_rating": 4.8,
          "best_seller": true,
          "prices": {
            "usd": 19.95,
            "gbp": 17.95,
            "eur": 18.95
          }
        }
      },
      {
        "_index": "books",
        "_id": "1",
        "_score": 1,
        "_source": {
          "title": "Effective Java",
          "author": "Josuha Bloch",
          "release_date": "2001-06-01",
          "amazon_rating": 4.7,
          "best_seller": true,
          "prices": {
            "usd": 9.95,
            "gbp": 7.95,
            "eur": 8.95
          }
        }
      }
    ]
  }
}




C:\Users\r2com> curl -XGET http://localhost:9200/books/_search -H "Content-Type: application/json" -d "{ ""query"": { ""ids"" : { ""values"": [1, 2, 3] } } }"
{"took":1,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":3,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"books","_id":"3","_score":1.0,"_source":{
        "title": "Effective Java",
        "author": "Josuha Bloch",
        "release_date": "2001-06-01",
        "amazon_rating": 4.7,
        "best_seller": true,
        "prices": {
                "usd": 9.95,
                "gbp": 7.95,
                "eur": 8.95
        }
}
},{"_index":"books","_id":"2","_score":1.0,"_source":{
  "title":"Core Java Volume I - Fundamentals",
  "author":"Cay S. Horstmann",
  "release_date":"2018-08-27",
  "amazon_rating":4.8,
  "best_seller":true,
  "prices": {
    "usd":19.95,
    "gbp":17.95,
    "eur":18.95
  }
}
},{"_index":"books","_id":"1","_score":1.0,"_source":{
        "title": "Effective Java",
        "author": "Josuha Bloch",
        "release_date": "2001-06-01",
        "amazon_rating": 4.7,
        "best_seller": true,
        "prices": {
                "usd": 9.95,
                "gbp": 7.95,
                "eur": 8.95
        }
}
}]}}


C:\Users\r2com> curl -XGET http://localhost:9200/books/_search?pretty -H "Content-Type: application/json" -d "{ ""query"": { ""ids"" : { ""values"": [1, 2, 3] } } }"
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "books",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "title" : "Effective Java",
          "author" : "Josuha Bloch",
          "release_date" : "2001-06-01",
          "amazon_rating" : 4.7,
          "best_seller" : true,
          "prices" : {
            "usd" : 9.95,
            "gbp" : 7.95,
            "eur" : 8.95
          }
        }
      },
      {
        "_index" : "books",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "title" : "Core Java Volume I - Fundamentals",
          "author" : "Cay S. Horstmann",
          "release_date" : "2018-08-27",
          "amazon_rating" : 4.8,
          "best_seller" : true,
          "prices" : {
            "usd" : 19.95,
            "gbp" : 17.95,
            "eur" : 18.95
          }
        }
      },
      {
        "_index" : "books",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "title" : "Effective Java",
          "author" : "Josuha Bloch",
          "release_date" : "2001-06-01",
          "amazon_rating" : 4.7,
          "best_seller" : true,
          "prices" : {
            "usd" : 9.95,
            "gbp" : 7.95,
            "eur" : 8.95
          }
        }
      }
    ]
  }
}

조회 결과에서 소스 데이터 생략

GET /books/_search
{
  "_source": false,
  "query": {
    "ids": {
      "values": [1, 2, 3]
    }
  }
}

모든 문서 가져오기

전체 텍스트 검색

match 쿼리

Josuha 저자 이름으로 검색

# match 쿼리는 단어의 위치 및 대소문자 관계 없이 일치하는 단어를 포함하면 결과 반환
"author": "Josuha"
"author": "josuha"
"author": "JOSUHA"
"author": "Bloch"

# 완벽한 단어 중에서 일치하는 단어가 없으므로 검색되지 않음 
"author": "jos"

단어의 시작 문자 일부로 조회하려는 경우

해당 문자로 시작하는 단어 검색

GET /books/_search
{
  "query": {
    "prefix": {
    
      # 단어에서 시작하는 문자를 소문자로 표기
      "author": "jos"	
      
    }
  }
}

저자 이름이 "Josuha Schildt"인 도서 조회

일치하는 도서가 없을 것을 예상

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": 1.2613049,
    "hits": [
      {
        "_index": "books",
        "_id": "4",
        "_score": 1.2613049,
        "_source": {
          "title": "Java: A Beginner’s Guide",
          
          # 기본적으로 OR 연산을 하므로, 
            저자 이름에 Josuha가 들어간 것과 Schildt가 들어간 것을 모두 조회
          "author": "Herbert Schildt",		
          
          "release_date": "2018-11-20",		   
          "amazon_rating": 4.2,
          "best_seller": true,
          "prices": {
            "usd": 19.99,
            "gbp": 19.99,
            "eur": 19.99
          }
        }
      },
      {
        "_index": "books",
        "_id": "3",
        "_score": 0.72615415,
        "_source": {
          "title": "Effective Java",
          "author": "Josuha Bloch",
          "release_date": "2001-06-01",
          "amazon_rating": 4.7,
          "best_seller": true,
          "prices": {
            "usd": 9.95,
            "gbp": 7.95,
            "eur": 8.95
          }
        }
      },
      {
        "_index": "books",
        "_id": "1",
        "_score": 0.72615415,
        "_source": {
          "title": "Effective Java",
          "author": "Josuha Bloch",
          "release_date": "2001-06-01",
          "amazon_rating": 4.7,
          "best_seller": true,
          "prices": {
            "usd": 9.95,
            "gbp": 7.95,
            "eur": 8.95
          }
        }
      }
    ]
  }
}

operator 매개변수를 이용해 명시적으로 AND로 설정

책 제목이 Effective Java 글자가 모두 들어 있는 문서 조회

📌 _bulk API를 이용한 대량 문서 색인

https://github.com/madhusudhankonda/elasticsearch-in-action/blob/main/datasets/books-kibana-dataset.txt

POST _bulk
{"index":{"_index":"books","_id":"1"}}
 ~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 |       메타 데이터
 +- 실행하려고 하는 작업(인덱싱, 삭제, 업데이트 등)
{"title": "Core Java Volume I – Fundamentals", ..., "tags": ["Programming Languages, Java Programming"]}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 실제 문서

색인된 문서 갯수 확인

→ 10개

id가 1인 문서 조회

같은 ID를 사용하는 문서는 업데이트된 것을 확인
→ 1~4번 문서는 업데이트됨

문서 구조 확인

{
  "title": "Head First Java",
  "author": "Kathy Sierra and Bert Bates",
  "edition": 2,
  "synopsis": "The most important selling points of Head First Java is its simplicity and super-effective real-life analogies that pertain to the Java programming concepts.",
  "amazon_rating": 4.3,
  "release_date": "2005-02-18",
  "tags": [
    "IT Certification Exams",
    "Object-Oriented Software Design",
    "Design Pattern Programming"
  ]
}

여러 필드에서 검색

multi_match 쿼리를 사용

title 또는 synopsis 항목에 Java 단어가 포함된 문서 조회

{
  "took": 66,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 10,
      "relation": "eq"
    },
    "max_score": 0.33537668,
    "hits": [
      {
        "_index": "books",
        "_id": "2",
        "_score": 0.33537668,
        "_source": {
          "title": "Effective Java",
          "author": "Joshua Bloch",
          "edition": 3,
          "synopsis": "A must-have book for every Java programmer and Java aspirant, Effective Java makes up for an excellent complementary read with other Java books or learning material. The book offers 78 best practices to follow for making the code better.",
          "amazon_rating": 4.7,
          "release_date": "2017-12-27",
          "tags": [
            "Object Oriented Software Design"
          ]
        }
      },
      {
        "_index": "books",
        "_id": "5",
        "_score": 0.30060259,
        "_source": {
          "title": "Head First Java",
          "author": "Kathy Sierra and Bert Bates",
          "edition": 2,
          "synopsis": "The most important selling points of Head First Java is its simplicity and super-effective real-life analogies that pertain to the Java programming concepts.",
          "amazon_rating": 4.3,
          "release_date": "2005-02-18",
          "tags": [
            "IT Certification Exams",
            "Object-Oriented Software Design",
            "Design Pattern Programming"
          ]
        }
      },
      {
        "_index": "books",
        "_id": "3",
        "_score": 0.27236223,
        "_source": {
          "title": "Java: A Beginner’s Guide",
          "author": "Herbert Schildt",
          "edition": 8,
          "synopsis": "One of the most comprehensive books for learning Java. The book offers several hands-on exercises as well as a quiz section at the end of every chapter to let the readers self-evaluate their learning.",
          "amazon_rating": 4.2,
          "release_date": "2018-11-20",
          "tags": [
            "Software Design & Engineering",
            "Internet & Web"
          ]
        }
      },
      {
        "_index": "books",
        "_id": "4",
        "_score": 0.27236223,
        "_source": {
          "title": "Java - The Complete Reference",
          "author": "Herbert Schildt",
          "edition": 11,
          "synopsis": "Convenient Java reference book examining essential portions of the Java API library, Java. The book is full of discussions and apt examples to better Java learning.",
          "amazon_rating": 4.4,
          "release_date": "2019-03-19",
          "tags": [
            "Software Design & Engineering",
            "Internet & Web",
            "Computer Programming Language & Tool"
          ]
        }
      },
      {
        "_index": "books",
        "_id": "6",
        "_score": 0.27236223,
        "_source": {
          "title": "Java Concurrency in Practice",
          "author": "Brian Goetz with Tim Peierls, Joshua Bloch, Joseph Bowbeer, David Holmes, and Doug Lea",
          "edition": 1,
          "synopsis": "Java Concurrency in Practice is one of the best Java programming books to develop a rich understanding of concurrency and multithreading.",
          "amazon_rating": 4.3,
          "release_date": "2006-05-09",
          "tags": [
            "Computer Science Books",
            "Programming Languages",
            "Java Programming"
          ]
        }
      },
      {
        "_index": "books",
        "_id": "1",
        "_score": 0.24897236,
        "_source": {
          "title": "Core Java Volume I – Fundamentals",
          "author": "Cay S. Horstmann",
          "edition": 11,
          "synopsis": "Java reference book that offers a detailed explanation of various features of Core Java, including exception handling, interfaces, and lambda expressions. Significant highlights of the book include simple language, conciseness, and detailed examples.",
          "amazon_rating": 4.6,
          "release_date": "2018-08-27",
          "tags": [
            "Programming Languages, Java Programming"
          ]
        }
      },
      {
        "_index": "books",
        "_id": "9",
        "_score": 0.24897236,
        "_ignored": [
          "synopsis.keyword"
        ],
        "_source": {
          "title": "Java Performance: The Definite Guide",
          "author": "Scott Oaks",
          "edition": 1,
          "synopsis": "Garbage collection, JVM, and performance tuning are some of the most favorable aspects of the Java programming language. It educates readers about maximizing Java threading and synchronization performance features, improve Java-driven database application performance, tackle performance issues",
          "amazon_rating": 4.1,
          "release_date": "2014-03-04",
          "tags": [
            "Design Pattern Programming",
            "Object-Oriented Software Design",
            "Computer Programming Language & Tool"
          ]
        }
      },
      {
        "_index": "books",
        "_id": "7",
        "_score": 0.18531466,
        "_source": {
          "title": "Test-Driven: TDD and Acceptance TDD for Java Developers",
          "author": "Lasse Koskela",
          "edition": 1,
          "synopsis": "Test-Driven is an excellent book for learning how to write unique automation testing programs. It is a must-have book for those Java developers that prioritize code quality as well as have a knack for writing unit, integration, and automation tests.",
          "amazon_rating": 4.1,
          "release_date": "2007-10-22",
          "tags": [
            "Software Architecture",
            "Software Design & Engineering",
            "Java Programming"
          ]
        }
      },
      {
        "_index": "books",
        "_id": "10",
        "_score": 0.054473724,
        "_source": {
          "title": "Head First Design Patterns",
          "author": "Eric Freeman & Elisabeth Robson with Kathy Sierra & Bert Bates",
          "edition": 10,
          "synopsis": "Head First Design Patterns is one of the leading books to build that particular understanding of the Java programming language.",
          "amazon_rating": 4.5,
          "release_date": "2014-03-04",
          "tags": [
            "Design Pattern Programming",
            "Object-Oriented Software Design eTextbooks",
            "Web Development & Design eTextbooks"
          ]
        }
      },
      {
        "_index": "books",
        "_id": "8",
        "_score": 0.047203016,
        "_source": {
          "title": "Head First Object-Oriented Analysis Design",
          "author": "Brett D. McLaughlin, Gary Pollice & David West",
          "edition": 1,
          "synopsis": "Head First is one of the most beautiful finest book series ever written on Java programming language. Another gem in the series is the Head First Object-Oriented Analysis Design.",
          "amazon_rating": 3.9,
          "release_date": "2014-04-29",
          "tags": [
            "Introductory & Beginning Programming",
            "Object-Oriented Software Design",
            "Java Programming"
          ]
        }
      }
    ]
  }
}

제목에 더 높은 우선순위(관련성) 부여

→ 결과 부스팅

{
  "took": 26,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 10,
      "relation": "eq"
    },
    "max_score": 1.0061301,
    "hits": [
      {
        "_index": "books",
        "_id": "2",
        "_score": 1.0061301,
        "_source": {
          "title": "Effective Java",
          "author": "Joshua Bloch",
          "edition": 3,
          "synopsis": "A must-have book for every Java programmer and Java aspirant, Effective Java makes up for an excellent complementary read with other Java books or learning material. The book offers 78 best practices to follow for making the code better.",
          "amazon_rating": 4.7,
          "release_date": "2017-12-27",
          "tags": [
            "Object Oriented Software Design"
          ]
        }
      },
      {
        "_index": "books",
        "_id": "5",
        "_score": 0.90180784,
        "_source": {
          "title": "Head First Java",
          "author": "Kathy Sierra and Bert Bates",
          "edition": 2,
          "synopsis": "The most important selling points of Head First Java is its simplicity and super-effective real-life analogies that pertain to the Java programming concepts.",
          "amazon_rating": 4.3,
          "release_date": "2005-02-18",
          "tags": [
            "IT Certification Exams",
            "Object-Oriented Software Design",
            "Design Pattern Programming"
          ]
        }
      },
      {
        "_index": "books",
        "_id": "3",
        "_score": 0.81708676,
        "_source": {
          "title": "Java: A Beginner’s Guide",
          "author": "Herbert Schildt",
          "edition": 8,
          "synopsis": "One of the most comprehensive books for learning Java. The book offers several hands-on exercises as well as a quiz section at the end of every chapter to let the readers self-evaluate their learning.",
          "amazon_rating": 4.2,
          "release_date": "2018-11-20",
          "tags": [
            "Software Design & Engineering",
            "Internet & Web"
          ]
        }
      },
      {
        "_index": "books",
        "_id": "4",
        "_score": 0.81708676,
        "_source": {
          "title": "Java - The Complete Reference",
          "author": "Herbert Schildt",
          "edition": 11,
          "synopsis": "Convenient Java reference book examining essential portions of the Java API library, Java. The book is full of discussions and apt examples to better Java learning.",
          "amazon_rating": 4.4,
          "release_date": "2019-03-19",
          "tags": [
            "Software Design & Engineering",
            "Internet & Web",
            "Computer Programming Language & Tool"
          ]
        }
      },
      {
        "_index": "books",
        "_id": "6",
        "_score": 0.81708676,
        "_source": {
          "title": "Java Concurrency in Practice",
          "author": "Brian Goetz with Tim Peierls, Joshua Bloch, Joseph Bowbeer, David Holmes, and Doug Lea",
          "edition": 1,
          "synopsis": "Java Concurrency in Practice is one of the best Java programming books to develop a rich understanding of concurrency and multithreading.",
          "amazon_rating": 4.3,
          "release_date": "2006-05-09",
          "tags": [
            "Computer Science Books",
            "Programming Languages",
            "Java Programming"
          ]
        }
      },
      {
        "_index": "books",
        "_id": "1",
        "_score": 0.7469171,
        "_source": {
          "title": "Core Java Volume I – Fundamentals",
          "author": "Cay S. Horstmann",
          "edition": 11,
          "synopsis": "Java reference book that offers a detailed explanation of various features of Core Java, including exception handling, interfaces, and lambda expressions. Significant highlights of the book include simple language, conciseness, and detailed examples.",
          "amazon_rating": 4.6,
          "release_date": "2018-08-27",
          "tags": [
            "Programming Languages, Java Programming"
          ]
        }
      },
      {
        "_index": "books",
        "_id": "9",
        "_score": 0.7469171,
        "_ignored": [
          "synopsis.keyword"
        ],
        "_source": {
          "title": "Java Performance: The Definite Guide",
          "author": "Scott Oaks",
          "edition": 1,
          "synopsis": "Garbage collection, JVM, and performance tuning are some of the most favorable aspects of the Java programming language. It educates readers about maximizing Java threading and synchronization performance features, improve Java-driven database application performance, tackle performance issues",
          "amazon_rating": 4.1,
          "release_date": "2014-03-04",
          "tags": [
            "Design Pattern Programming",
            "Object-Oriented Software Design",
            "Computer Programming Language & Tool"
          ]
        }
      },
      {
        "_index": "books",
        "_id": "7",
        "_score": 0.55594397,
        "_source": {
          "title": "Test-Driven: TDD and Acceptance TDD for Java Developers",
          "author": "Lasse Koskela",
          "edition": 1,
          "synopsis": "Test-Driven is an excellent book for learning how to write unique automation testing programs. It is a must-have book for those Java developers that prioritize code quality as well as have a knack for writing unit, integration, and automation tests.",
          "amazon_rating": 4.1,
          "release_date": "2007-10-22",
          "tags": [
            "Software Architecture",
            "Software Design & Engineering",
            "Java Programming"
          ]
        }
      },
      {
        "_index": "books",
        "_id": "10",
        "_score": 0.054473724,
        "_source": {
          "title": "Head First Design Patterns",
          "author": "Eric Freeman & Elisabeth Robson with Kathy Sierra & Bert Bates",
          "edition": 10,
          "synopsis": "Head First Design Patterns is one of the leading books to build that particular understanding of the Java programming language.",
          "amazon_rating": 4.5,
          "release_date": "2014-03-04",
          "tags": [
            "Design Pattern Programming",
            "Object-Oriented Software Design eTextbooks",
            "Web Development & Design eTextbooks"
          ]
        }
      },
      {
        "_index": "books",
        "_id": "8",
        "_score": 0.047203016,
        "_source": {
          "title": "Head First Object-Oriented Analysis Design",
          "author": "Brett D. McLaughlin, Gary Pollice & David West",
          "edition": 1,
          "synopsis": "Head First is one of the most beautiful finest book series ever written on Java programming language. Another gem in the series is the Head First Object-Oriented Analysis Design.",
          "amazon_rating": 3.9,
          "release_date": "2014-04-29",
          "tags": [
            "Introductory & Beginning Programming",
            "Object-Oriented Software Design",
            "Java Programming"
          ]
        }
      }
    ]
  }
}

문장 검색

시놉시스에 "must-have book for every Java programmer" 문장이 들어간 문서 검색

match 쿼리에 operator를 사용해서 검색

match_phrase를 사용해서 검색

일치하는 부분을 하이라이팅 처리

{
  "took": 159,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 7.300332,
    "hits": [
      {
        "_index": "books",
        "_id": "2",
        "_score": 7.300332,
        "_source": {
          "title": "Effective Java",
          "author": "Joshua Bloch",
          "edition": 3,
          "synopsis": "A must-have book for every Java programmer and Java aspirant, Effective Java makes up for an excellent complementary read with other Java books or learning material. The book offers 78 best practices to follow for making the code better.",
          "amazon_rating": 4.7,
          "release_date": "2017-12-27",
          "tags": [
            "Object Oriented Software Design"
          ]
        },
        "highlight": {
          "synopsis": [
            "A <em>must-have book for every Java programmer</em> and Java aspirant, Effective Java makes up for an excellent"
          ]
        }
      }
    ]
  }
}

누락된 단어가 있는 문장 검색

시놉시스에 for every가 누락된 "must-have book Java programmer" 문장으로 검색

slop 매개변수 추가

→ 검색할 때 구문에 누락 또는 정렬되지 않은 단어 수를 나타내는 정수

match 쿼리에 operator를 사용하는 방법

match 쿼리 실행 시 맞춤법 오류가 있는 경우

tags 항목에 Computer 단어가 들어간 문서 조회

사용자 실수로 Computer라는 단어를 Komputer로 입력한 경우

→ 일치하는 결과 존재 X

fuzziness 매개변수를 이용해 글자가 잘못 입력된 경우(틀리거나, 생략되거나, 추가된 경우)에도 검색 허용

📌 용어 수준 쿼리(term-level query)

구조화된 데이터 쿼리를 지원하기 위한 쿼리 유형

용어 수준 쿼리는 일치 여부에 집중하고, 관련성 점수(score)를 생성하지 않음

구조화되지 않은 데이터(Ex. 문자열)는 분석되어 저장
구조화된 데이터(Ex. 숫자, 날짜, 범위, IP 주소 등)는 분석되지 않고 그대로 저장

문서 구조 확인

{
  "title": "Head First Java",
  "author": "Kathy Sierra and Bert Bates",
  "edition": 2,
  "synopsis": "The most important selling points of Head First Java is its simplicity and super-effective real-life analogies that pertain to the Java programming concepts.",
  "amazon_rating": 4.3,
  "release_date": "2005-02-18",
  "tags": [
    "IT Certification Exams",
    "Object-Oriented Software Design",
    "Design Pattern Programming"
  ]
}

edition에 3이 포함된 문서 검색

→ match 쿼리 이용

검색 결과의 _source 부분을 title과 edition 정보만 출력되도록 수정

term 쿼리 이용

range 쿼리

amazon_rating이 4.5 이상, 5.0 이하인 도서 문서 조회

📌 복합 쿼리

부울(bool)
상수 점수(constant_score)
기능 점수(function_score)
부스팅(boosting)
분리 최대(dis_max)

부울(bool) 쿼리

GET books/_search
{
  "query": {
    "bool": {
    
      # 문서와 일치 
      "must": [ {    } ], 	
      
      # 문서와 일치하지 않음
      "must_not": [ {    } ],
      
      # 반드시 일치해야 하는 것은 아니나, 일치하는 경우 높은 관련성 점수가 부여 
      "should": [ {    } ], 
      
      # must 절과 유사하게 문서와 일치 
      "filter": [ {    } ]		
    }
  }
}

📌 참고

https://esbook.kimjmin.net/04-data/4.1-rest-api

https://esbook.kimjmin.net/04-data/4.2-crud

https://esbook.kimjmin.net/04-data/4.3-_bulk

https://esbook.kimjmin.net/04-data/4.4-_search

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html

0개의 댓글