GW LABS

기초부터 다지는 ElasticSearch 운영 노하우 (2) - ElasticSearch 기본 동작 본문

Book-Review/Programing

기초부터 다지는 ElasticSearch 운영 노하우 (2) - ElasticSearch 기본 동작

GeonWoo Kim 2022. 8. 23. 13:27

2.1 문서 색인과 조회

  • ElasticSearch API 구조
    • ${url}/${인덱스이름}/${타입이름}/${ID}
  • 색인 요청 과정
    1. 인덱스 생성
    2. 타입 생성
    3. 스키마 생성
    4. 문서 ID 검사
      1. 신규 색인
      2. 기존 문서 업데이트
  • 문서생성
curl --location --request POST 'http://localhost:9200/user/_doc/1?pretty' \
--header 'Content-Type: application/json' \
--data-raw '{
    "username" : "gwkim"
}'

# 응답
{
    "_index": "user",
    "_id": "1",
    "_version": 4,
    "result": "updated", 
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 5,
    "_primary_term": 1
}
  • 문서조회
curl --location --request GET 'http://localhost:9200/user/_doc/1?pretty'

# 응답
{
    "_index": "user",
    "_id": "1",
    "_version": 4,
    "_seq_no": 5,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "username": "gwkim"
    }
}
  • 문서삭제
curl --location --request DELETE 'http://localhost:9200/user/_doc/1?pretty'

# 응답
{
    "_index": "user",
    "_id": "1",
    "_version": 5,
    "result": "deleted",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 6,
    "_primary_term": 1
}
  • 인덱스생성
curl --location --request PUT 'http://localhost:9200/content?pretty'

# 응답
{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "content"
}
  • 인덱스조회
curl --location --request GET 'http://localhost:9200/_cat/indices?v'

# 응답
health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   content  0UnmPZWGSTm7tJGORR0Dsw   1   1          0            0       225b           225b
yellow open   user     VZzUTEBtTkyhN6VGS0FVkQ   1   1          1            2      5.1kb          5.1kb
yellow open   contents IVKXuFcvQsecxWoC-HDlCA   1   1          2            0      9.3kb          9.3kb
  • 스키마조회
    • 스키마가 정의된 상태에서 문서 색인시 타입을 다르게 넣으면 exception이 발생한다. (스키마 충돌)
curl --location --request GET 'http://localhost:9200/contents/_mappings?pretty'

# 응답
{
    "contents": {
        "mappings": {
            "properties": {
                "author": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "rating": {
                    "type": "float"
                },
                "title": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                }
            }
        }
    }
}

 

2.2 문서 검색하기

  • bluk insert
    • elasticsearch bluk API용 json 데이터를 준비하여 실행해야한다.
    • /books/_doc/_bluk?pretty&refresh
  • 풀 스캔 쿼리
    • /books/_search?q=*&pretty
  • 매칭 쿼리
/books/_search?q=*&pretty
{
	"query" : {"match": {"name" : "gwkim"}}
}

 

2.3 문서 분석하기

  • search API를 통한 aggregation
/books/_search?q=*&pretty
{
	"size" : 0,
	"aggs" : {
		"group_by_state" : {
			"terms" : {
				"field" : "topics.keyword"
			}
		}
	}
}
Comments