Help us understand the problem. What is going on with this article?

Elasticsearch index,typeについて

More than 1 year has passed since last update.

  • Elasticsearchのindexとtypeのところがわからなかったので調べました

概要

  • 検索すると見つかるindex=database, type=tableのDB用語による例えはかえってわかりにくかった、と言っている
  • (https://www.elastic.co/blog/index-vs-type)
  • 複数の構造をindexとtypeでどう使い分けるかのポイントは上に書いてある。要約すると間違えそうなので、各自で頑張って読もう。
  • 構造をどう考えたらいいのか理解するために動かしてみる
  • 2018/11/02追記)重要な参考情報:https://qiita.com/mkisono/items/4bcaedeced3b70312976

環境

  • 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に属する
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした