elasticsearch에 json 생성파일로 인덱스를 생성하는 예제이다.  (그리고 필드 변경/삭제에 관련한 이슈가 있는 예제이다.)


만약 동일한 이름의 인덱스가 만들어져 있으면, es는 400 에러를 발생한다. 따라서, 인덱스를 삭제하고 다시 해야 한다.


$ curl -s -XPOST http://localhost:9200/feeds/ -d @settings.json

{"error":"IndexAlreadyExistsException[[feeds] already exists]","status":400}


삭제한다. 

 $ curl -s -XDELETE http://localhost:9200/feeds/

{"acknowledged":true}


settings.json 파일을 생성한다.



$ cat settings.json

{

    "mappings": {

        "feed_item": {

            "_source": {

                "enabled": true

            },

            "properties": {

                "profile_id": {

                    "type": "integer"

                },

                "aid": {

                    "type": "long"

                },

                "type": {

                    "type" : "string",

                    "analyzer" : "string_lowercase"

                }

            }

        }

    },

    "settings": {

        "analysis": {

            "analyzer": {

                "string_lowercase": {

                    "filter": "lowercase",

                    "tokenizer": "keyword"

                }

            }

        },

        "index": {

            "number_of_replicas": "1",

            "number_of_shards": 10,

            "refresh_interval": "2s",

            "store.type" : "memory"

        },

        "routing": {

            "required": true

        }

    }

}



인덱스를 생성한다. 

$ curl -s -XPOST http://localhost:9200/feeds/ -d @settings.json

{"acknowledged":true}


정상적으로 인덱스가 생성되었는지 확인한다. _mapping으로 확인할 수 있다. 

$ curl -s -XGET http://localhost:9200/feeds/_mapping?pretty

{

  "feeds" : {

    "mappings" : {

      "feed_item" : {

        "properties" : {

          "aid" : {

            "type" : "long"

          },

          "profile_id" : {

            "type" : "integer"

          },

          "type" : {

            "type" : "string",

            "analyzer" : "string_lowercase"

          }

        }

      }

    }

  }

}


필드를 동적으로 추가할 수 있다. 


curl -s -XPUT http://localhost:9200/feeds/_mapping/feed_item -d '

 {

   "feed_item" : {

     "properties" : {

     "read" : { "type" : "boolean" }

     }

   }

 }'




정상적으로 property가 추가되었는지 확인한다. 


{

  "feeds" : {

    "mappings" : {

      "feed_item" : {

        "properties" : {

          "aid" : {

            "type" : "long"

          },

          "profile_id" : {

            "type" : "integer"

          },

          "read" : {

            "type" : "boolean"

          },

          "type" : {

            "type" : "string",

            "analyzer" : "string_lowercase"

          }

        }

      }

    }

  }

}



특정 properties를 삭제해 보자. DELETE 내리는 순간 모두 삭제된다.!!! 



$ curl -s -XDELETE http://localhost:9200/feeds/_mapping/feed_item -d '

 {

   "feed_item" : {

     "properties" : {

    "read" : { "type" : "boolean" }

    }

   }

}'


$ curl -s -XGET http://localhost:9200/feeds/_mapping?pretty

{ }



* 주의할 점

- mapping 명령어 사용시에는 DELETE를 신중히 해야 한다. 

- 한번 mapping되면 property를 추가할 수 있지만, 변경이나 삭제가 불가능하다.  


(property라 부르는 것들은 사실상 es에서는 field라 불리운다.)


Posted by '김용환'
,