首页 > 编程知识 正文

elasticsearch创建索引命令,elasticsearch的索引

时间:2023-05-03 14:04:19 阅读:194421 作者:1713

1. 创建索引的同时指定特殊字段的类型 PUT /gunspoc{ "mappings": { "doc":{ "properties":{ "name":{ "type":"keyword" }, "age":{ "type": "long" }, "address":{ "type":"text" }, "birthday":{ "type": "date", "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" } } } }}

这里建立了一个索引,且指定了name的类型为keyword,这样name字段就可以用term来精准搜索了,long类型的age就可以用来计算和比较大小了,birthday指定为日期类型,其可以是format中的格式化类型也可以是时间戳类型,其他的没有指定类型的字段将以默认的映射来在es中建立字段。

2. 查看mapping GET /gunspoc/_mapping

结果:

{ "gunspoc" : { "mappings" : { "doc" : { "properties" : { "address" : { "type" : "text" }, "age" : { "type" : "long" }, "birthday" : { "type" : "date", "format" : "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" }, "name" : { "type" : "keyword" } } } } }} 3. 查看settings GET /gunspoc/_settings?pretty

结果:

{ "gunspoc" : { "settings" : { "index" : { "creation_date" : "1607391267314", "number_of_shards" : "5", "number_of_replicas" : "1", "uuid" : "Oljl4kqeSFiWCFz_1M5XvA", "version" : { "created" : "6050499" }, "provided_name" : "gunspoc" } } }}

number_of_shards: 该索引的的分片数
number_of_replicas: 该索引的副本数
(这里的number_of_shards: 5和 number_of_replicas:1 是建立索引的时候的默认值,也可以在建立索引的时候自定义)

4. 建立索引同时设置settings PUT /gunspoc2{ "settings": { "number_of_shards": 6, "number_of_replicas": 1, "refresh_interval": "10s", "translog":{ "flush_threshold_size":"1gb", "sync_interval":"30s", "durability":"async" } }, "mappings": { "doc":{ "properties":{ "name":{ "type":"keyword" }, "age":{ "type": "long" }, "address":{ "type":"text" }, "birthday":{ "type": "date", "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" } } } }}

number_of_shards: 5 (主分片数量一旦设置后就不能修改了)
number_of_replicas:1(副本数量可以修改)
refresh_interval :索引的刷新时间间隔(即数据写入es到可以搜索到的时间间隔,设置越小越靠近实时,但是索引的速度会明显下降,),默认为1秒,如果我们对实时搜索没有太大的要求,反而更注重索引的速度,那么我们就应该设置的稍微大一些,这里我设置10s

translog中存储了ES的操作记录,写入的索引并没有实时落盘到索引文件,而是先双写到内存和translog文件。因此不难看出translog的作用就是保证ES数据不丢失。为了保证性能,插入ES的数据并不会立刻落盘,而是首先存放在内存当中,等到条件成熟后触发flush操作,内存中的数据才会被写入到磁盘当中。如果ES服务器突然断电,那么停留在内存中还没来得及写入磁盘中的数据是不是就丢失了呢?还好translog保留了这些数据的操作日志,在ES服务重启的时候,会读取translog,恢复这部分数据。

durability:async ,异步刷写translog日志,(默认是同步的)
“flush_threshold_size”:“1gb” (当translog的大小达到此值时会进行一次flush操作。默认是512mb。)
“sync_interval”:“30s”,每隔30s检查translog 刷到硬盘(默认5s)。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。