1、索引
查看所有索引
GET /_cat/indices?v
健康状况
GET _cluster/health
GET _cluster/health?level=indices
分词器
POST _analyze
{
"analyzer": "ik_max_word",
"text": "四川森森管理公司"
}
索引数据拷贝
POST _reindex
{
"source": {
"index": "position_v1"
},
"dest": {
"index": "position_v2"
}
}
创建索引
#只创建索引
PUT /test
# 创建索引同时创建字段类型
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"
}
}
}
}
}
获取索引字段类型
GET /position_v2/_mapping
查看索引settings
GET /supplier_position_v1/_settings
{
"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 是建立索引的时候的默认值,也可以在建立索引的时候自定义)
建立索引同时设置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)。
查看单个索引
GET /supplier_position_v1
{
"supplier_position_v1" : { #索引名
"aliases" : { },#别名
"mappings" : { #映射
"properties" : {
"ageLow" : {
"type" : "integer"
},
"ageUp" : {
"type" : "integer"
},
"categoryOne" : {
"type" : "long"
}
},
"settings" : { #设置
"index" : {
"routing" : {
"allocation" : {
"include" : {
"_tier_preference" : "data_content"
}
}
},
"refresh_interval" : "1s",
"number_of_shards" : "5",//设置 - 索引 - 主分片数量
"provided_name" : "supplier_position_v1",
"creation_date" : "1649411888583", #设置 - 索引 - 创建时间
"store" : {
"type" : "fs"
},
"number_of_replicas" : "1", //设置 - 索引 - 副本数量
"uuid" : "0KH61wtQRYK1mNaw3xUolw",//设置 - 索引 - uid
"version" : {
"created" : "7100199"
}
}
}
}
}
2. 文档
POST /shopping/_doc/1
{
"title":"小米手机",
"category":"小米",
"images":"http://www.gulixueyuan.com/xm.jpg",
"price":3999.00
}
返回结果
{
"_index": "shopping",
"_type": "_doc",
"_id": "1",//<------------------自定义唯一性标识
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1
}