ES-索引库

数据准备

本次学习涵盖ES简单查询,聚合查询,所以在创建测试库时会可以涵盖一些个性化字段,用于学习搜索用法

索引创建

几个疑问

1.能否用中文命名

安排:我用"蓝闪test",中英文混合l来创建,看看会怎么样
image

使用postman测试,得到成功反馈。

2.重复创建会怎样

相同条件,再次调用新建索引库接口,得到如下反馈,index [蓝闪test/iV3Q_EKUQdOP2uEwpro9ww] already exists",告知索引库已存在。

image

改下名字"蓝闪Test",再插

image

哦嚯,不合法,提示只能必须是小写?英文只能小写?验证一波

排除中文干扰,起一个新名字“MY_FIRST_INDEX”,再次测试,还是报不合法,必须小写
image

试试中间偷偷改一个字母,看看行不行,ES很强啊,不按规矩来就不给你建库。
image

不折腾了,全小写再来一遍,哟,成功了。

image

3.怎样才是规范的命名

由于ElasticSearch使用索引名称作为磁盘上的目录名称,这些名称必须符合不同操作系统的约定。
同时需遵守下列规则:

1、索引名必须为小写字母

2、不能包括 , /, *, ?, “, <, >, |, 空格, 逗号, #

3、7.0版本之后不能使用冒号:

4、不能以这些字符 -, _, + 开头

5、不能包括 . 或 …

6、长度不能超过 255 个字符

以上结论来自网上,好奇心害死猫,我再来验证一下以上结论

结论验证

第一条我们已经试过了,来看第二条:

image

果然报错,而且还告诉我们不能以'-','+'开头。官方提示,我信了。

验证第三条(我的测试es版本号是7.8.1,符合要求):

image

把':'放尾巴试试

image

嗯,看来是正确的结论,包含':'就不行

接着干,验证第五条

把索引库名字改成myfirst_in.dex,成功建库,???,教程翻车了吗?

image

再试试...,也成功了

image

换成句号,试试,也成功了。

image

去管理面板看看,确实成功了,所以此条规则,在7.8.1版本是支持的,验证失败。

image

继续验证,第六条

最大支持255?那么就找临界值去验证

image

索引长度254,创建成功
image

索引长度255,创建成功

image

索引长度256,创建失败,第六条验证成功

image

————————————————

在网上还找到了这么一道题:

image

想必你可以得出答案了吧。 是的,答案是A。

image

直接插入数据(不设id插入)

http://127.0.0.1:9200/蓝闪test/_doc
{
 "name":"孙悟空",
 "profession": "战士",
 "team":1,
 "harm":450,
 "friends":["猪八戒","哪吒","嫦娥"],
 "createTime":"2022-12-12 18:18:18",
 "price":18888.0
}

// 返回结果
{
    "_index": "蓝闪test",
    "_type": "_doc",
    "_id": "xEhtd4UBiEnfei9vqfy1",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}

image

完整索引库结构

{
    "version": 4,
    "mapping_version": 2,
    "settings_version": 1,
    "aliases_version": 1,
    "routing_num_shards": 1024,
    "state": "open",
    "settings": {
        "index": {
            "creation_date": "1672744003690",
            "number_of_shards": "1",
            "number_of_replicas": "1",
            "uuid": "iV3Q_EKUQdOP2uEwpro9ww",
            "version": {
                "created": "7080199"
            },
            "provided_name": "蓝闪test"
        }
    },
    "mappings": {
        "_doc": {
            "properties": {
                "profession": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "ignore_above": 256,
                            "type": "keyword"
                        }
                    }
                },
                "harm": {
                    "type": "long"
                },
                "createTime": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "ignore_above": 256,
                            "type": "keyword"
                        }
                    }
                },
                "price": {
                    "type": "float"
                },
                "name": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "ignore_above": 256,
                            "type": "keyword"
                        }
                    }
                },
                "team": {
                    "type": "long"
                },
                "friends": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "ignore_above": 256,
                            "type": "keyword"
                        }
                    }
                }
            }
        }
    },
    "aliases": [],
    "primary_terms": {
        "0": 1
    },
    "in_sync_allocations": {
        "0": [
            "PRrAzvhiTdKDeXA5gxbNZw"
        ]
    },
    "rollover_info": {}
}

查看数据

image

设置id插入

http://127.0.0.1:9200/蓝闪test/_doc/1
{
 "name":"百里守约",
 "profession": "射手",
 "team":1,
 "harm":550,
 "friends":["百里玄策","凯","花木兰"],
 "createTime":"2022-12-13 18:18:18",
 "price":13888.0
}

// 返回结果
{
    "_index": "蓝闪test",
    "_type": "_doc",
    "_id": "1",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 1,
    "_primary_term": 1
}

查看数据

image

posted @ 2023-01-03 20:58  忘崽牛仔  阅读(94)  评论(0编辑  收藏  举报