Elasticsearch存储数据之前需要先创建索引,类似于结构型数据库建库建表。创建索引时定义了每个字段的索引方式和数据类型,同时可以定义索引的一些行为配置,这些配置参数如下:
静态配置(只能在索引创建时设置) | 说明 |
---|---|
number_of_shards | 主分片数,默认为5 |
shard.check_on_startup | 索引打开前是否检查分片是否有损坏,参数可以是: false :不检查,默认值 checksum :检查物理损坏 true:检查物理和逻辑损坏,这将消耗大量内存和CPU fix:检查物理和逻辑损坏,有损坏的分片将被集群自动删除,这可能导致数据丢失,es7.0移除 |
routing_partition_size | 自定义路由值可以转发的目的分片数,默认为 1,此值必须小于number_of_shards |
codec | 默认使用LZ4压缩方式存储数据,也可以设置为 best_compression,它使用 DEFLATE 方式以牺牲字段存储性能为代价来获得更高的压缩比例 |
静态配置(可以随时修改) | 说明 |
---|---|
number_of_replicas | 每个主分片的副本数,默认为 1 |
auto_expand_replicas | 基于可用节点的数量自动分配副本数量,默认为 false |
refresh_interval | 执行刷新操作的频率,决定索引的最近更改多久之后可以被搜索到。默认为 1s,可以设置为 -1 禁用刷新 |
max_result_window | 设置索引搜索的 from+size 的最大值,默认为 10000 |
max_rescore_window | 设置索引搜索中 rescore 的 window_size 的最大值 |
blocks.read_only | 索引和索引元数据是否为只读 |
blocks.read | 设置为 true 可禁用对索引的读取操作 |
blocks.write | 设置为 true 可禁用对索引的写入操作 |
blocks.metadata | 设置为 true 可禁用索引元数据的读取和写入 |
max_refresh_listeners | 索引的每个分片上可用的最大刷新侦听器数 |
常用API:
1、查看指定索引信息:
GET http://$user:$passwd@$host:$port/$index
2、创建索引:
PUT http://$user:$passwd@$host:$port/$index
{
"settings": {
"index": {
"number_of_shards": 1, //索引的主分片个数,默认值是5,这个配置在索引创建后不能修改
"number_of_replicas" : 0 //每个主分片的副本分片个数,默认值是1,对于活动的索引库,这个配置可以随时修改
}
},
"aliases": {
索引别名: {} //务必要设置索引别名,方便以后数据平滑迁移
},
"mappings": {
"$type": {
"dynamic": "strict", //不允许自动索引不存在字段的数据
"_source": {
"includes":["字段名2","字段名3"] //_source中只包含字段2和字段3
},
"_all": {
"enabled": false //禁用_all,高版本不允许设置为true
},
"properties": {
字段名1: {
"type": "text",
"analyzer": "ik_smart", //默认使用ik_smart分词
"fields": {
"keyword": { //定义分析器:字段名1.keyword
"type": "keyword",
"ignore_above": 64
},
"completion": {
"type": "completion", //用于completion suggester
"analyzer": "ik_smart"
}
}
},
字段名2: {
"type": "boolean"
},
字段名3: {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
字段名4: {
"type": "long"
}
}
}
}
}
3、查看指定索引类型下的映射信息:
GET http://$user:$passwd@$host:$port/$index/_mappings/$type
4 、修改指定索引配置:
PUT http://$user:$passwd@$host:$port/$index/_settings
{
"refresh_interval": "2s"
}
5、新增别名:
PUT http://$user:$passwd@$host:$port/_aliases
{
"actions": [
{
"add": {
"index": 索引名,
"alias": 索引别名
}
}
]
}
6、删除指定别名:
PUT http://$user:$passwd@$host:$port/_aliases
{
"actions": [
{
"remove": {
"index": 索引名,
"alias": 索引别名
}
}
]
}
7、别名重新指向:
PUT http://$user:$passwd@$host:$port/_aliases
{
"actions": [
{
"remove": {
"index": 索引名,
"alias": 旧索引别名
}
}
{
"add": {
"index": 索引名,
"alias": 新索引别名
}
}
]
}
8、增加别名的同时增加过滤条件,用不同的别名查出不同的数据:
PUT http://$user:$passwd@$host:$port/_aliases
{
"actions": [
{
"add": {
"index": 索引名,
"alias": 索引别名,
"filter":{
"term":{
"字段名":"过滤的值"
}
}
}
}
]
}
9、新增属性:
PUT http://$user:$passwd@$host:$port/$index/_mappings/$type
{
"properties": {
新增的属性名: {
"type": "keyword"
}
}
}
10、新增字段:
PUT http://$user:$passwd@$host:$port/$index/_mappings/$type
{
"properties": {
原有的属性名: {
"type": "keyword", //原有的type
"fields": {
新增的字段名: {
"type": "completion",
"analyzer": "ik_smart"
}
}
}
}
}
11、删除索引,可以有多种方式:
DELETE http://$user:$passwd@$host:$port/$index
DELETE http://$user:$passwd@$host:$port/$index0,$index1
DELETE http://$user:$passwd@$host:$port/$index*
DELETE http://$user:$passwd@$host:$port/_all
DELETE http://$user:$passwd@$host:$port/*
索引维护更多详情请参考:https://www.elastic.co/guide/en/elasticsearch/reference/6.5/indices.html