# ElasticSearch 맀핑(Mapping)

img


# μ‹€μŠ΅ ν™˜κ²½

  • πŸ’‘ 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 (opens new window) λ‚΄μš©μ„ 읽어볼 것을 ꢌμž₯ν•œλ‹€.

{
	"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 버전은 곡식 λ¬Έμ„œ (opens new window)에 λ‚˜μ™€μžˆλŠ” 것과 같이 λ¬΄ν˜•μ‹ 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ν•˜κΈ°

μ—¬λŸ¬ Documentλ₯Ό bulk둜 ν•œλ²ˆμ— λ„£μ–΄λ³΄μž.

# βœ” 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이 μ •μƒμ μœΌλ‘œ μž…λ ₯된 것을 확인할 수 μžˆλ‹€!


λ³Έ ν¬μŠ€νŒ…μ€ Inflearn의 ELK μŠ€νƒ (ElasticSearch, Logstash, Kibana) 으둜 데이터 뢄석 (opens new window) κ°•μ˜λ₯Ό μ°Έκ³ ν•˜μ—¬ μž‘μ„±λ˜μ—ˆμŠ΅λ‹ˆλ‹€.


Last Updated: 6/18/2023, 2:13:15 PM