ES-索引的增删改查

1.索引

目的:存储学生的分数信息,且支持 搜索 “姓名、课程名、分数”


    --  示例数据 --  
  {
    "username":"zs",
    "courses": [
      {"course":"数学","grade": 99},
      {"course":"英语","grade": 88}
    ]
  }

1.1 创建索引

1.1.1 自动创建索引

put 数据时,es会自动创建索引(不推荐)

1.1.2 手动创建索引

user_course 为索引名


# 创建新索引
PUT /user_course
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      },
      "courses": {
        "type": "nested",
        "properties": {
          "course": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword"
              }
            }
          },
          "grade": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword"
              }
            }
          }
        }
      }
    }
  }
} 

courses 使用 nested , 支持 course=“数学” && grade>=98的查询
nested的作用,参考后续章节

1.2 删除索引

 DELETE /user_course

1.3 查看索引

     GET /user_course/_mapping

1.4 修改索引

1.4.1 创建新索引

PUT /user_course_new_index
{
  "mappings": {
     "name": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
    "properties": {
      "courses": {
        "type":"nested",
        "properties": {
          "course": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "grade": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
     
    }
  }
} 

1.4.2 迁移数据

POST _reindex
{
  "source": {
    "index": "user_course"
  },
  "dest": {
    "index": "user_course_new_index"
  }
}

1.4.3 将对外访问的别名设置到新的 索引

POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "user_course_new_index",
        "alias": "my_alias"
      }
    }
  ]
}

对外提供别名访问的好处:ES内部可以修改索引,对外0感知

你可能感兴趣的:(elasticSearch,elasticsearch)