Elasticsearch 5.4 Indices(索引) API

简介: 前言一索引管理1 创建索引2 删除索引3 查看索引信息4 索引是否存在5 关闭打开索引6 索引收缩7 翻滚索引二mapping管理1 设置mapping2 查看mapping3 获取字...


前言

声明:本博客根据ELasticsearch官网文档翻译整理,转载请注明出处:http://blog.csdn.net/napoay


索引API可以用于管理单个索引、索引设置、别名、映射和索引模板。

一、索引管理

1.1 创建索引

创建索引

PUT twitter

默认分片为5,副本为1.

创建索引并指定分片数和副本数:

PUT twitter
{
    "settings" : {
        "index" : {
            "number_of_shards" : 3, 
            "number_of_replicas" : 2 
        }
    }
}

或者简写为:

PUT twitter
{
    "settings" : {
        "number_of_shards" : 3,
        "number_of_replicas" : 2
    }
}

创建索引并指定mapping:

PUT test
{
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
        "type1" : {
            "properties" : {
                "field1" : { "type" : "text" }
            }
        }
    }
}

1.2 删除索引

DELETE /twitter

1.3 查看索引信息

查看所有的settings、别名、mapping,命令:

GET /twitter

添加参数过滤信息:

GET twitter/_settings,_mappings

1.4 索引是否存在

如果想知道集群中是否存在某个索引,可以使用以下命令:

HEAD twitter

如果存在,返回状态码200:

200 - OK

如果不存在,返回状态码404:

404 - Not Found

1.5 关闭/打开索引

对于不使用的索引,关闭索引可以节省开销,但是索引关闭以后读写操作是无法进行的。

打开索引:

POST /my_index/_close

关闭索引:

POST /my_index/_open

可以同时关闭多个索引,如果其中有索引不存在会报异常,可以使用ignore_unavailable=true参数忽略不存在索引。

1.6 索引收缩

shrink index AP可以把一个索引变成一个更少分片的索引,但是收缩后的分片数必须是原始分片数的因子(因子就是所有可以整除这个数的数,不包括这个数自身),比如有8个分片的索引可以收缩为4、2、1,有15个分片的索引可以收缩为5、3、1,如果分片数为素数(7、11等),那么只能收缩为1个分片。 收缩索引之前,索引中的每个分片都要在同一个节点上。

收缩索引的完成过程:

  • 首先,创建了一个新的目标索引,设置与源索引相同,但新索引的分片数量较少。
  • 然后把源索引的段到硬链接到目标索引。(如果文件系统不支持硬链接,那么所有段都被复制到新索引中,这是一个耗费更多时间的过程。)
  • 最后,新的索引恢复使用,好像它是一个刚刚重新开放的封闭索引。

搜索之前,使索引为只读状态并使分片重新分配到同一个节点:

PUT /my_source_index/_settings
{
  "settings": {
    "index.routing.allocation.require._name": "shrink_node_name", 
    "index.blocks.write": true 
  }
}

设置目标索引名和分片数,别名可选:

POST my_source_index/_shrink/my_target_index
{
  "settings": {
    "index.number_of_replicas": 1,
    "index.number_of_shards": 1, 
    "index.codec": "best_compression" 
  },
  "aliases": {
    "my_search_indices": {}
  }
}

1.7 翻滚索引

二、mapping管理

2.1 设置mapping

put mapping可以给一个已存在的索引增加type的mapping,也可以给一个存在的type增加字段的mapping。

PUT twitter 
{
  "mappings": {
    "tweet": {
      "properties": {
        "message": {
          "type": "text"
        }
      }
    }
  }
}

PUT twitter/_mapping/user 
{
  "properties": {
    "name": {
      "type": "text"
    }
  }
}

PUT twitter/_mapping/tweet 
{
  "properties": {
    "user_name": {
      "type": "text"
    }
  }
}

一般情况下字段的mapping设置是不可以更新的,有几个特例除外:

  • properties嵌套属性可以新增
  • ignore_above 参数的值可以更新
PUT my_index 
{
  "mappings": {
    "user": {
      "properties": {
        "name": {
          "properties": {
            "first": {
              "type": "text"
            }
          }
        },
        "user_id": {
          "type": "keyword"
        }
      }
    }
  }
}

PUT my_index/_mapping/user
{
  "properties": {
    "name": {
      "properties": {
        "last": { 
          "type": "text"
        }
      }
    },
    "user_id": {
      "type": "keyword",
      "ignore_above": 100 
    }
  }
}

2.2 查看mapping

查看一个索引的mapping:

GET /twitter/_mapping

查看一个索引的一个type的mapping:

GET /twitter/_mapping/tweet

查看所有索引的mapping:

GET /_mapping

或者:

GET /_all/_mapping

2.3 获取字段mapping

get field mapping api可以查看索引的一个或多个字段的mapping,设置创建一个索引做测试:

PUT publications
{
    "mappings": {
        "article": {
            "properties": {
                "id": { "type": "text" },
                "title":  { "type": "text"},
                "abstract": { "type": "text"},
                "author": {
                    "properties": {
                        "id": { "type": "text" },
                        "name": { "type": "text" }
                    }
                }
            }
        }
    }
}
GET publications/_mapping/article/field/title
GET publications/_mapping/article/field/id
GET publications/_mapping/article/field/author.id

2.4 类型是否存在

查看索引是否存在某个type:

HEAD twitter/_mapping/tweet

返回值为200说明存在,404说明不存在。

三、别名管理

3. 1 索引别名设置

可以给一个或多个索引设置别名,但是别名不能和已有索引名称相同。
给索引名为test1的索引设置别名为alias1:

POST /_aliases
{
    "actions" : [
        { "add" : { "index" : "test1", "alias" : "alias1" } }
    ]
}

移除别名:

POST /_aliases
{
    "actions" : [
        { "remove" : { "index" : "test1", "alias" : "alias1" } }
    ]
}

更新别名的映射关系就是先移除再添加:

POST /_aliases
{
    "actions" : [
        { "remove" : { "index" : "test1", "alias" : "alias1" } },
        { "add" : { "index" : "test2", "alias" : "alias1" } }
    ]
}

也可以同时给多个索引设置同一个别名:

POST /_aliases
{
    "actions" : [
        { "add" : { "index" : "test1", "alias" : "alias1" } },
        { "add" : { "index" : "test2", "alias" : "alias1" } }
    ]
}

也可以使用通配符,一下所有以test开头的索引都设置别名为all_test_indices:

POST /_aliases
{
    "actions" : [
        { "add" : { "index" : "test*", "alias" : "all_test_indices" } }
    ]
}

四、索引配置

4.1 获取索引设置

查看索引的settings:

GET /twitter/_settings

查看多个索引的settings:

GET /twitter,kimchy/_settings

GET /_all/_settings

GET /log_2013_*/_settings

4.2 更新索引设置

修改副本:

PUT /twitter/_settings
{
    "index" : {
        "number_of_replicas" : 2
    }
}

修改settings用于提高Bulk的导入性能,bulk之前设置刷新时间为-1,也就是bulk导入期间不再刷新:

PUT /twitter/_settings
{
    "index" : {
        "refresh_interval" : "-1"
    }
}

bulk导入之后恢复刷新时间并强制段合并

PUT /twitter/_settings
{
    "index" : {
        "refresh_interval" : "1s"
    }
}
POST /twitter/_forcemerge?max_num_segments=5

4.3 分析器

查看分词标准分词结果:

GET _analyze
{
  "analyzer" : "standard",
  "text" : "this is a test"
}

查看IK分词结果:

GET _analyze
{
  "analyzer" : "ik_smart",
  "text" : "北京今天局地高温"
}

4.4 索引模板

索引模板可以自动匹配新创建的索引。

PUT _template/template_1
{
  "template": "te*",
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "type1": {
      "_source": {
        "enabled": false
      },
      "properties": {
        "host_name": {
          "type": "keyword"
        },
        "created_at": {
          "type": "date",
          "format": "EEE MMM dd HH:mm:ss Z YYYY"
        }
      }
    }
  }
}

删除索引模板:

DELETE /_template/template_1

查看索引模板:

GET /_template

查看一个或多个:

GET /_template/template_1
GET /_template/template_1,template_2

五、监控管理

5.1 索引统计信息

GET /_stats
GET /index1,index2/_stats

以上命令返回的索引的相关信息非常多,可以通过参数过滤https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-stats.html

5.2 索引段

segment是比Lucene索引更小的单位,通过segment可以获取更多的关于分片和索引的信息。

查看索引的段信息:

GET test/_segments

返回结果:

{
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "indices": {
    "test": {
      "shards": {
        "0": [
          {
            "routing": { "state": "STARTED", "primary": true, "node": "3dQd1RRVTMiKdTckM68nPQ" },
            "num_committed_segments": 0,
            "num_search_segments": 0,
            "segments": {} }
        ],
        "1": [
          {
            "routing": { "state": "STARTED", "primary": true, "node": "3dQd1RRVTMiKdTckM68nPQ" },
            "num_committed_segments": 0,
            "num_search_segments": 0,
            "segments": {} }
        ],
        "2": [
          {
            "routing": { "state": "STARTED", "primary": true, "node": "3dQd1RRVTMiKdTckM68nPQ" },
            "num_committed_segments": 0,
            "num_search_segments": 0,
            "segments": {} }
        ],
        "3": [
          {
            "routing": { "state": "STARTED", "primary": true, "node": "3dQd1RRVTMiKdTckM68nPQ" },
            "num_committed_segments": 1,
            "num_search_segments": 1,
            "segments": { "_1": { "generation": 1, "num_docs": 1, "deleted_docs": 0, "size_in_bytes": 3727, "memory_in_bytes": 2588, "committed": true, "search": true, "version": "6.5.0", "compound": true } } }
        ],
        "4": [
          {
            "routing": { "state": "STARTED", "primary": true, "node": "3dQd1RRVTMiKdTckM68nPQ" },
            "num_committed_segments": 1,
            "num_search_segments": 1,
            "segments": { "_0": { "generation": 0, "num_docs": 1, "deleted_docs": 0, "size_in_bytes": 3206, "memory_in_bytes": 2042, "committed": true, "search": true, "version": "6.5.0", "compound": true } } }
        ]
      }
    }
  }
}

统计索引占段内存:

curl -s "http://localhost:9200/_cat/segments/test?v&h=shard,segment,size,size.memory" |awk '{sum += $NF} END {print sum}'

5.3 索引恢复

GET index1,index2/_recovery?human
GET _recovery?human&detailed=true

5.4 索引分片存储

六、状态管理

6.1 清除缓存

POST /twitter/_cache/clear

POST /kimchy,elasticsearch/_cache/clear

POST /_cache/clear

6.2 刷新

POST /twitter/_refresh

POST /kimchy,elasticsearch/_refresh

POST /_refresh

6.3 flush

POST twitter/_flush

POST kimchy,elasticsearch/_flush

POST _flush

6.4 强制段合并(force merge)

POST /twitter/_forcemerge

POST /kimchy,elasticsearch/_forcemerge

POST /_forcemerge
相关实践学习
以电商场景为例搭建AI语义搜索应用
本实验旨在通过阿里云Elasticsearch结合阿里云搜索开发工作台AI模型服务,构建一个高效、精准的语义搜索系统,模拟电商场景,深入理解AI搜索技术原理并掌握其实现过程。
ElasticSearch 最新快速入门教程
本课程由千锋教育提供。全文搜索的需求非常大。而开源的解决办法Elasricsearch(Elastic)就是一个非常好的工具。目前是全文搜索引擎的首选。本系列教程由浅入深讲解了在CentOS7系统下如何搭建ElasticSearch,如何使用Kibana实现各种方式的搜索并详细分析了搜索的原理,最后讲解了在Java应用中如何集成ElasticSearch并实现搜索。  
目录
相关文章
|
11月前
|
缓存 监控 前端开发
顺企网 API 开发实战:搜索 / 详情接口从 0 到 1 落地(附 Elasticsearch 优化 + 错误速查)
企业API开发常陷参数、缓存、错误处理三大坑?本指南拆解顺企网双接口全流程,涵盖搜索优化、签名验证、限流应对,附可复用代码与错误速查表,助你2小时高效搞定开发,提升响应速度与稳定性。
|
存储 人工智能 自然语言处理
Elasticsearch Inference API增加对阿里云AI的支持
本文将介绍如何在 Elasticsearch 中设置和使用阿里云的文本生成、重排序、稀疏向量和稠密向量服务,提升搜索相关性。
828 14
Elasticsearch Inference API增加对阿里云AI的支持
|
存储 人工智能 API
(Elasticsearch)使用阿里云 infererence API 及 semantic text 进行向量搜索
本文展示了如何使用阿里云 infererence API 及 semantic text 进行向量搜索。
811 8
|
存储 缓存 监控
优化Elasticsearch 索引设计
优化Elasticsearch 索引设计
452 5
|
测试技术 API 开发工具
ElasticSearch7.6.x 模板及滚动索引创建及注意事项
ElasticSearch7.6.x 模板及滚动索引创建及注意事项
391 8
|
存储 JSON 关系型数据库
Elasticsearch 索引
【11月更文挑战第3天】
530 4
|
监控 API 索引
Elasticsearch集群使用 _cluster/health API
Elasticsearch集群使用 _cluster/health API
898 2
|
Unix API 索引
Elasticsearch集群使用 _cat/health API
Elasticsearch集群使用 _cat/health API
496 1
|
安全 Java Linux
Linux安装Elasticsearch详细教程
Linux安装Elasticsearch详细教程
2506 64
|
JSON 安全 数据可视化
Elasticsearch(es)在Windows系统上的安装与部署(含Kibana)
Kibana 是 Elastic Stack(原 ELK Stack)中的核心数据可视化工具,主要与 Elasticsearch 配合使用,提供强大的数据探索、分析和展示功能。elasticsearch安装在windows上一般是zip文件,解压到对应目录。文件,elasticsearch8.x以上版本是自动开启安全认证的。kibana安装在windows上一般是zip文件,解压到对应目录。elasticsearch的默认端口是9200,访问。默认用户是elastic,密码需要重置。
6822 0

热门文章

最新文章