8.ElasticSearch系列之索引模板与索引

1. 索引模板创建索引

可以通过http://127.0.0.1:5601/app/management/data/index_management/templateskibana工具进行创建索引模板

也可以自定义语句,如创建poi索引模板

POST _index_template/poi
{
  "index_patterns": ["poi*"],
  "template" : {
    "settings" : {
      "index" : {
        "number_of_shards" : "1",
        "number_of_replicas" : "1"
      }
    },
    "mappings" : {
      "dynamic": "strict", // 严格模式,不允许动态创建字段
      "properties" : {
        "city" : {
          "type" : "keyword"
        },
        "region" : {
          "type" : "keyword"
        },
        "name" : {
          "type" : "text"
        },
        "location" : {
          "type" : "geo_point" // 地址坐标点类型,可以进行范围搜索
        }
      }
    }
  }
}
# 查看创建的索引
GET _index_template/poi
# 创建索引
PUT poi
# 查看索引
GET poi
2. 索引新增字段及重建
# 索引poi新增创建时间字段
PUT poi/_mapping
{
  "properties": {
    "c_date": {
      "type": "date"
    }
  }
}
# 当想对name进行中文分词时,需要重建索引,然后修改模板,删除旧索引,在重建回来,如下步骤
POST _reindex
{
  "source": {
    "index": "poi"
  },
  "dest": {
    "index": "poi_bak"
  }
}
POST _index_template/poi
{
  "index_patterns": ["poi*"],
  "template" : {
    "settings" : {
      "index" : {
        "number_of_shards" : "1",
        "number_of_replicas" : "1"
      }
    },
    "mappings" : {
      "dynamic": "strict",
      "properties" : {
        "city" : {
          "type" : "keyword"
        },
        "region" : {
          "type" : "keyword"
        },
        "name" : {
          "type" : "text",
          "analyzer": "ik_smart" // 新增分词
        },
        "location" : {
          "type" : "geo_point"
        }
      }
    }
  }
}
DELETE poi
PUT POI
POST _reindex
{
  "source": {
    "index": "poi_bak"
  },
  "dest": {
    "index": "poi"
  }
}
// 如果想修改字段名,可在重建时进行修改
POST _reindex?wait_for_completion=false
{
  "source": {
    "index": "poi_bak"
  },
  "dest": {
    "index": "poi"
  },
  "script": {
    "source": "ctx._source.paiMaiType=ctx._source.remove(\"zcType\")"
  }
}

DELETE poi_bak

欢迎关注公众号算法小生沈健的技术博客

posted @ 2022-10-18 21:05  算法小生  阅读(140)  评论(0编辑  收藏  举报