kok202
[2019.03.06] Elastic Search (Mapping, Search, Aggregation)

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

Mapping

 = Scheme 

 = 어떤 데이터가 있는지 그 데이터는 어떤 타입이어야 하는지 알리는 것

 = Mapping은 테이블(문서)에 지정하는 과정

참조 : https://github.com/minsuk-heo/BigData/tree/master/ch01



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

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

mapping { } 으로 되있음

classes 인덱스는 있지만 매핑이 안된상태




curl -XPUT http://localhost:9200/classes/class/_mapping?pretty -H 'Content-Type: application/json' -d @ch01/classesRating_mapping.json

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

mapping이 { } 가 아니게됨
어떤 필드가 어떤 타입의 데이터인지 명시하게된 상태
예제를 실행해보다 보면 Mapping 과정에서 No handler for type [string] declared on field 에러가 발생
-> Elastic search 가 5 -> 6으로 업데이트 되면서 string 이 사라지고 text로 대체됨



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

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

스키마가 정해져있는 상태로 데이터를 집어 넣은 것을 확인 가능하다.





Search


curl -XGET http://localhost:9200/classes/class/_search?q=student_count:30&pretty

q= 는 쿼리를 의미
_search 키워드가 검색을 하겠다는 의미 




curl -XGET http://localhost:9200/classes/class/_search -H 'Content-Type: application/json' -d '{"query":{"term":{"student_count":30}}}'

-d 키워드를 이용하면 json 포맷을 사용하여 검색할 수 있다.





Aggregation (집합)

Search에 어떤식으로 검색해달라고 방법을 지정해주는 것

Metric aggregation : 평균, 합계 등을 할 수 있음

통계를 위한 Aggregation 파일 : https://github.com/minsuk-heo/BigData/tree/master/ch03



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

통계를 위한 Aggregation 파일이 FilePath.json 라고 할때 위와 같이 curl을 날려 통계를 구할 수 있다.




Bucket aggregation : 특정 필드의 값을 기준으로 그룹핑하고 그룹(document)들에 대한 통계 버켓을 만든다. 

Metric aggregation 와 사용법은 같다.