015.Elasticsearch Mapping介绍

2020-06-30  本文已影响0人  CoderJed

1. mapping解析

1.1 mapping是什么

1.2 创建mapping

为空index设置mapping,index需要提前创建好

# ES6.x需要在_mapping后指定type,type可以自动创建
curl -X PUT "node01:9200/nba/_mapping/_doc" -H 'Content-Type:application/json' -d'
{
    "properties": {
        "name": {
            "type": "text"
        },
        "team_name": {
            "type": "text"
        },
        "position": {
            "type": "keyword"
        },
        "play_year": {
            "type": "keyword"
        },
        "jerse_no": {
            "type": "keyword"
        }
    }
}
'

# ES7.x
curl -X PUT "node01:9200/nba/_mapping" -H 'Content-Type:application/json' -d'
{
    "properties": {
        "name": {
            "type": "text"
        },
        "team_name": {
            "type": "text"
        },
        "position": {
            "type": "keyword"
        },
        "play_year": {
            "type": "keyword"
        },
        "jerse_no": {
            "type": "keyword"
        }
    }
}
'

创建index的时候设置mapping

# ES6.x
curl -X PUT "node01:9200/index4" -H 'Content-Type:application/json' -d'
{
  "mappings": {
    "test_type": {
      "properties": {
        "id": {
          "type": "text"
        }
      }
    }
  }
}
'
# ES7.x
curl -X PUT "node01:9200/index4" -H 'Content-Type:application/json' -d'
{
  "mappings": {
    "properties": {
      "id": {
      "type": "text"
      }
    }
  }
}
'

1.3 查看mapping

查看某个索引的mapping

# ES6.0需要指定type
curl -X GET "node01:9200/nba/_mapping/_doc"
# ES7.0不需要指定type
curl -X GET "node01:9200/nba/_mapping"

{
    "nba": {
        "mappings": {
            "_doc": {
                "properties": {
                    "jerse_no": {
                        "type": "keyword"
                    },
                    "name": {
                        "type": "text"
                    },
                    "play_year": {
                        "type": "keyword"
                    },
                    "position": {
                        "type": "keyword"
                    },
                    "team_name": {
                        "type": "text"
                    }
                }
            }
        }
    }
}

查看多个索引或者type的mapping

查看全部索引的mapping

  curl -X GET node01:9200/_all/_mapping/test_type1,test_type2
{
      "test_index2": {
          "mappings": {
              "test_type2": {
                  "properties": {
                      "name": {
                          "type": "text"
                      }
                  }
              }
          }
      },
      "test_index1": {
          "mappings": {
              "test_type1": {
                  "properties": {
                      "name": {
                          "type": "text"
                      }
                  }
              }
          }
      }
  }
  
  curl -X GET node01:9200/_mapping
  {
      "index1": {
          "mappings": {
              "_doc": {
                  "properties": {
                      "jerse_no": {
                          "type": "keyword"
                      },
                      "name": {
                          "type": "text"
                      },
                      "play_year": {
                          "type": "keyword"
                      },
                      "position": {
                          "type": "keyword"
                      },
                      "team_name": {
                          "type": "text"
                      }
                  }
              }
          }
      },
      "index2": {
          "mappings": {}
      },
      "test_index2": {
          "mappings": {
              "test_type2": {
                  "properties": {
                      "name": {
                          "type": "text"
                      }
                  }
              }
          }
      },
      "test_index": {
          "mappings": {
              "test_type": {
                  "properties": {
                      "message": {
                          "type": "text",
                          "fields": {
                              "keyword": {
                                  "type": "keyword",
                                  "ignore_above": 256
                              }
                          }
                      },
                      "user": {
                          "type": "text",
                          "fields": {
                              "keyword": {
                                  "type": "keyword",
                                  "ignore_above": 256
                              }
                          }
                      }
                  }
              }
          }
      },
      "test2": {
          "mappings": {}
      },
      "nba": {
          "mappings": {
              "_doc": {
                  "properties": {
                      "jerse_no": {
                          "type": "keyword"
                      },
                      "name": {
                          "type": "text"
                      },
                      "play_year": {
                          "type": "keyword"
                      },
                      "position": {
                          "type": "keyword"
                      },
                      "team_name": {
                          "type": "text"
                      }
                  }
              }
          }
      },
      "test_index1": {
          "mappings": {
              "test_type1": {
                  "properties": {
                      "name": {
                          "type": "text"
                      }
                  }
              }
          }
      },
      "test1": {
          "mappings": {}
      },
      "index3": {
          "mappings": {}
      }
  }

1.4 修改mapping

# 只能新增field,不能修改已有field的数据类型
curl -X PUT "node01:9200/test_index1/_mapping/test_type1" -H 'Content-Type:application/json' -d'
{
    "properties": {
        "id": {
            "type": "text"
        }
    }
}
'

# 如果要写上原有的字段,一定要和以前的数据类型一样
curl -X PUT "node01:9200/test_index1/_mapping/test_type1" -H 'Content-Type:application/json' -d'
{
    "properties": {
        "id": {
            "type": "text"
        },
        "name": {
            "type": "text"
        }
    }
}
'

# EX7.x不需要指定type
curl -X PUT "node01:9200/test_index1/_mapping" -H 'Content-Type:application/json' -d'
{
    "properties": {
        "id": {
            "type": "text"
        }
    }
}
'

1.5 mapping其他常用属性总结

PUT index_name
{
  "mappings": {
    "type_name": {
      "_source": {
          "enabled": true
      },
      "_all": {
          "enabled": true
      },
      "dynamic": "strict",
      "date_detection": false,
      "properties": {
          "field_name": {
              "type": "type_name", # 指定field的数据类型
              "analyzer": "analyzer_name", # 指定field的分词器
              "include_in_all": false # 这个field是否包含在_all field中
          }
      }
    }
  }
}

1.6 mapping模板

# 定制化type级别的mapping模板
# 此模板的意思:
# 假如插入的数据的field的名称中包含"title"这个单词,就将其value设置为text类型,且使用ik_max_word分词器分词
# 其他的field则使用dynamic设置的策略来进行数据类型和分词器的初始化
PUT index_name
{
  "mappings": {
    "type_name": {
      "dynamic_templates": [
        {
          "template_name": {
            "match": "*title*",
            "match_mapping_type": "text",
            "mapping": {
              "type": "text",
              "analyzer": "ik_max_word"
            }
          }
        }
      ] 
    }
  }
}

# 定制化index级别的mapping模板
# 此模板的意思:
# blog这个这个type的_all=true,其他type的_all=false
PUT my_index
{
  "mappings": {
    "_default_": {
      "_all": { "enabled": false}
    },
    "blog": {
      "_all": { "enabled": true}
    }
  }
}

2.ES常用的数据类型

2.1 核心数据类型

2.2 复杂数据类型

3. type底层存储

type,是一个index中用来区分类似的数据的,类似的数据,但是可能有不同的fields,而且有不同的属性来控制索引建立和分词等功能,ES底层的lucene是没有type的概念的,在document中,实际上将type作为一个document的特殊field来存储,即_type,ES通过_type来进行type的过滤和筛选,一个index中的多个type,实际上是放在一起存储的,因此一个index下,type名相同的document,其数据结构和其他设置一定是一样的。

例如以下index:

# 注意:此语句在ES6.x和ES7.x是不能执行的,因为ES6.x只能有一个type,ES7.x移除了type的概念
PUT /my_index
{
  "mappings": {
    "my_type1": {
      "properties": {
        "name": {
          "type": "keyword"
        },
        "age": {
          "type": "long"
        }
      }
    },
    "my_type2": {
      "properties": {
        "name": {
          "type": "keyword"
        },
        "sex": {
          "type": "long"
        }
      }
    }
  }
}

# 插入两条document
PUT /my_index/my_type1/1
{
  "name": "Tom",
  "age": 20
}

PUT /my_index/my_type2/2
{
  "name": "Jerry",
  "sex": 1
}

# 实际的存储为:
{
  "_type": "my_type1"
  "name": "Tom",
  "age": 20,
  "sex": null
}
{
  "_type": "my_type2"
  "name": "Jerry",
  "age": null,
  "sex": 1
}

说明:如果使用ES5.x版本,且在同一个index中使用多个type,那么将类似结构的type放在一个index下,这些type应该有多个field是相同的,假如两个type的field完全不同,却放在一个index下,那么就每条数据都会有很多的空值,会带来严重的性能问题。

上一篇下一篇

猜你喜欢

热点阅读