kok202
[2019.03.06] Elastic search (CRUD)

2019. 3. 6. 11:40[정리] 데이터베이스/[NoSQL] ElasticSearch

다음 강의를 정리중입니다.

https://www.youtube.com/watch?v=69OoC7haeeA&list=PLVNY1HnUlO25m5tT06HaiHPs2nV3cLhUD&index=1




Elastic search JVM에서 동작한다.

설치 경로 : /usr/share/elasticsearch

설정 경로 : /etc/elasticsearch

init 스크립트 : /etc/init.d/elasticsearch

실행 커맨드: service elasticsearch start




 Elastic Search

Index

 Type

 document (JSON 파일)

 JSON field

 Realation DB

 Database

 Table

 Row

 Column


CRUD (Create, Read, Update, Delete)

Elastic search는 REST API 로 통신한다.

그러므로 관계형 데이터베이스에서 사용하는 select, update, insert, delete 쿼리는 ES 에서는GET, PUT, POST, DELETE 로 대응된다.

즉 ES에서 통신하는 방법은 다음과 같다.

curl -XGET http://localhost:9200/classes

curl -XPUT http://localhost:9200/classes

curl -XPOST http://localhost:9200/classes

curl -XDELETE http://localhost:9200/classes





READ

curl -XGET http://localhost:9200/classes?pretty

(ElasticSearch주소 / 인덱스 이름) 으로 접근한다.

X는 curl에서 REST API을 명시하는 커맨드

?pretty는 JSON 포맷으로 예쁘게 보여줘라


CREATE

curl -XPOST http://localhost:9200/classes/class/1 -H 'Content-Type: application/json' -d {"title":"algorithm", "professor":"John"}

curl -XPOST http://localhost:9200/classes/class/1 -H 'Content-Type: application/json' -d @FilePath.json

curl -XPOST http://localhost:9200/_bulk?pretty -H 'Content-Type: application/json' --data-binary @FilePath.json

-H 는 헤더파일 지정이다.

-d 는 데이터 지정이다.

_bulk를 붙이면 파일 안에 여러 데이터가 있을 경우 한번에 올릴 수 있다.


UPDATE

curl -XPOST http://localhost:9200/classes/class/1/_update?pretty -H 'Content-Type: application/json' -d '{"doc":{"maxPeople":20}}'

curl -XPOST http://localhost:9200/classes/class/1/_update?pretty -H 'Content-Type: application/json' -d '{"doc":{"maxPeople":30}}'

curl -XPOST http://localhost:9200/classes/class/1/_update?pretty -H 'Content-Type: application/json' -d '{"script":{"ctx._source.maxPeople":+=10}}'

_update를 붙이면 field 하나를 추가할 수도 있고 변경 할수도 있다.

"doc" 대신 "script"를 사용하면 좀 더 산술적인 연산이 가능하다.