要使用elasticsearch就先要创建索引表 就相当于数据库的表

创建索引表:

put关键字:创建库
setting:设置索引库的属性 可以不设置 直接put /itheima

  1. put /itheima
  2. {
  3. "settings": {
  4. "name": "黑马"
  5. }
  6. }

查询索引库:查看文档和属性约束

GET /heima

查询所有的索引库:

get _cat/indices

删除索引库:删除索引库时 文档数据也随之删除

DELETE /itheima


为每张表的属性创建映射:

映射表中字段的类型:type:字段数据类型,常见的简单类型有:

  • 字符串:text(可分词的文本)、keyword(精确值,例如:品牌、国家、ip地址)
  • 数值:long、integer、short、byte、double、float、
  • 布尔:boolean
  • 日期:date
  • 对象:object

是否创建索引:index:是否创建索引,默认为true 一般要以此字段排序的就要设置true
设置分词器:analyzer:使用哪种分词器 设置分词器ik分词器 ik_max_word(细粒度的分析)和ik_smart(粗粒度的分词)两种方式
设置子字段:properties:该字段的子字段

创建映射 为每个字段设置属性 属性名的上一层必须为properties

PUT /heima
{
  "mappings": {
    "properties": {
      "id":{
        "type": "long"
        "copy_to":"all"
      },
      "price":{
        "type": "double"
      },
      "isHot":{
        "type": "boolean"
      },
      "title":{
        "type": "text",
        "analyzer": "ik_smart"
      },
      "category":{
        "type": "keyword"
      },
      "score":{
        "type": "float"
      },
      "alias":{
        "properties": {
          "firstName":{
            "type": "keyword"
          },
          "lastName": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

当索引库不存在 mapping写里面 第一行和创建索引库一样

PUT /heima
{
  "mappings": {
    "properties": {
      "id":{
        "type": "long"
      },

当索引库已经存在 mapping写外面

PUT /heima/_mapping
{
  "properties": {
    "title": {
      "type": "text",
      "analyzer": "ik_smart"
    },

查看映射关系:就是查看约束关系

GET /heima/_mapping

无法删除和修改约束 只能新增约束

#向已有的索引库映射添加属性
PUT /heima/_mapping
{
  "properties":{
    "from": {
      "type": "text"
    }
  }
}