ES(Elasticsearch)入门-深入索引操作

1.创建索引

  • 使用PUT 请求。
  • 结构
PUT /${index_name} // 索引名称
{
    "settings":{
    ... 索引相关的配置项目,如何:分配个数 副分片个数等
    },
    "mappings":{
    ... 数据的结构
    }
}

-----------------------------------实例 -------------------------------------
PUT /indexname
{
  "settings": {
    "number_of_shards": 2,  // 指定主分片数
    "number_of_replicas": 2 // 副本分片数
  },
  "mappings": {
    "properties": {
      "name":{
         "type": "text"
      },
      "value":{
        "type": "integer"
      }
    }
  }
}

运行结果

2. 写入稳定

  • 使用 Post 请求
  • 结构
POST /${indexname}/_doc/${Id}
{
  ... 文档数据
}
-----------------------------------实例 -------------------------------------
POST /indexname/_doc/001
{
 "name":"测试_ES",
 "value":1
}

运行结果

3. 查询信息

  • 使用GET 请求
  • 结构
## ID 查询
GET /${indexname}/_doc/${Id}
-----------------------------------实例 -------------------------------------
GET /indexname/_doc/001

运行结果

GET /${indexname}/_search
{
   query:{
     
     ... 查询条件
   }
}
## 一般字段查询 也就是完全匹配
-----------------------------------实例 -------------------------------------
GET /indexname/_search
{
  "query": {
    "term": {
      "value": {
        "value": 1
      }
    }
  }
}

运行结果: 已经进行了打分

## 文本字段搜索  模糊查询
GET /indexname/_search
{
  "query": {
    "match": {
      "name": "_ES"
    }
  }
}

运行结果:

查询结果 已经进行了打分

4 删除索引

  • 使用 DELETE
  • 结构
DELETE /${indexname}
-----------------------------------实例 -------------------------------------
DELETE /indexname
删除结果

5 关闭索引 就是暂时不用 此索引

  • 使用 POST
  • 结构
POST /${indexname}/_close
-----------------------------------实例 -------------------------------------
## 创建索引用 条目1 因为您已经删除索引了
PUT /indexname
{
  "settings": {
    "number_of_shards": 2,  // 指定主分片数
    "number_of_replicas": 2 // 副本分片数
  },
  "mappings": {
    "properties": {
      "name":{
         "type": "text"
      },
      "value":{
        "type": "integer"
      }
    }
  }
}
## 关闭
POST /indexname/_close

## 测试写入 使用 条目2 
关闭文档结果
测试关闭后插入

6 .打开索引

  • 使用Post 请求
  • 结构
POST /${indexname}/_open
-----------------------------------实例 -------------------------------------
POST /indexname/_open
打开结果

7. 创建索引别名 # 关系类型的的数据库有点像 视图Union 表

  • 使用POST
  • 结构
POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "${indexname}",
        "alias": "${aliasname}"
        "is_write_index":true //写入这个索引
      }
    },
    {
        "add": {
          "index": "${indexname}",
        "alias": "${aliasname}"
      }
    }
  ]
}
-----------------------------------实例 -------------------------------------
## 在创建一个 索引
PUT /indexname01
{
  "settings": {
    "number_of_shards": 2,  // 指定主分片数
    "number_of_replicas": 2 // 副本分片数
  },
  "mappings": {
    "properties": {
      "name":{
         "type": "text"
      },
      "value":{
        "type": "integer"
      }
    }
  }
}
## 写入一条数据
POST /indexname01/_doc/001
{
 "name":"测试01_ES",
 "value":2
}
POST /indexname/_doc/001
{
 "name":"测试_ES",
 "value":1
}
## 创建别名
POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "indexname",
        "alias": "alias1"
      }
    },
     {
      "add": {
        "index": "indexname01",
        "alias": "alias1"
      }
    }
  ]
}
## 测试查询
GET /alias1/_search
{
  "query": {
    "term": {
      "_id": {
        "value": "001"
      }
    }
  }
}
创建别名结果
测试别名查询

7.2 别名索引的写入

## 未设置 is_write_index 情况下
POST /alias1/_doc/002
{
 "name":"测试02_ES",
 "value":3
}
错误信息
## 这是is_wirte_index
POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "indexname",
        "alias": "alias1",
        "is_write_index":true
      }
    }
  ]
}
## 写入数据
POST /alias1/_doc/002
{
 "name":"测试02_ES",
 "value":3
}
## 测试查询
GET /alias1/_search
{
  "query": {
    "match": {
      "name": "测试"
    }
  }
}
测试查询结果 02 在indexname 中

7.3 别名替换。为满足上次代码 的动态修改数据里来源

如:一个 log 索引有个 1 主分片,2 个副分片 ,在系统增长时出现查询性能瓶颈,希望 添加多个主分片,及多个副分片,此时就可以用 替换方案

## 接续上文 我们创建一个 索引 indexname03 修改类其主副分片数,创建
PUT /indexname03
{
  "settings": {
    "number_of_shards": 4,  // 指定主分片数
    "number_of_replicas": 4 // 副本分片数
  },
  "mappings": {
    "properties": {
      "name":{
         "type": "text"
      },
      "value":{
        "type": "integer"
      }
    }
  }
}
## 查看一下 alias1 定义
GET /alias1
## 替换到 别名中的 indexname01
POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "indexname03",
        "alias": "alias1"
      }
    },
    {
      
      "remove": {
        "index": "indexname01",
         "alias": "alias1"
      }
    }
  ]
}
## 查看一下 alias1 定义
GET /alias1
posted @ 2024-05-21 14:47  忧伤恋上了快乐  阅读(4)  评论(0编辑  收藏  举报