PUT test_index
当elasticsearch返回true时,就代表着我们在elasticsearch中创建了一个名为test_index的索引已经成功,同时在创建索引时没有为该索引指定任何字段。
然后我们查看该索引的详细信息:
GET test_index
{
"test_index": {
"aliases": {},
"mappings": {
"dynamic_templates": [
{
"message_full": {
"match": "message_full",
"mapping": {
"fields": {
"keyword": {
"ignore_above": 2048,
"type": "keyword"
}
},
"type": "match_only_text"
}
}
},
{
"message": {
"match": "message",
"mapping": {
"type": "match_only_text"
}
}
},
{
"strings": {
"match_mapping_type": "string",
"mapping": {
"type": "keyword"
}
}
}
]
},
"settings": {
"index": {
"refresh_interval": "10s",
"indexing": {
"slowlog": {
"threshold": {
"index": {
"warn": "200ms",
"trace": "20ms",
"debug": "50ms",
"info": "100ms"
}
},
"source": "1000"
}
},
"translog": {
"sync_interval": "5s",
"durability": "async"
},
"provided_name": "test_index",
"max_result_window": "65536",
"creation_date": "1700017444301",
"unassigned": {
"node_left": {
"delayed_timeout": "5m"
}
},
"number_of_replicas": "1",
"uuid": "YAIUJ64oTYuQpEjMvQK_Rg",
"version": {
"created": "8080199"
},
"routing": {
"allocation": {
"include": {
"_tier_preference": "data_hot,data_warm,data_cold"
}
}
},
"search": {
"slowlog": {
"threshold": {
"fetch": {
"warn": "200ms",
"trace": "50ms",
"debug": "80ms",
"info": "100ms"
},
"query": {
"warn": "500ms",
"trace": "50ms",
"debug": "100ms",
"info": "200ms"
}
}
}
},
"number_of_shards": "1"
}
}
}
}
我们可以看到在索引的mappings中,除了动态模板自带的mapping配置之外,没有任何其他索引mapping。同时还适配了default@template模板的索引参数。
现在我们对该索引手动插入一条数据。
PUT /test_index/_doc/1?pretty
{
"name":"张三",
"age":23,
"remark":"热爱学习,热爱读书,热爱生活"
}
此时elasticsearch回返回以下信息,告诉我们在test_index索引中已经成功创建了一条"_id"为1的数据,该数据的"_version"也为1,在2个分片上执行成功,失败0个分片。
现在我们重新执行GET test_index
,我们发现在索引的mapping中,多出了properties部分,在properties中已经有了"name","age","remark"三个字段。同时elasticsearch还对这三个字段自动映射了字段类型。
{
"test_index": {
"aliases": {},
"mappings": {
"dynamic_templates": [
{
"message_full": {
"match": "message_full",
"mapping": {
"fields": {
"keyword": {
"ignore_above": 2048,
"type": "keyword"
}
},
"type": "match_only_text"
}
}
},
{
"message": {
"match": "message",
"mapping": {
"type": "match_only_text"
}
}
},
{
"strings": {
"match_mapping_type": "string",
"mapping": {
"type": "keyword"
}
}
}
],
"properties": {
"age": {
"type": "long"
},
"name": {
"type": "keyword"
},
"remark": {
"type": "keyword"
}
}
}
}
此时我们可以通过查询对我们插入的数据进行检索。可以看到elasticsearch已经为我们返回了匹配到的数据。
GET test_index/_search
{
"query": {
"term": {
"age": {
"value": "23"
}
}
}
}
当我们使用另一种查询,使用match来查询当前数据时,我们发现elasticsearch并没有为我们返回数据。
GET test_index/_search
{
"query": {
"match": {
"remark": "读书"
}
}
}
通过排查索引mapping我们发现,elasticsearch为remark字段推断的类型为keyword,通过之前的文章,我们知道keyword是不能被分词的。所以我们通过match查询,无法查到数据。那么我们在创建索引时,应该如何操作呢?
"properties": {
"age": {
"type": "long"
},
"name": {
"type": "keyword"
},
"remark": {
"type": "keyword"
}
}
我们在创建索引时,对索引的字段类型进行手动的约束。
PUT /test_index_new
{
"mappings": {
"properties": {
"name": { "type": "text" },
"age": { "type": "integer" },
"remark": { "type": "text" }
}
}
}
我们在PUT一条相同的数据后,在对test_index_new索引进行match查询,我们发现已经可以正常查到数据。
GET test_index_new/_search
{
"query": {
"match": {
"remark": "读书"
}
}
}
优点:
缺点:
思考:
在特定业务场景下,我们手动创建索引能够更好的满足我们的业务需求,当业务激增时,需要手动创建的索引越来越多,是否有更便捷的方式让我们进行索引的创建呢?
我们可以通过定义索引模板,来针对不同业务的索引进行索引参数的预定义与mapping映射的预定义。当索引在创建时,会通过读取模板中的"index_patterns"来对索引进行相应的模板适配。以满足不同业务场景下,不同的索引需求。
在下面的代码中,我们创建了一个名为test_template的模板,模板的优先级为1,主要适配"test"开头的索引。在索引settings中,我们设置了索引主分片数为3,副本数为1。在索引mappings中 我们约束了"name","age","remark"三个字段的字段类型。当elasticsearch返回true后,则代表着模板创建完成。
PUT _template/test_template
{
"order": 1,
"index_patterns": ["test*"],
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings": {
"_source": {
"enabled": false
},
"properties": {
"name": { "type": "text" },
"age": { "type": "integer" },
"remark": { "type": "text" }
}
}
}
我们现在重新创建一个名为test_template_index的索引,且什么都不指定,让我们来观察一下索引的mapping情况。
PUT test_template_index
此时我们在索引mapping中发现,这个索引已经自动将我们之前在模板中为索引名是"test"打头的索引参数与mapping自动进行了适配。极大程度的方便了我们对于索引的创建与管理。
{
"test_template_index": {
"aliases": {},
"mappings": {
"_source": {
"enabled": false
},
"dynamic_templates": [
{
"message_full": {
"match": "message_full",
"mapping": {
"fields": {
"keyword": {
"ignore_above": 2048,
"type": "keyword"
}
},
"type": "match_only_text"
}
}
},
{
"message": {
"match": "message",
"mapping": {
"type": "match_only_text"
}
}
},
{
"strings": {
"match_mapping_type": "string",
"mapping": {
"type": "keyword"
}
}
}
],
"properties": {
"age": {
"type": "integer"
},
"name": {
"type": "text"
},
"remark": {
"type": "text"
}
}
},
"settings": {
"index": {
"refresh_interval": "10s",
"indexing": {
"slowlog": {
"threshold": {
"index": {
"warn": "200ms",
"trace": "20ms",
"debug": "50ms",
"info": "100ms"
}
},
"source": "1000"
}
},
"translog": {
"sync_interval": "5s",
"durability": "async"
},
"provided_name": "test_template_index",
"max_result_window": "65536",
"creation_date": "1700030711762",
"unassigned": {
"node_left": {
"delayed_timeout": "5m"
}
},
"number_of_replicas": "1",
"uuid": "cTiJp67xThSwpavbV3Hlog",
"version": {
"created": "8080199"
},
"routing": {
"allocation": {
"include": {
"_tier_preference": "data_hot,data_warm,data_cold"
}
}
},
"search": {
"slowlog": {
"threshold": {
"fetch": {
"warn": "200ms",
"trace": "50ms",
"debug": "80ms",
"info": "100ms"
},
"query": {
"warn": "500ms",
"trace": "50ms",
"debug": "100ms",
"info": "200ms"
}
}
}
},
"number_of_shards": "3"
}
}
}
}
优点:
缺点:
原因与排查思路:
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。