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

1. 索引模板创建索引

可以通过kibana工具进行创建索引模板

也可以自定义语句,如创建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
3. 远程索引同步
# 远程索引同步至本地,conflicts=proceed遇到错误忽略继续执行
POST _reindex?wait_for_completion=false
{
  "source": {
    "remote": {
      "host": "http://192.168.0.XX:9200",
      "socket_timeout": "30s",
      "connect_timeout": "30s",
      "username": "xxxx",
      "password": "xxxx"
    },
    "index": "poi",
    "size": 1000,
    "query": {
      "match_all": {}
    }
  },
  "dest": {
    "index": "poi"
  }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沈健_算法小生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值