前文咱们提到,Elasticsearch的数据都存储在索引中,也就是说,索引至关因而MySQL中的数据库。是最基础的概念。今天分享的也是关于索引的一些经常使用的操做。html
curl -X PUT "localhost:9200/jackey?pretty"
复制代码
ES建立索引使用PUT请求便可,上面是最简单的新建一个索引的方法,除此以外,你还能够指定:数据库
索引名称有如下限制:json
\
,/
,*
, ?
, "
, <
, >
, |
,
(空格),,
, #
-
,_
和+
开头.
或..
请求支持的一些参数有:安全
前面咱们提到建立索引时能够指定三种属性,这三种属性都须要放在body中。bash
索引的别名,一个别名能够赋给多个索引。app
给一个index起别名的方式有两种,一种是建立index时候在body中增长aliases,另外一种是经过更新已有索引的方式增长。curl
方式一:elasticsearch
curl -X PUT "localhost:9200/jackey?pretty" -H 'Content-Type: application/json' -d' { "aliases" : { "alias_1" : {}, "alias_2" : { "filter" : { "term" : {"user" : "kimchy" } }, "routing" : "kimchy" } } } '
复制代码
方式二:ide
curl -X POST "localhost:9200/_aliases?pretty" -H 'Content-Type: application/json' -d' { "actions" : [ { "add" : { "index" : "jackey", "alias" : "alias1" } } ] } '
复制代码
方式一中,咱们还在body中增长了filter和routing。这主要是用于指定使用别名的条件。指定了filter后,经过alias_2,只能访问user为kimchy的document。而routing的值被用来路由,即alias_2只能路由到指定的分片。此外还有index_routing和search_routing,它们和routing相似,这里不作过多解释了。还有一个比较重要的属性是is_write_index,这个属性默认是false,若是设置成true,表示能够经过这个别名来写索引,默认状况下,别名像一个软连接,是不能够修改原索引的。ui
此外,还可使用通配符为多个索引增长相同的别名
curl -X POST "localhost:9200/_aliases?pretty" -H 'Content-Type: application/json' -d' { "actions" : [ { "add" : { "index" : "test*", "alias" : "all_test_indices" } } ] } '
复制代码
除了add,还可使用remove来删除别名
curl -X POST "localhost:9200/_aliases?pretty" -H 'Content-Type: application/json' -d' { "actions" : [ { "remove" : { "index" : "test1", "alias" : "alias1" } } ] } '
复制代码
先看一个例子:
curl -X PUT "localhost:9200/twitter?pretty" -H 'Content-Type: application/json' -d' { "settings" : { "index" : { "number_of_shards" : 3, "number_of_replicas" : 2 } } } '
复制代码
索引的setting分为静态和动态两种。静态的只能在索引建立或关闭时设置;动态的则可使用update-index-settings API来实时设置。上面的例子中,number_of_shards属于静态设置,number_of_replicas属于动态设置。
索引能够设置的setting能够在官方文档的Index modules查看,下面我会挑几个我认为比较重要的介绍一下。
先从静态开始:
动态setting:
除了以上静态setting和动态setting以外,setting中还能够设置一些其余的值,例如分词器等,这些咱们之后再作更详细的介绍。
curl -X PUT "localhost:9200/test?pretty" -H 'Content-Type: application/json' -d' { "settings" : { "number_of_shards" : 1 }, "mappings" : { "properties" : { "field1" : { "type" : "text" } } } } '
复制代码
Mapping主要用于帮助Elasticsearch理解每一个域中数据的类型。7.0.0以前mapping的定义一般包括type名称。Elasticsearch支持的数据类型比较多,其中比较核心的简单数据类型包括:
其余的类型,咱们之后会作更加详细的介绍。
删除索引使用的是DELETE请求。
curl -X DELETE "localhost:9200/jackey?pretty"
复制代码
你能够在路径中指定具体索引,也可使用通配符,须要删除多个索引时,可使用逗号分隔。若是要删除所有索引,能够指定索引为_all或*(不要这么作)。在生产环境,咱们经过在elasticsearch.yml文件中将action.destructive_requires_name配置为true来禁止这些危险的操做。
删除操做支持的参数有如下几种:
前面咱们已经提到过了打开/关闭索引。被关闭的索引几乎不能对它进行任何操做,它只是用来保留数据的。而打开或关闭索引一般须要重启分片来使操做生效。具体的操做以下:
curl -X POST "localhost:9200/jackey/_open?pretty"
复制代码
curl -X POST "localhost:9200/jackey/_close?pretty"
复制代码
支持的参数有:
这些参数在前面都有介绍。这里就再也不赘述了。
随着数据的愈来愈多,咱们可能会有拆分索引的需求,感谢ES为咱们提供了便利。
curl -X POST "localhost:9200/twitter/_split/split-twitter-index?pretty" -H 'Content-Type: application/json' -d' { "settings": { "index.number_of_shards": 2 } } '
复制代码
在拆分索引以前,要保证索引是只读状态,而且集群健康状态为green。设置只读的方法是:
curl -X PUT "localhost:9200/my_source_index/_settings?pretty" -H 'Content-Type: application/json' -d' { "settings": { "index.blocks.write": true } } '
复制代码
拆分索引的具体操做是:
关于索引的使用就先介绍到这里。还有不少不完善的地方,之后会继续补充。想要了解更多详细信息的同窗能够查看官方文档。