1. Index, Type, Document

이전 글에서 Cluster에 대해 알아보았습니다.

Clusternode들의 집합이고 노드는 shard로 구성되며, 데이터는 shard로 분산되어 저장합니다.


Index는 1개 이상의 primary shard에 매핑되고, 0개 이상의 replica shard를 가질 수있는 논리적 이름 공간을 말하며, RDBMS의 database와 같은 개념입니다.

Type은 RDBMS에서 database안에 존재하는 table 개념이고,

Document는 RDBMS에서 row와 같은 개념입니다. 즉, 실제로 검색 할 데이터를 의미합니다.


Restful API를 통해 index에 document를 추가할 수 있는데, 이를 "문서를 색인화 한다"고 합니다.

문서를 색인화 하기 위해서는 index가 어떤 type인지, 그리고 _id를 지정해줘야 합니다.

여기서 _id는 RDBMS에서 table의 pk와 같습니다.


다음은 customer index에서 external type으로 1번 id에 document를 추가하는 API입니다.


이제부터 Index, Type, Document에 대한 CURD API를 알아보겠습니다.





1. 클러스터에 존재하는 모든 index 조회

# curl -XGET 'localhost:9200/_cat/indices?v'

처음엔 아무런 데이터가 없습니다.





2. index 추가

다음으로 customer라는 index를 추가해보고 다시 모든 index를 조회 해보겠습니다.

# curl -XPUT 'localhost:9200/customer?pretty'
# curl -XGET 'localhost:9200/_cat/indices?v'


"acknowledged: true"이면, 작업이 성공되었다는 뜻입니다.

조회 결과 customer index가 생성된 것을 확인할 수 있습니다.

그리고 API를 호출할 때 ?pretty를 URL에 추가해줬는데요, 이는 결과( response )를 예쁘게 보여주기 위함입니다.

yellow status인 이유는 이전 글( 링크 )에서 언급을 했었습니다.





3. document 추가

다음으로 index에 document를 추가해보겠습니다.

문서를 색인화 하려면 어떤 type인지, 몇 번 _id에 색인화할 것인지 명시해줘야 합니다.


아래의 API 호출은 customer2 index에서 info type의 1번 _id에 색인화하는 작업입니다.

-d 옵션은 --data-binary의 축약이며, 추가할 데이터를 명시합니다.

# curl -XPOST 'localhost:9200/customer2/info/1?pretty' -H 'Content-Type: application/json' -d '{ "name": "victolee" }'


여기서 customer2 index와 info type는 생성을 한 적이 없는데, index와 type을 자동으로 생성하면서 색인화가 되었다는 점에 주목할 필요가 있습니다.



또한 document를 추가할 때 _id 값을 명시하지 않으면, 임의의 문자열을 _id로 할당합니다.

# curl -XPOST 'localhost:9200/customer2/info?pretty' -H 'Content-Type: application/json' -d '{
"name": "victolee2"
}'



그런데 추가하려는 필드가 name 뿐만 아니라 여러가지가 있다면 명령어에 데이터를 모두 입력하기가 귀찮고, 반복적으로 사용할 수도 없습니다.

그래서 데이터를 json 형식의 파일로 만들어서 API를 호출하는 방법을 사용할 수 있습니다.

-d 옵션에 json을 작성하는 것이 아닌 @파일경로 를 작성하여 명령어를 실행하면 됩니다.

# vi data.json

{ "name": "victory", "address": "경기도", "phone": "010-123-1234", "reg_date": "2019-03-31" }


# curl -XPOST 'localhost:9200/customer2/info/2?pretty' -H 'Content-Type: application/json' -d @data.json





4. document 조회

다음으로 지금까지 추가한 document들을 조회해보겠습니다.

ES는 검색 엔진으로서 다양한 검색 방법을 제공하는데요.

다음 글에서 검색에 대해 자세히 알아보도록 하고, 지금은 간단한 API만 소개하도록 하겠습니다.


먼저 customer2 index의 모든 document를 조회하는 방법입니다.

# curl -XGET 'localhost:9200/customer2/_search?pretty'


URL에 있는 _search는 query DSL를 위해 사용하는 API이며, 원래는 뒤에 query를 명시해줘야 하지만 다음 글에서 자세히 다루도록 하겠습니다.



다음은 모든 index( _all )에서 모든 document를 검색해보겠습니다.

customer index에 데이터가 없으므로 데이터를 추가하고 검색을 해보도록 하겠습니다.

# curl -XPOST 'localhost:9200/customer/info?pretty' -H 'Content-Type: application/json' -d '{ "name": "victolee" }' # curl -XGET 'localhost:9200/_all/_search?pretty'

customer, customer2 두 index 모두에서 document를 검색한 것을 확인할 수 있습니다.



다음은 _id를 이용해서 document를 검색하는 방법입니다.

# curl -XGET 'localhost:9200/customer2/info/1?pretty'

맨 처음 추가했던 document가 조회되네요.

응답 결과의 _source 프로퍼티에는 실제 데이터가 있습니다.



다음은 document의 응답 결과를 줄일 수 있는 방법입니다. ( 참고 링크 )

SQL에서 SELECT 문에 특정 컬럼들을 명시하는 방법과 같은 개념입니다. ( SELECT 컬럼1, 컬럼2 FROM 테이블 )

예를 들어, 실제 데이터인 _source만 보고 싶을 때 query String에 filter_path=_source를 추가합니다.

# curl -XGET 'localhost:9200/customer2/info/2?pretty&filter_path=_source'

# curl -XGET 'localhost:9200/customer2/info/2?pretty&filter_path=_source.name'




5. document 수정

수정 작업은 추가 작업이랑 유사합니다.

customer2 index에서 _id가 1인 document를 수정해보도록 하겠습니다.

# curl -XPUT 'localhost:9200/customer2/info/1?pretty' -H 'Content-Type: application/json' -d '{
"name": "victolee_update"
}'

document가 수정 됐고, _version 정보가 바뀐 것을 확인할 수 있습니다.





6. index, document 삭제

특정 id의 document를 삭제하는 방법입니다.

# curl -XDELETE 'localhost:9200/customer2/info/1?pretty'



다음은 index를 삭제하는 방법입니다.

# curl -XDELETE 'localhost:9200/customer2?pretty'





이상으로 index, document를 중심으로 데이터를 조작하는 CRUD 작업을 해보았습니다.

다음은 조금 더 응용된 방식으로 document를 검색하는 API에 대해 알아보겠습니다.