Elasticsearch 入门到精通-Elasticsearch创建索引

1、创建索一个没有字段的索引

#创建索引(并且指定分片数量)
PUT my_index1
{
  "settings": {
    "number_of_shards": 2,
    "number_of_replicas": 2
  }
}

2、创建索一个有字段的索引

#创建索引(并且指定分片数量)
PUT myindex2
{
  "settings": {
    "number_of_shards": 5,
    "number_of_replicas": 0
  },
  "mappings": {
    "properties": {
      "id": {
        "type": "long"
      },
      "name": {
        "type": "text"
      },
      "type": {
        "type": "keyword"
      },
      "score": {
        "type": "double"
      }
    }
  }
}

 3、修改分片副本

#创建索引(并且指定分片数量)
PUT my_index1/_settings
{
  "index": {
        "number_of_replicas": "4"
      }
}

4、新增Mapping映射(新增字段)

POST /my_index2/_mapping
{
  "properties": {
      "phone":{
        "type": "keyword"
      }
    }
}

注:只能新增字段,不能删除字段,一旦设置type类型,不可更改。

为什么不能修改一个字段的type?原因是一个字段的类型修改以后,那么该字段的所有数据都需要重新索引。Elasticsearch底层使用的是lucene库,字段类型修改以后索引和搜索要涉及分词方式等操作。


版权声明:本文为wangguoqing_it原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。