프로그래밍/검색

elasticsearch 6.2 mapping 하기

kugancity 2018. 11. 12. 01:44
반응형


참고: 날짜 타입  https://www.elastic.co/guide/en/elasticsearch/reference/current/date.html

bulk 색인하기 https://www.elastic.co/guide/en/elasticsearch/guide/2.x/bulk.html

index 확인방법 https://www.elastic.co/guide/en/elasticsearch/reference/current/_exploring_your_data.html

관련 포스팅 : http://yookeun.github.io/elasticsearch/2018/03/09/elastic-mapping/



elastic search 는 dynamic mapping 또는 explicit mapping 이 있다. 

dynamic mapping은 json 파일을 입력하면 자동으로 mapping type이 정해지는것이고 

explicit mapping 은 필드마다 매핑 타입을 따로 지정해주는 것이다. (문서 참조



dynamic mapping 이후 mapping 결과 확인하기 


 curl -XGET 'http://localhost:9200/indexname/_mapping/?pretty'




명시적 매핑 하기 (예시) 


curl -v -H 'Content-Type: application/json' -XPUT 'http://localhost:9200/indexname' --data-binary @mapping_indexname.json




명시적 매핑을 할 때 일일이 필드 이름 타이핑 하기 귀찮아서 

일단 bulk index로 dynamic mapping을 한 후 위 방법으로 mapping을 확인하거나 

kibana에서 mapping을 받아와서 수정하고 싶은 부분만 수정하고 있다. 


dynamic mapping을 할 경우 대부분 아래 예시와 같이 text로 매핑되어있을 경우가 많은데 

숫자라면 short, integer, float 등을 사용하고 키바나에서 분류를 쉽게 보고 싶으면 keyword로 해주는 것이 좋다. 


숫자 타입 링크https://www.elastic.co/guide/en/elasticsearch/reference/current/number.html







{

  "mappings": {

    "_doc": {

      "properties": {

        "address_jibun": {

          "type": "text",

          "fields": {

            "keyword": {

              "type": "keyword",

              "ignore_above": 256

            }

          }

        },

        "address_road": {

          "type": "text",

          "fields": {

            "keyword": {

              "type": "keyword",

              "ignore_above": 256

            }

          }

        },

        "anniversary": {

          "type": "date"

        },

        "arts_ratio": {

          "type": "text",

          "fields": {

            "keyword": {

              "type": "keyword",

              "ignore_above": 256

            }

          }

        },

        "budget_per_student": {

          "type": "text",

          "fields": {

            "keyword": {

              "type": "keyword",

              "ignore_above": 256

            }

          }

        },

     ...

      ,

        "student_per_teacher": {

          "type": "text",

          "fields": {

            "keyword": {

              "type": "keyword",

              "ignore_above": 256

            }

          }

        },

        "type": {

          "type": "text",

          "fields": {

            "keyword": {

              "type": "keyword",

              "ignore_above": 256

            }

          }

        }

      }

    }

  }

}




mapping 파일 예시 


{

    "mappings" : {

      "_doc" : {

        "properties" : {

          "area1" : {

            "type" : "text"

          },

          "area2" : {

            "type" : "text"

          },

          "areacode" : {

            "type" : "keyword"

          },

          "area3 : {

            "type" : "text"

          },

          "constructionyear" : {

            "type" : "short"

          },

          "date" : {

            "type" : "date"

          },

          "deposit" : {

            "type" : "date"

          }

          "exclusiveusearea" : {

            "type" : "float"

          },

          "floor" : {

            "type" : "short"

          },

          "name" : {

            "type" : "text"

          },

          "price" : {

            "type" : "integer"

          }

        }

      }

    }


}




728x90
반응형