Mapping - accgetter/Elasticsearch GitHub Wiki

Mapping基本

特定のindexにmapping

PUT /{index}
{
    "mappings": {
        "{type}": {
            "properties" : {
                "xxx" : { "type" : "string"}
            }
        }
    }
}

複数indexのtype縛りでmapping {index}の部分はワイルドカードで設定

PUT /{index}/_mapping/{type}
{
      "properties": {
        "xxx": {
          "type": "string"
        }
      }
}

文字列の完全一致での検索をするためのMapping

下記のようなマッピングをして、analyzed をしないようにする

PUT /_template/template_1
{
    "template" : "*",
    "mappings" : {
        "_default_" : {
            "_source" : { "compress" : true },
            "properties" : {
                "referer" : { "type" : "string", "index" : "not_analyzed" },
                "url" : { "type" : "string", "index" : "not_analyzed" }
            }
        }
    }
}

_default_ の場合すべてのtypeに適用される Default Mapping "template" : "*" Index Templates "template_1" という名前でmapping情報を定義しておき、"template" : "*"のワイルドカードは、 index名の指定になっている。この場合どんな名前のindexが作成されても適用される。

mapping のやり直し

以下のように特定のindexにのみmappingしなおしてerrorが出るときは

{"error":{"root_cause":[{"type":"merge_mapping_exception","reason":"Merge failed with failures {[mapper [referer] has different [index] values, mapper [referer] has different [doc_values] values, cannot change from disabled to enabled, mapper [referer] has different [analyzer], mapper [referer] is used by multiple types. Set update_all_types to true to update [omit_norms] across all types., mapper [referer] is used by multiple types. Set update_all_types to true to update [search_analyzer] across all types., mapper [referer] is used by multiple types. Set update_all_types to true to update [search_quote_analyzer] across all types., mapper [url] has different [index] values, mapper [url] has different [doc_values] values, cannot change from disabled to enabled, mapper [url] has different [analyzer], mapper [url] is used by multiple types. Set update_all_types to true to update [omit_norms] across all types., mapper [url] is used by multiple types. Set update_all_types to true to update [search_analyzer] across all types., mapper [url] is used by multiple types. Set update_all_types to true to update [search_quote_analyzer] across all types.]}"}],"type":"merge_mapping_exception","reason":"Merge failed with failures {[mapper [referer] has different [index] values, mapper [referer] has different [doc_values] values, cannot change from disabled to enabled, mapper [referer] has different [analyzer], mapper [referer] is used by multiple types. Set update_all_types to true to update [omit_norms] across all types., mapper [referer] is used by multiple types. Set update_all_types to true to update [search_analyzer] across all types., mapper [referer] is used by multiple types. Set update_all_types to true to update [search_quote_analyzer] across all types., mapper [url] has different [index] values, mapper [url] has different [doc_values] values, cannot change from disabled to enabled, mapper [url] has different [analyzer], mapper [url] is used by multiple types. Set update_all_types to true to update [omit_norms] across all types., mapper [url] is used by multiple types. Set update_all_types to true to update [search_analyzer] across all types., mapper [url] is used by multiple types. Set update_all_types to true to update [search_quote_analyzer] across all types.]}"},"status":400}

indexのバックアップをとって、削除、そしてmapping、そしてimportでいけました。

curl -XPUT 'localhost:9200/point-2016.02.04' -d \
'{
  "mappings": {
    "access": {
      "properties": {
        "url": { 
          "type": "string",
          "index" : "not_analyzed"
        },
        "referer": { 
          "type": "string",
          "index" : "not_analyzed"
        }
      }
    },
    "ap": {
      "properties": {
        "url": { 
          "type": "string",
          "index" : "not_analyzed"
        },
        "referer": { 
          "type": "string",
          "index" : "not_analyzed"
        }
      }
    }
  }
}
'

mapping の確認方法

$ curl -XGET 'localhost:9200/_all/_mapping'