로컬에서는 잘 되는데 ☘️

[ElasticSearch] Mapping

by youngjun._.

실습 환경

  • 💡 Elasticsearch 7.9.0
  • 💡 Windows 10
  • 💡 Git Bash

Mapping은 RDB에서 스키마와 같은 개념이다.

이전 포스팅에서 살펴본 것처럼 Mapping 없이도 데이터를 넣고 읽을 수 있었다.

하지만 실제로 서비스를 할 때 Mapping을 하지 않을 경우 여러 문제가 발생할 수 있다.

  • Document에 날짜를 넣고 싶은데 단순히 텍스트로 들어간다.

와 같은 상황에 Kibana 등의 가시화 도구와 분석을 할 때 타입이 맞지 않는 문제가 대표적이다.


실습에서 사용할 데이터는 다음과 같다.

Mapping 실습을 하기위해 Index를 먼저 삭제한 이후에 다시 생성하겠다.


1. Index 삭제하고 다시 만들기

✔ 삭제하기

$ curl -XDELETE http://localhost:9200/classes?pretty

✔ 생성하기

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

✔ 확인하기

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

결과를 보면 mappings 부분이 비어있는 것을 확인할 수 있다.

{
"classes" : {
"aliases" : { },
"mappings" : { },
"settings" : {
"index" : {
"creation_date" : "1598314352997",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "vLmOaHZLRMW_7kOZbi0ZRg",
"version" : {
"created" : "7090099"
},
"provided_name" : "classes"
}
}
}
}

2. Mapping 생성하기

Mapping으로 데이터 타입을 미리 지정해주기 위해 mapping JSON 파일이 있어야한다.

참고한 강의에서는 mapping type을 string으로 주었는데 이는 6.x 버전부터 text로 바뀌었다. 자세한 내용을 보고싶다면 포스팅 중 Error-handling을 확인하거나, stackoverflow 내용을 읽어볼 것을 권장한다.

{
"class" : {
"properties" : {
"title" : {
"type" : "text"
},
"professor" : {
"type" : "text"
},
"major" : {
"type" : "text"
},
"semester" : {
"type" : "text"
},
"student_count" : {
"type" : "integer"
},
"unit" : {
"type" : "integer"
},
"rating" : {
"type" : "integer"
},
"submit_date" : {
"type" : "date",
"format" : "yyyy-MM-dd"
},
"school_location" : {
"type" : "geo_point"
}
}
}
}

다음과 같은 JSON 파일로 mapping 시켜보자.


X PUT 명령어를 사용해 mapping을 생성한다. 다음과 같이 입력하면 된다.

curl -XPUT 'http://localhost:9200/classes/class/_mapping' -d @classesRating_mapping.json

지난 포스팅과 마찬가지로 Windows에서는 Content-Type 을 H 옵션으로 입력해줘야한다.

그리고 7.x 버전은 공식 문서에 나와있는 것과 같이 무형식 API를 사용하므로 include_type_name=true를 추가해주어야 한다.

$ curl -XPUT 'http://localhost:9200/classes/class/_mapping?include_type_name=true&pretty' -d @classesRating_mapping.json -H 'Content-Type: application/json'

Error 없이 정상적으로 수행됐다면 class 타입에 mapping이 추가된 것이다.

확인해보자!

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

결과

{
"classes" : {
"aliases" : { },
"mappings" : {
"properties" : {
"major" : {
"type" : "text"
},
"professor" : {
"type" : "text"
},
"rating" : {
"type" : "integer"
},
"school_location" : {
"type" : "geo_point"
},
"semester" : {
"type" : "text"
},
"student_count" : {
"type" : "integer"
},
"submit_date" : {
"type" : "date",
"format" : "yyyy-MM-dd"
},
"title" : {
"type" : "text"
},
"unit" : {
"type" : "integer"
}
}
},
"settings" : {
"index" : {
"creation_date" : "1598314352997",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "vLmOaHZLRMW_7kOZbi0ZRg",
"version" : {
"created" : "7090099"
},
"provided_name" : "classes"
}
}
}
}

참고로 type 중 geo_point는 지도에 실제 위치를 띄어줄 수 있는 type이다.

3. 실제 데이터를 Index에 bulk하기

여러 Documentbulk로 한번에 넣어보자.

✔ Bulk 하기

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

✔ 확인하기

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

결과

{
"_index" : "classes",
"_type" : "class",
"_id" : "1",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"title" : "Machine Learning",
"Professor" : "YoungJun Park",
"major" : "Computer Science",
"semester" : [
"spring",
"fall"
],
"student_count" : 100,
"unit" : 3,
"rating" : 5,
"submit_date" : "2020-01-02",
"school_location" : {
"lat" : 36.0,
"lon" : -120.0
}
}
}

Mapping type이 정상적으로 입력된 것을 확인할 수 있다!


본 포스팅은 InflearnELK 스택 (ElasticSearch, Logstash, Kibana) 으로 데이터 분석 강의를 참고하여 작성되었습니다.


블로그의 프로필 사진

블로그의 정보

개발하는만두

youngjun._.

활동하기