1. 介绍
1.1 介绍
Elasticsearch的索引通过settings设置索引的基础参数,包括分词器、分片、慢查询等等。
Elasticsearch的索引通过mappings设置索引的字段参数,可以设置根字段类型、子级字段类型、孙级字段类型,还可以设置字段的格式等等。
今天福哥要教给大家在创建索引的时候设置settings和mappings参数的方法。
2. settings
2.1 全新索引
如果是全新的创建索引的话,没有什么需要注意的地方,直接通过PUT方法将settings参数提交给ES即可。
2.1.1 data.json
{
"settings" : {
"index" : {
"number_of_shards" : "5",
"number_of_replicas" : "1",
"version" : { }
}
}
}
2.1.2 创建索引
curl -X PUT -H 'Content-Type: application/json' -d '@data.json' 'http://localhost:9200/tfapi_utf8'
2.2 导出索引
如果是导出索引的话就有一些门道了,我们先看看如何导出索引。
2.2.1 导出索引的settings参数
curl 'http://localhost:9200/tfapi_utf8/_settings?pretty=true'
2.2.2 去除自动创建字段
这里面有一些字段属于索引自动创建的,不能在创建新索引的时候带着,否则会报错。
包括:index.creation_date、index.uuid、index.version.created、index.version.upgraded、index.provided_name几个字段。
{
"tfapi_utf8" : {
"settings" : {
"index" : {
"number_of_shards" : "5",
"number_of_replicas" : "1",
"version" : { }
}
}
}
}
2.2.3 去除索引名称
最后我们需要把最外面的tfapi_utf8去掉,也就是说把settings放到最外层。
{
"settings" : {
"index" : {
"number_of_shards" : "5",
"number_of_replicas" : "1",
"version" : { }
}
}
}
2.3 创建带settings的索引
创建索引通过PUT方法提交给ES,福哥下面给出了一个例子。
2.3.1 demo.json
{
"settings" : {
"index" : {
"number_of_shards" : "5",
"number_of_replicas" : "1",
"version" : { }
}
}
}
2.3.2 创建索引
curl -X PUT -H 'Content-Type: application/json' -d '@demo.json' 'http://localhost:9200/demo?pretty=true'
3. mapping
3.1 全新索引
如果是全新的创建索引的话,没有什么需要注意的地方,直接通过PUT方法将mappings参数提交给ES即可。
3.1.1 data.json
{
"mappings" : {
"tfapi_utf8" : {
"properties" : {
"authorId" : {
"type" : "long"
},
"content" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"mCommentC" : {
"type" : "long"
},
"mDatetime" : {
"type" : "date"
},
"mId" : {
"type" : "long"
},
"mLMDatetime" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"mReadC" : {
"type" : "long"
},
"mStat" : {
"type" : "long"
},
"mTitle" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"tagIds" : {
"type" : "long"
}
}
}
}
}
3.1.2 创建索引
curl -X PUT -H 'Content-Type: application/json' -d '@data.json' 'http://localhost:9200/tfapi_utf8'
3.2 导出索引
导出索引和全新索引没有什么太大的区别,只需要进行一步操作处理一下即可。
3.2.1 导出索引的mappings参数
curl 'http://localhost:9200/tfapi_utf8/_mappings?pretty=true'
3.2.2 去除索引名称
我们只需要把最外面的tfapi_utf8去掉,也就是说把mappings放到最外层。
{
"mappings" : {
"tfapi_utf8" : {
"properties" : {
"authorId" : {
"type" : "long"
},
"content" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"mCommentC" : {
"type" : "long"
},
"mDatetime" : {
"type" : "date"
},
"mId" : {
"type" : "long"
},
"mReadC" : {
"type" : "long"
},
"mStat" : {
"type" : "long"
},
"mTitle" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"tagIds" : {
"type" : "long"
}
}
}
}
}
3.3 创建带mappings的索引
创建索引通过PUT方法提交给ES,福哥下面给出了一个例子。
3.3.1 demo.json
{
"mappings" : {
"tfapi_utf8" : {
"properties" : {
"authorId" : {
"type" : "long"
},
"content" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"mCommentC" : {
"type" : "long"
},
"mDatetime" : {
"type" : "date"
},
"mId" : {
"type" : "long"
},
"mReadC" : {
"type" : "long"
},
"mStat" : {
"type" : "long"
},
"mTitle" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"tagIds" : {
"type" : "long"
}
}
}
}
}
3.3.2 创建索引
curl -X PUT -H 'Content-Type: application/json' -d '@demo.json' 'http://localhost:9200/demo?pretty=true'
4. settings和mappings
如果要在创建索引的时候同时设置settings和mappings也简单,只要把这两个JSON拼到一起就行了。
4.1 data.json
{
"settings" : {
"index" : {
"number_of_shards" : "5",
"number_of_replicas" : "1",
"version" : { }
}
},
"mappings" : {
"tfapi_utf8" : {
"properties" : {
"authorId" : {
"type" : "long"
},
"content" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"mCommentC" : {
"type" : "long"
},
"mDatetime" : {
"type" : "date"
},
"mId" : {
"type" : "long"
},
"mReadC" : {
"type" : "long"
},
"mStat" : {
"type" : "long"
},
"mTitle" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"tagIds" : {
"type" : "long"
}
}
}
}
}
4.2 创建索引
curl -X PUT -H 'Content-Type: application/json' -d '@demo.json' 'http://localhost:9200/demo?pretty=true'
5. 总结
福哥今天带着童鞋们学习了如何在创建Elasticsearch索引的时候设置settings和mappings参数的技巧,要发挥ES的最大性能就需要对ES索引的settings和mappings的设置有一个深入的研究才可以。
https://m.tongfu.net/home/35/blog/513319.html