LoginSignup
47
53

More than 3 years have passed since last update.

Elasticsearch index,typeについて

Last updated at Posted at 2017-10-23

  • Elasticsearchのindexとtypeのところがわからなかったので調べました
  • version5.5に基づいています。6以降は、概要の追記部分からキャッチアップしてください。

概要

環境

  • AWS Elasticsearch service(Elasticsearch5.5)

実験開始

index作成

  • この時はtypeもmappingもない
$ curl -X PUT https://search-my-es-xxx.ap-northeast-1.es.amazonaws.com/my_index?pretty
{
  "acknowledged" : true,
  "shards_acknowledged" : true
}

$ curl -X GET https://search-my-es-xxx.ap-northeast-1.es.amazonaws.com/my_index?pretty
{
  "my_index" : {
    "aliases" : { },
    "mappings" : { },
    "settings" : {
      "index" : {
        "creation_date" : "1508770567930",
        "number_of_shards" : "5",
        "number_of_replicas" : "1",
        "uuid" : "HkK-3dIlTNOw9rX_7tlHQw",
        "version" : {
          "created" : "5050299"
        },
        "provided_name" : "my_index"
      }
    }
  }
}

mapping作成

  • この書き方は新規作成用だからエラー (index .. already exists..)
$ curl -XPUT https://search-my-es-xxx.ap-northeast-1.es.amazonaws.com/my_index -d '
{
    "mappings" : {
        "my_type1" : {
            "properties" : {
                "field1" : { "type" : "text" }
            }
        }
    }
}'

mapping作成

  • これはOK
$ curl -XPUT https://search-my-es-xxx.ap-northeast-1.es.amazonaws.com/my_index/_mapping/my_type1 -d '
{
    "properties" : {
        "field1" : { "type" : "text" }
    }
}'

# typeが増えた
$ curl -X GET https://search-my-es-xxx.ap-northeast-1.es.amazonaws.com/my_index?pretty
{
  "my_index" : {
    "aliases" : { },
    "mappings" : {
      "my_type1" : {
        "properties" : {
          "field1" : {
            "type" : "text"
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1508770567930",
        "number_of_shards" : "5",
        "number_of_replicas" : "1",
        "uuid" : "HkK-3dIlTNOw9rX_7tlHQw",
        "version" : {
          "created" : "5050299"
        },
        "provided_name" : "my_index"
      }
    }
  }
}

別のtypeに別のfield定義を追加

$ curl -XPUT https://search-my-es-xxx.ap-northeast-1.es.amazonaws.com/my_index/_mapping/my_type2 -d '
{
    "properties" : {
        "field2" : { "type" : "text" }
    }
}'

$ curl -X GET https://search-my-es-xxx.ap-northeast-1.es.amazonaws.com/my_index?pretty
{
  "my_index" : {
    "aliases" : { },
    "mappings" : {
      "my_type2" : {
        "properties" : {
          "field2" : {
            "type" : "text"
          }
        }
      },
      "my_type1" : {
        "properties" : {
          "field1" : {
            "type" : "text"
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1508770567930",
        "number_of_shards" : "5",
        "number_of_replicas" : "1",
        "uuid" : "HkK-3dIlTNOw9rX_7tlHQw",
        "version" : {
          "created" : "5050299"
        },
        "provided_name" : "my_index"
      }
    }
  }
}

同じindexのtype違いで同名のfieldは同一の定義でないといけない

# my_type1で定義済みのfield1を別定義にしているのでこれはエラー(Mapper for [field1] conflicts with existing mapping)
$ curl -XPUT https://search-my-es-xxx.ap-northeast-1.es.amazonaws.com/my_index/_mapping/my_type2 -d '
{
    "properties" : {
        "field1" : {
            "type" : "text",
            "analyzer": "standard"
        }
    }
}'

# my_type1と同じ定義にする これはOK
$ curl -XPUT https://search-my-es-xxx.ap-northeast-1.es.amazonaws.com/my_index/_mapping/my_type2 -d '
{
    "properties" : {
        "field1" : {
            "type" : "text"
        }
    }
}'

$ curl -X GET https://search-my-es-xxx.ap-northeast-1.es.amazonaws.com/my_index?pretty
{
  "my_index" : {
    "aliases" : { },
    "mappings" : {
      "my_type2" : {
        "properties" : {
          "field1" : {
            "type" : "text"
          },
          "field2" : {
            "type" : "text"
          }
        }
      },
      "my_type1" : {
        "properties" : {
          "field1" : {
            "type" : "text"
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1508770567930",
        "number_of_shards" : "5",
        "number_of_replicas" : "1",
        "uuid" : "HkK-3dIlTNOw9rX_7tlHQw",
        "version" : {
          "created" : "5050299"
        },
        "provided_name" : "my_index"
      }
    }
  }
}

document登録

  • my_type1にfield2はないが自動で定義される
$ curl -XPUT https://search-my-es-xxx.ap-northeast-1.es.amazonaws.com/my_index/my_type1/1 -d '
{
    "field1": "aaa bbb",
    "field2": "aaa ccc"
}'

$ curl -XGET https://search-my-es-xxx.ap-northeast-1.es.amazonaws.com/my_index/_mapping/my_type1?pretty
{
  "my_index" : {
    "mappings" : {
      "my_type1" : {
        "dynamic" : "strict",
        "properties" : {
          "field1" : {
            "type" : "text"
          },
          "field2" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          }
        }
      }
    }
  }
}

定義変更

  • 自動mappingをしないようにする
$ curl -XPUT https://search-my-es-xxx.ap-northeast-1.es.amazonaws.com/my_index/_mapping/my_type1 -d '
{
    "dynamic" : "strict"
}'

# field3は未定のためエラー(mapping set to strict...)
$ curl -XPUT https://search-my-es-xxx.ap-northeast-1.es.amazonaws.com/my_index/my_type1/3 -d '
{
    "field1": "aaa bbb",
    "field2": "aaa ccc",
    "field3": "aaa ddd"
}'

結論

  • indexは複数のtypeを持つ
  • mappingはtypeごとに定義を持つ
  • 同一indexのtype間で同名のpropertyは同一の定義のみ許可
  • Documentは何らかのindex-typeに属する
47
53
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
47
53