Elasticsearch-Index索引

一.索引管理
直接put数据PUT index/_doc/1,ES会自动生成索引,并建立动态映射dynamic mapping
在生产上,需要自己手动建立索引和映射,为了更好地管理索引。就像数据库的建表语句一样

  1. 创建索引
PUT /index 
{
  "setting": {},
  "mappings": {
    "properties": {
      "field1": {"type": "text"}, ...
    }
  },
  "aliases": {
    "default_index": {}
  }
}

eg:

PUT my_index
{
  "settings": { # 索引的设置
    "number_of_shards": 1, # 分片数
    "number_of_replicas": 1 # 副本数
  },
  "mappings": { # 设置映射,
    "properties": {
      "field1": {
        "type": "text"
      },
      "field2": {
        "type": "text"
      }
    }
  },
  "aliases": { # 别名
    "default_index": {} # 可以通过my_index查询也可使用default_index查询
  }
}

eg:

PUT my_index
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1
  },
  "mappings": { 
    "properties": {
      "field1": {
        "type": "text"
      },
      "field2": {
        "type": "text"
      }
    }
  },
  "aliases": {
    "default_index": {}
  }
}

PUT my_index/_doc/1
{
  "field1": "php",
  "field2": "java"
}

GET my_index/_doc/1

GET default_index/_doc/1
  1. 查询索引
GET my_index
GET my_index/_mapping
GET my_index/_settings

  1. 修改索引
修改副本数
PUT my_index/_settings
{
  "index": {
    "number_of_replicas" : 2
  }
}
  1. 删除索引
    DELETE /my_index
    DELETE /inidex_1, index_2
    DELETE /inidex_*
    DELETE /_all # 删除所有索引
    为了安全起见,防止恶意删除索引,删除时必须指定索引名,在elasticsearch.yml配置文件中更改
    action.destructive_requires_name: true
posted @ 2024-04-07 11:21  py卡卡  阅读(8)  评论(0编辑  收藏  举报