elasticSearch(二):索引的多种玩法

680 阅读4分钟

一、索引

介绍

索引是一堆相同结构的文档的集合,不同的索引代表不同的业务类型数据。在6.X版本之前,索引类似于关系型数据库中的一个库,它的type类似于表。而在6.X版本之后不再使用type,在8.0版本开始彻底移除了Type。

创建

创建命令

1、可以采用PUT命令,只定义索引名创建索引,这样定义一些设置会取默认值,例如分片为1,副本为1,刷新时间为1s等;例:

PUT index_test

2、在创建索引的过程中携带一些索引的设置;例:

PUT index_test
{
  "settings": {
    "number_of_replicas": 1,
    "number_of_shards": 2,
    "refresh_interval": "1s"
  },
  "mappings": {
    "properties": {
      "msg":{
        "type": "text",
        "analyzer": "standard"
      }
    }
  },
  "aliases":{
    "mytest":{
  }
  }
}

其中settings.number_of_replicas=1代表该索引有一个副本,settings.number_of_shards=2代表该索引有2个分片,而mappings中的值则代表该索引的映射,在后面映射章节详细介绍。aliases则代表该索引的别名,例如上述示例表示该索引别名为mytest。

命名规则

  • 只能使用小写字母;
  • 不能包含“ ” \ / * ? < > | ` , # : 空格等特殊符号;
  • 不能以- —— + 作为开始字符;
  • 不可以命名为. ..
  • 不能超过255个字节;
  • 尽量采用英文。

更改

更改命令

PUT index_test/_settings
{
  "settings": {
    "number_of_replicas": 1,
    "refresh_interval": "1s"
  }
}

更改规则

对于索引一些属性是可以更改的,也有一些一旦创建就不能更改的。定义:

静态设置:只允许在创建索引时或者针对已关闭的索引进行设置。
动态设置:可以借助更新索引来动态设置,设置后立刻生效。

例如:

  • settings.number_of_shards索引分片属性,只有在创建索引时生效,无法动态更改。如果想更改只能进行reindex操作。它是一个静态设置,执行更改操作会提示:
{
  "error" : {
    "root_cause" : [
      {
        "type" : "illegal_argument_exception",
        "reason" : "Can't update non dynamic settings [[index.number_of_shards]] for open indices [[index_test/36jDtKNpTlSsOrlCVaWUdw]]"
      }
    ],
    "type" : "illegal_argument_exception",
    "reason" : "Can't update non dynamic settings [[index.number_of_shards]] for open indices [[index_test/36jDtKNpTlSsOrlCVaWUdw]]"
  },
  "status" : 400
}
  • settings.refresh_interval它代表数据的刷新时间,也就是生成一个segment的时间,使得数据可以被搜索到,如果业务实时性要求不高便可以把它设置为一个较大的数值。它是一个动态设置,执行更改操作:
{
  "acknowledged" : true
}

动态设置、静态设置分别可以设置哪些参数可以自行查阅官方文档。

删除

删除命令

1、直接指定索引删除

DELETE index_test

2、结合delete_by_query来删除,保留索引结构

POST index_test/_delete_by_query
{
  "query":{
    "match_all":{
      
    }
  }
}
  • 方式一为物理删除,效率更高,删除完空间立刻释放。
  • 方式二为逻辑删除,它会形成新的数据,在后续合并段的时候进行真正的数据删除。

模板

当数据量很大,需要根据日期进行分片时,例如我们现在记录企微聊天记录的表就这样进行分片。而每次都手动创建或者脚本创建都很麻烦,此时索引模板就派上用场了。

索引模板命令

1、普通模板

PUT _index_template/test_template
{
  "index_patterns": [
    "tem_*"
  ],
  "template": {
    "settings": {
      "number_of_shards": 2
    },
    "mappings": {
      "properties": {
        "name": {
          "type": "text"
        }
      }
    }
  }
}

POST tem_01/_doc
{
  "phone":"13199990000"
}

GET tem_01/_mapping

GET tem_01/_settings


第一个命令_index_template代表创建索引模板,匹配所有以tem_开头的索引;第二个命令是对索引tem_01插入一条数据,同时会自动创建一个索引;第三、四个命令是查看这个索引。 结果:

{
  "tem_01" : {
    "mappings" : {
      "properties" : {
        "name" : {
          "type" : "text"
        },
        "phone" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}

{
  "tem_01" : {
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "2",
        "provided_name" : "tem_01",
        "creation_date" : "1720701657066",
        "number_of_replicas" : "1",
        "uuid" : "xK9eqi2VTzSVcLn-SZELMA",
        "version" : {
          "created" : "7171899"
        }
      }
    }
  }
}

可以看到刚刚通过模板设置的属性都可以查到。

2、组件模板

顾名思义就是通过拆分为各个组件,可以进行单独使用。

PUT _component_template/component_mapping_tem1
{
  "template": {
    "mappings": {
      "properties": {
        "name":{
          "type": "text"
        },
        "age":{
          "type": "keyword"
        }
      }
    }
  }
}

PUT _component_template/component_setting_tem1
{
  "template": {
    "settings": {
      "number_of_replicas": 3
    }
  }
}

PUT _index_template/component_index_01
{
  "index_patterns": [
    "tem01_*"
  ],
  "composed_of": [
    "component_setting_tem1",
    "component_mapping_tem1"
  ]
}

前两个命令是分别创建一个mapping组件和一个setting组件,第三个命令是创建一个索引模板引用上述两个组件。最终的效果和第一种方式相同,但更加灵活。

别名

数据根据日期进行切分存储在不同的索引中,每次查询的时候总不能一直更改代码去查询不同的索引,希望代码中的索引不变可以动态的去路由到具体索引中。此时就可以使用索引别名来完成该功能。

命令

PUT temp02
{
  "aliases": {
    "temp03": {}
  },
  "mappings": {
    "properties": {
      "name":{
        "type": "text"
      }
    }
  }
}

GET temp02/_mapping

GET temp03/_mapping

第一命令是创建一个索引temp02,并设置别名为temp03。接下来分别查询索引和索引的别名。结果都是同样的。

{
  "temp02" : {
    "mappings" : {
      "properties" : {
        "name" : {
          "type" : "text"
        }
      }
    }
  }
}