Skip to content

Instantly share code, notes, and snippets.

@bwangelme
Created May 6, 2020 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bwangelme/bd49c805137b353787a20cc39a3d4113 to your computer and use it in GitHub Desktop.
Save bwangelme/bd49c805137b353787a20cc39a3d4113 to your computer and use it in GitHub Desktop.
ES 中的 index template 和 Dynamic Template
PUT ttemplate/_doc/1
{
"someNumber": "1",
"someDate": "2019/02/03"
}
GET ttemplate/_mapping
GET _cat/indices
# 设置索引的模板
PUT _template/template_default
{
"index_patterns": ["*"],
"order": 0,
"version": 1,
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1
}
}
# 开启数字检测,关闭日期检测
PUT _template/template_test
{
"index_patterns": ["test*"],
"order": 1,
"settings": {
"number_of_shards": 1,
"number_of_replicas": 2
},
"mappings": {
"date_detection": false,
"numeric_detection": true
}
}
GET /_template/template_default
# 可以使用通配符获取所有索引模板
GET /_template/temp*
PUT testtemplate/_doc/1
{
"someNumber": 1,
"someDate": "2019/01/03"
}
GET testtemplate/_mapping
GET testtemplate/_settings
PUT testmy
{
"settings": {
"number_of_replicas": 5
}
}
PUT testmy/_doc/1
{
"key": "value"
}
POST testmy/_doc
{
"key1": "value1"
}
GET testmy/_search
GET testmy/_settings
# dynamic template
# 为索引的具体字段设置索引
PUT my_index
{
"mappings": {
"dynamic_templates": [
{
"strings_as_boolean": {
"match_mapping_type": "string",
"match": "is*",
"mapping": {
"type": "boolean"
}
}
},
{
"strings_as_keywords": {
"match_mapping_type": "string",
"mapping": {
"type": "keyword"
}
}
}
]
}
}
PUT my_index/_doc/1
{
"firstName": "Ruan",
"isVIP": "true"
}
GET my_index/_mapping
DELETE my_index
# 利用 dynamic template 新建字段
# 将name对象中除 middle 之外的所有字段都复制到 fullname 字段中
PUT my_index
{
"mappings": {
"dynamic_templates": [
{
"full_name": {
"path_match": "name.*",
"path_unmatch": "*.middle",
"mapping": {
"type": "text",
"copy_to": "full_name"
}
}
}
]
}
}
PUT my_index/_doc/1
{
"name": {
"first": "John",
"middle": "Winston",
"last": "Lennon"
}
}
# 可以通过 full_name 查找文档
GET my_index/_search?q=full_name:Winston
GET my_index/_search?q=full_name:Lennon
# 疑问,这个 full_name 是一个字段呢?为什么返回结果中没有它
GET my_index/_search
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment