Elasticsearch 5.4 Indices(索引) API

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: 前言一索引管理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
相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
目录
相关文章
|
25天前
|
存储 自然语言处理 关系型数据库
ElasticSearch索引 和MySQL索引那个更高效实用那个更合适
ElasticSearch索引 和MySQL索引那个更高效实用那个更合适
37 0
|
2月前
|
存储 API 索引
Elasticsearch Reroute API 的使用
Elasticsearch Reroute API 的使用
41 1
|
2月前
|
存储 算法 NoSQL
Elasticsearch拆分索引知多少
Elasticsearch拆分索引知多少
33 0
|
3月前
|
存储 JSON 自然语言处理
Elasticsearch 利用API进行搜索
Elasticsearch 利用API进行搜索
35 0
|
3月前
|
数据采集 存储 自然语言处理
elasticsearch 跨索引联合多条件查询
elasticsearch 跨索引联合多条件查询
|
1月前
|
JSON 监控 数据管理
【Elasticsearch专栏 12】深入探索:Elasticsearch使用索引生命周期管理(ILM)自动化删除旧数据
Elasticsearch的ILM功能允许用户定义策略,自动管理索引从创建到删除的生命周期。用户可以设置策略,根据索引年龄或大小自动删除旧数据,节省存储空间。通过应用ILM策略于索引模板,新索引将遵循预定义的生命周期。用户还可以监控ILM状态,确保策略按预期执行。使用ILM,用户可以高效地管理数据,确保旧数据及时删除,同时保持数据完整性和安全性。
|
3月前
|
索引
elasticsearch 创建索引模版template
elasticsearch 创建索引模版template
|
2月前
|
存储 自然语言处理 搜索推荐
【Elasticsearch专栏 01】深入探索:Elasticsearch的正向索引和倒排索引是什么?
正向索引根据文档ID直接查找文档内容,适用于精确匹配场景;而倒排索引则基于文档内容构建,通过关键词快速定位相关文档,适用于全文搜索,显著提高查询效率,是搜索引擎的核心技术。
|
2月前
|
存储 自然语言处理 搜索推荐
【Elasticsearch专栏 02】深入探索:Elasticsearch为什么使用倒排索引而不是正排索引
倒排索引在搜索引擎中更受欢迎,因为它直接关联文档内容,支持全文搜索和模糊搜索,提高查询效率。其紧凑的结构减少了存储空间,并方便支持多种查询操作。相比之下,正排索引在搜索效率、存储和灵活性方面存在局限。
|
2月前
|
API 索引
Elasticsearch Index Shard Allocation 索引分片分配策略
Elasticsearch Index Shard Allocation 索引分片分配策略
76 1

热门文章

最新文章