급하게 엘라스틱 서치를 사용할 일이 생겨 일단 엘라스틱 서치를 테스트 해보았다. 테스트를 위한 엘라스틱 서치는 도커로 실행 하였고 아래의 파일이 컨테이너 실행에 사용된 docker-compose.yaml
파일이다.
version: '3.8'
services:
elsticsearch01:
image: docker.elastic.co/elasticsearch/elasticsearch-oss:7.10.2-arm64 # AWS managed service 에서 지원해주는 elasticsearch 버전
container_name: elsticsearch01
environment:
- cluster.name=elsticsearch-cluster
- node.name=es01
- network.host=_site_
- discovery.seed_hosts=elsticsearch02,elsticsearch03
- cluster.initial_master_nodes=es01,es02,es03
- bootstrap.memory_lock=true # Elasticsearch가 사용하는 힙 메모리 영역을 점유
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- ~/dev/elasticsearch/data01:/usr/share/elasticsearch/data # 색인된 데이터 마운트
- ~/dev/elasticsearch/logs01:/usr/share/elasticsearch/logs # 실행 로그 마운트
ports:
- 9200:9200
networks:
- elastic
elsticsearch02:
image: docker.elastic.co/elasticsearch/elasticsearch-oss:7.10.2-arm64 # AWS managed service 에서 지원해주는 elasticsearch 버전
container_name: elsticsearch02
environment:
- cluster.name=elsticsearch-cluster
- node.name=es02
- network.host=_site_
- discovery.seed_hosts=elsticsearch01,elsticsearch03
- cluster.initial_master_nodes=es01,es02,es03
- bootstrap.memory_lock=true # Elasticsearch가 사용하는 힙 메모리 영역을 점유
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- ~/dev/elasticsearch/data02:/usr/share/elasticsearch/data # 색인된 데이터 마운트
- ~/dev/elasticsearch/logs02:/usr/share/elasticsearch/logs # 실행 로그 마운트
networks:
- elastic
elsticsearch03:
image: docker.elastic.co/elasticsearch/elasticsearch-oss:7.10.2-arm64 # AWS managed service 에서 지원해주는 elasticsearch 버전
container_name: elsticsearch03
environment:
- cluster.name=elsticsearch-cluster
- node.name=es03
- network.host=_site_
- discovery.seed_hosts=elsticsearch02,elsticsearch03
- cluster.initial_master_nodes=es01,es02,es03
- bootstrap.memory_lock=true # Elasticsearch가 사용하는 힙 메모리 영역을 점유
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- ~/dev/elasticsearch/data03:/usr/share/elasticsearch/data # 색인된 데이터 마운트
- ~/dev/elasticsearch/logs03:/usr/share/elasticsearch/logs # 실행 로그 마운트
networks:
- elastic
networks:
elastic:
driver: bridge
현재 최신 버전의 엘라스틱 서치는 8.5 버전이지만 Amazon OpenSearch Service에서 현재 지원해주는 가장 최신 버전은 7.10 버전이고 해당 managed service의 사용도 고려해야 했기 때문에 7.10 버전으로 테스트를 진행했다.
도커 데스크톱으로 컨테이너가 성공적으로 실행되는 것을 확인할 수 있었다.
curl 명령어로 클러스터 상태정보를 호출해 동작을 확인해보았다.
curl -XGET "http://localhost:9200"
{
"name" : "0f1dc595210f",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "-jx8shz3Rtay_M6VZlYZQQ",
"version" : {
"number" : "7.10.2",
"build_flavor" : "oss",
"build_type" : "tar",
"build_hash" : "747e1cc71def077253878a59143c1f785afa92b9",
"build_date" : "2021-01-13T00:42:12.435326Z",
"build_snapshot" : false,
"lucene_version" : "8.7.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
request
$ curl -XPUT "http://localhost:9200/test_index/_doc/1" -H 'Content-Type: application/json' -d'
{
"name": "이동영",
"message": "Elasticsearch 입력 테스트"
}'
response
{"_index":"test_index","_type":"_doc","_id":"1","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}
POST 메서드를 사용하고 _doc
까지만 입력해주면 임의로 ID 값을 생성받을 수 있다.
request
curl -XPOST "http://localhost:9200/test_index/_doc" -H 'Content-Type: application/json' -d'
{
"name": "이동삼",
"message": "Elasticsearch 입력 테스트"
}'
{"_index":"test_index","_type":"_doc","_id":"SH5IyYQBdmi1ZgkkioOt","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":6,"_primary_term":1}
request
curl -XGET "http://localhost:9200/test_index/_doc/1"
response
{"_index":"test_index","_type":"_doc","_id":"1","_version":1,"_seq_no":0,"_primary_term":1,"found":true,"_source": {"name": "이동영","message": "Elasticsearch 입력 테스트"}}
request
curl -XPOST "http://localhost:9200/test_index/_update/1" -H 'Content-Type: application/json' -d'
{
"doc": {
"name": "이동일",
"message": "Elasticsearch 업데이트 테스트"
}
}'
response
{"_index":"test_index","_type":"_doc","_id":"1","_version":2,"result":"updated","_shards":{"total":2,"successful":2,"failed":0},"_seq_no":1,"_primary_term":1}
다시 get을 통해 업데이트가 된 것을 확인할 수 있다.
request
curl -XGET "http://localhost:9200/test_index/_doc/1"
response
{"_index":"test_index","_type":"_doc","_id":"1","_version":2,"_seq_no":1,"_primary_term":1,"found":true,"_source":{"name":"이동일","message":"Elasticsearch 업데이트 테스트"}}
필드 하나만 업데이트
request
curl -XPOST "http://localhost:9200/test_index/_update/1" -H 'Content-Type: application/json' -d'
{
"doc": {
"message": "Elasticsearch message만 업데이트 테스트"
}
}'
response
{"_index":"test_index","_type":"_doc","_id":"1","_version":3,"result":"updated","_shards":{"total":2,"successful":2,"failed":0},"_seq_no":2,"_primary_term":1}
다시 get을 통해 필드가 하나만 업데이트가 된 것을 확인할 수 있다.
request
curl -XGET "http://localhost:9200/test_index/_doc/1"
response
{"_index":"test_index","_type":"_doc","_id":"1","_version":3,"_seq_no":2,"_primary_term":1,"found":true,"_source":{"name":"이동일","message":"Elasticsearch message만 업데이트 테스트"}}
request
curl -XDELETE "http://localhost:9200/test_index/_doc/1"
response
{"_index":"test_index","_type":"_doc","_id":"1","_version":4,"result":"deleted","_shards":{"total":2,"successful":2,"failed":0},"_seq_no":3,"_primary_term":1}
삭제한 도큐먼트를 GET 하면
curl -XGET "http://localhost:9200/test_index/_doc/1"
found가 false 로 나온다.
{"_index":"test_index","_type":"_doc","_id":"1","found":false}
인덱스 자체를 DELETE 하게 되면,
curl -XDELETE "http://localhost:9200/test_index"
아래와 같이 응답을 준다.
{"acknowledged":true}
벌크 명령은 바로 curl에 포함시켜 던질 수도 있지만 따로 저장한 json 파일을 --data-binary
로 지정해 던질 수 있다. 파일 이름 앞에 @
를 붙여햐한다.
bulk.json이라는 파일명으로 아래와 같이 저장했다.
{"index":{"_index":"test_index", "_id":"1"}}
{"field":"one"} // test_index/_doc/1 에 {"field":"one"} 입력
{"index":{"_index":"test_index", "_id":"2"}}
{"field":"two"} // test_index/_doc/2 에 {"field":"two"} 입력
{"delete":{"_index":"test_index", "_id":"2"}} // test_index/_doc/2 도큐먼트 삭제
{"create":{"_index":"test_index", "_id":"3"}}
{"field":"three"} // test_index/_doc/3 에 {"field":"three"} 입력
{"update":{"_index":"test_index", "_id":"1"}}
{"doc":{"field":"two"}} // test_index/_doc/1 를 {"field":"two"} 로 수정
이때 주의 할 점은 json 파일의 끝을 개행해주지 않으면
The bulk request must be terminated by a newline [\\n]
라는 에러가 나오기 때문에 파일의 끝은 개행해주어야 한다.
request
curl -XPOST "http://localhost:9200/test_index/_bulk" -H 'Content-Type: application/json' --data-binary @bulk.json
response
{"took":501,"errors":false,"items":[{"index":{"_index":"test_index","_type":"_doc","_id":"1","_version":1,"result":"created","_shards":{"total":2,"successful":2,"failed":0},"_seq_no":0,"_primary_term":1,"status":201}},{"index":{"_index":"test_index","_type":"_doc","_id":"2","_version":1,"result":"created","_shards":{"total":2,"successful":2,"failed":0},"_seq_no":1,"_primary_term":1,"status":201}},{"delete":{"_index":"test_index","_type":"_doc","_id":"2","_version":2,"result":"deleted","_shards":{"total":2,"successful":2,"failed":0},"_seq_no":2,"_primary_term":1,"status":200}},{"create":{"_index":"test_index","_type":"_doc","_id":"3","_version":1,"result":"created","_shards":{"total":2,"successful":2,"failed":0},"_seq_no":3,"_primary_term":1,"status":201}},{"update":{"_index":"test_index","_type":"_doc","_id":"1","_version":2,"result":"updated","_shards":{"total":2,"successful":2,"failed":0},"_seq_no":4,"_primary_term":1,"status":200}}]}
참고자료:
https://esbook.kimjmin.net/
https://www.elastic.co/guide/en/elasticsearch/reference/7.5/docker.html