티스토리 뷰

$./get.sh 1 $./get.sh 100 $./get.sh 101안녕하세요 강정호입니다. 오늘은 Elastic Search의 인덱스 생성 및 기본적인 명령어들을 공부해 볼게요.



Index 생성


1. 인덱스 생성

$ curl -XPUT 'localhost:9200/customer?v&pretty'


: PUT 명령어를 이용해서 customer 라는 인덱스를 생성합니다. 이 때 출력 방식은 v&pretty 방식으로 하여 보기 좋게(이쁘게) 출력하는 옵션입니다.




2. 생성한 인덱스 체크하기

$ curl -XGET 'localhost:9200/_cat/indices?v&pretty'


: GET 명령어를 사용합니다. _cat은 실행 함수로서 indices(인덱스 목록)를 v&pretty 방식으로 출력합니다.



데이터 삽입 : XPUT


$ vi put1.sh : put1.sh 파일 생성


$ curl -XPUT 'localhost:9200/customer/external/1?v&pretty' -d'{"name": "John Doe"}'

: -XPUT 방식으로 customer라는 인덱스에 id=1인 데이터를 삽입한다. 그 데이터는 name=John Doe이다.



$ chmod +x put1.sh : put1.sh를 실행할 수 있게 권한을 주는 명령어가 chmod(change mode)이다.


$ ./put1.sh : put1.sh에 입력된 쿼리문이 실행된다.




데이터 반환 -XGET


$vi get.sh : get.sh 파일을 생성한다.


$ curl -XGET 'localhost:9200/customer/external/'$1'/'

: GET 방식으로 id=1인 데이터를 customer 인덱스에서 가져온다.


$ chmod +x get.sh : get.sh 파일이 실행될 수 있도록 권한 부여


$ ./get.sh 1 : id=1에 해당하는 데이터 출력

$ ./get.sh 2 : id=2에 해당하는 데이터 출력




데이터 삽입 -XPOST


$ vi insert.sh : insert.sh 파일 생성


curl -XPOST 'localhost:9200/customer/external?v&pretty' -d'{"name" : "Tom Jerry"}'


$ ./insert.sh


위의 특징으로는 insert된 데이터가 가지는 id는 랜덤값인 것을 볼 수 있다.

이것이 -XPUT과 -XPOST의 차이점이다. -XPUT은 명시적인 아이디가 있을 때 사용하고, -XPOST는 명시적인 아이디가 없을 때 사용한다.


참고 링크 : Elastic Search XPUT, XPOST 차이 



데이터 업데이트 : XPOST


$ cp put1.sh update1.sh

$ vi update1.sh


curl -XPOST 'localhost:9200/customer/external/1/_update?v&pretty' -d'{"doc" : {"name" : "Jane Doe"}}'

: POST 방식으로 customer 인덱스에서 external 타입으로 id=1인 데이터를 _update 함수를 사용하여 업데이트한다.



$./update1.sh

$./get.sh 1 : 업데이트 된 데이터를 반환 받는다.


위와 같이 Jane Doe로 데이터가 변경된 것을 볼 수 있다.


$ cp update1.sh update2.sh

$ vi update2.sh


curl -XPOST 'localhost:9200/customer/external/1/_update?v&pretty' -d'{"doc":{"name":"Jane Doe", "age":20}}'


$./update2.sh

$./get.sh 1




데이터 삭제 : XDELETE


$vi delete.sh


curl -XDELETE 'localhost:9200/customer/external/2'

: DELETE 명령어로 customer 인덱스의 id=2을 삭제한다.


$./delete.sh


$./get.sh 2 : id=2에 해당하는 데이터가 없기 때문에 false가 리턴된다.




Bulk insert : 여러 개의 데이터 삽입하기


$vi bulk.json

 {"index":{"_id":"100"}} 

 {"name": "Donald Trump"} 

 {"index":{"_id":"101"}} 

 {"name": "Hillery Clinton" }


$vi bulk_json.sh

curl -XPOST 'localhost:9200/customer/external/_bulk?v&pretty' --data-binary "@bulk.json"


: customer 인덱스에 _bulk 함수를 사용하여 bulk.json 파일의 데이터를 삽입한다.




Bulk Function


$ vi bulk_func.json


{"update":{"_id":"1"}} 

{"doc": { "name": "John Doe becomes Jane Doe" } } 

{"delete":{"_id":“100"}}


$ bulk_func.sh

curl -XPOST 'localhost:9200/customer/external/_bulk?v&pretty' --data-binary "@bulk_func.json"


$./bulk_func.sh


$./get.sh 1

$./get.sh 100 

$./get.sh 101



Search & Sort


$ vi search_all.sh

curl -XGET 'localhost:9200/customer/_search?v&pretty' -d'{"query" : {"match_all" : {} } }'

: match_all 이라는 쿼리를 사용해서 _search를 한다. customer 인덱스의 모든 데이터를 가져오는 작업이다.



$ ./search_all.sh




$ vi search_sort.sh

curl -XGET 'localhost:9200/customer/_search?v&pretty' -d'{ "query": { "match_all": {} }, "sort": [ { "age": "desc" } ] }'


$ ./search_sort.sh



인덱스 삭제하기 : XDELETE


$ vi delete_index.sh

curl -XDELETE 'localhost:9200/customer'


$./delete_index.sh


acknowledge : true 라는 메시지가 출력되면서 인덱스가 삭제된다.


$ curl -XGET 'localhost:9200/_cat/indices?v&pretty'

--> 인덱스가 없는 것을 알 수 있다




이상으로 Elastic Search의 기본 명령어에 대해 공부해 보았습니다. 다음 시간에는 쿼리문을 사용하고, 웹 상에서 엘라스틱 서치를 사용하는 법을 공부해 보겠습니다.

댓글