Elasticsearch-核心篇(5)-映射操作一、创建

一、创建映射

  1. 有了索引库,等于有了数据库中的database
  2. 接下来就需要建索引库(index)中的映射,类似于数据库(database)中的表结构(table)
  3. 创建数据库表需要设置字段名称,类型,长度,约束等;索引库也一样,需要知道这个类型下有哪些字段,每个字段有哪些约束信息,这就叫做映射(mapping)
  4. 如果不指定映射将会默认按照给定的类型自动添加对应映射

1.1 创建时映射

  1. 可以在创建索引时指定映射
    • 其中mappings.properties为固定结构,指定创建映射属性
PUT index
{
  "mappings" : {
      "properties" : {
        "id" : {
          "type" : "long"
        },
        "title" : {
          "type" : "text"
        }
      }
  }
}
复制代码
  1. 创建完成后将会返回创建信息
{
  "index" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "id" : {
          "type" : "long"
        },
        "title" : {
          "type" : "text"
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1623072136760",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "aatM8ty0QQuZWXo-99lvnA",
        "version" : {
          "created" : "7080099"
        },
        "provided_name" : "index"
      }
    }
  }
}
复制代码

1.2 映射修改

  1. 可以对已经存在的索引添加映射
  2. 需要使用 PUT /_mapping
  3. 修改时只需要指定properties即可,不需要指定mappings
PUT index/_mapping
{
  "properties" : {
    "id" : {
      "type" : "long"
    },
    "title" : {
      "type" : "text"
    }
  }
}
复制代码
  1. 修改结果
{
  "acknowledged" : true
}
复制代码

1.3 添加时映射

  1. 如果不明确指定,则将会在添加数据时自动判定
  2. 创建普通索引,此时没有映射信息
{
  "index" : {
    "aliases" : { },
    "mappings" : { },
    "settings" : {
      "index" : {
        "creation_date" : "1623072418993",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "682iz4t_TRa-1Nc7bfm-ng",
        "version" : {
          "created" : "7080099"
        },
        "provided_name" : "index"
      }
    }
  }
}

复制代码
  1. 向索引中添加数据
POST index/_doc/1
{
  "id": 3,
	"title": "华为手机",
	"category": "华为"
}
复制代码
  1. 将会自动识别对应值的映射
{
  "index" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "category" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "id" : {
          "type" : "long"
        },
        "title" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1623072418993",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "682iz4t_TRa-1Nc7bfm-ng",
        "version" : {
          "created" : "7080099"
        },
        "provided_name" : "index"
      }
    }
  }
}
复制代码

二、查看映射

  1. 查看索引完全信息,内容包含映射信息
GET index
复制代码
{
  "index" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "category" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "id" : {
          "type" : "long"
        },
        "title" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1623072418993",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "682iz4t_TRa-1Nc7bfm-ng",
        "version" : {
          "created" : "7080099"
        },
        "provided_name" : "index"
      }
    }
  }
}
复制代码
  1. 指定查询索引信息
GET index/_mapping
复制代码
{
  "index" : {
    "mappings" : {
      "properties" : {
        "category" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "id" : {
          "type" : "long"
        },
        "title" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}
复制代码