当前位置: 首页 > news >正文

学习用Node.js和Elasticsearch构建搜索引擎(7):零停机时间更新索引配置或迁移索引...

上一篇说到如果一个索引的mapping设置过了,想要修改type或analyzer,通常的做法是新建一个索引,重新设置mapping,再把数据同步过来。

那么如何实现零停机时间更新索引配置或迁移索引?这就需要用到索引的别名设置。

思路:

1、假设我们的索引是demo_v1,我们定义了一个别名demo,以后所有的操作都用别名demo操作。

2、现在索引demo_v1的mapping设置或者其他一些设置不满足我们的需求了,我们需要修改。新建一个索引demo_v2,同时设置好最新配置。

3、同步索引demo_v1的数据到索引demo_v2。直到同步完。

4、移除索引demo_v1的别名demo,同时设置索引demo_v2的别名为demo。

5、删除索引demo_v1。

6、迁移完成。以后如果还有设置变更,可以按照这个思路继续设置索引demo_v3、demo_v4……

 

接下来用一个例子说明实现过程,实际项目中我也是按这个思路做的。如果有一些命令操作看不懂,可参看上一篇文章。

1、创建索引demo_v1

> curl -XPUT 'localhost:9200/demo_v1'  
{"acknowledged":true,"shards_acknowledged":true}%

2、给索引demo_v1添加几条数据

#给索引demo_v1添加了type=fruit的3条数据,每条数据用name和tag两个字段
> curl -XPOST 'localhost:9200/_bulk?pretty' -d' { "index" : { "_index" : "demo_v1", "_type" : "fruit", "_id" : "1" }} { "name" : "苹果","tag":"苹果,水果,红富士"} { "create" : { "_index" : "demo_v1", "_type" : "fruit", "_id" : "2" }} { "name" : "香蕉","tag":"香蕉,水果,海南,弯弯,小黄人"} { "index" : { "_index" : "demo_v1", "_type" : "fruit", "_id" : "3" }} { "name" : "西瓜","tag":"西瓜,水果,圆形,绿,闰土"} '
#返回
{ "took" : 34, "errors" : false, "items" : [ { "index" : { "_index" : "demo_v1", "_type" : "fruit", "_id" : "1", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "created" : true, "status" : 201 } }, { "create" : { "_index" : "demo_v1", "_type" : "fruit", "_id" : "2", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "created" : true, "status" : 201 } }, { "index" : { "_index" : "demo_v1", "_type" : "fruit", "_id" : "3", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "created" : true, "status" : 201 } } ] }

3、给索引demo_v1设置别名demo

#设置别名
> curl -XPUT 'localhost:9200/demo_v1/_alias/demo'
{"acknowledged":true}%

4、使用别名查看信息

#使用别名查看一下数据,是可以查询到的
> curl -XGET 'localhost:9200/demo/fruit/_search?pretty'

#查看mapping
> curl -XGET 'localhost:9200/demo/fruit/_mapping?pretty'
#返回
{
  "demo_v1" : {
    "mappings" : {
      "fruit" : {
        "properties" : {
          "name" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "tag" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          }
        }
      }
    }
  }
}

#检索数据
> curl -XGET 'http://localhost:9200/demo/fruit/_search?pretty' -d '{
    "query" : {
        "term" : { "tag" : "" }
   }
}'
#返回
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 0.28582606,
    "hits" : [
      {
        "_index" : "demo_v1",
        "_type" : "fruit",
        "_id" : "1",
        "_score" : 0.28582606,
        "_source" : {
          "name" : "苹果",
          "tag" : "苹果,水果,红富士"
        }
      },
      {
        "_index" : "demo_v1",
        "_type" : "fruit",
        "_id" : "3",
        "_score" : 0.27233246,
        "_source" : {
          "name" : "西瓜",
          "tag" : "西瓜,水果,圆形,绿,闰土"
        }
      },
      {
        "_index" : "demo_v1",
        "_type" : "fruit",
        "_id" : "2",
        "_score" : 0.24257512,
        "_source" : {
          "name" : "香蕉",
          "tag" : "香蕉,水果,海南,弯弯,小黄人"
        }
      }
    ]
  }
}

数据因为先前创建索引时没有设置mapping,所以这些设置都是默认设置,分词器也默认标准分词器。

上面检索标签tag中带有“水”的数据,都查询出来了,说明默认分词器把“水果”这个词拆分了。

如果我们需要tag字段按照逗号分词,“水果”作为一个完整的词不拆分该怎么弄呢?

5、新建一个索引demo_v2,同时自定义逗号分词器,并把逗号分词器应用到tag字段上

#新建索引demo_v2
> curl -XPUT 'http://localhost:9200/demo_v2/' -d'{ "settings": { "index": { "analysis": { "analyzer": { "douhao_analyzer": { "pattern": ",", "type": "pattern" } } }, "number_of_shards": 5, "number_of_replicas": 1 } }, "mappings": { "fruit": { "properties": { "name": { "type": "text", "index": "not_analyzed" }, "tag": { "type": "string", "analyzer": "douhao_analyzer", "search_analyzer": "douhao_analyzer" } } } } }'
#返回
{"acknowledged":true,"shards_acknowledged":true}%

 关于mapping设置及分词器设置可参见官方文档:

 https://www.elastic.co/guide/en/elasticsearch/reference/5.3/mapping.html#mapping-type

 https://www.elastic.co/guide/en/elasticsearch/reference/5.3/analysis-analyzers.html

6、同步索引demo_v1中的数据到demo_v2

我使用工具elasticdump同步数据,ElasticDump是一个ElasticSearch的数据导入导出开源工具包。

官方地址:https://github.com/taskrabbit/elasticsearch-dump

同步命令如下:

> elasticdump --input='http://localhost:9200/demo_v1'  --output='http://localhost:9200/demo_v2' --type=data
Wed, 21 Jun 2017 09:53:15 GMT | starting dump
Wed, 21 Jun 2017 09:53:15 GMT | got 3 objects from source elasticsearch (offset: 0)
Wed, 21 Jun 2017 09:53:15 GMT | sent 3 objects to destination elasticsearch, wrote 3
Wed, 21 Jun 2017 09:53:15 GMT | got 0 objects from source elasticsearch (offset: 3)
Wed, 21 Jun 2017 09:53:15 GMT | Total Writes: 3
Wed, 21 Jun 2017 09:53:15 GMT | dump complete

7、验证一下demo_v2中的数据

#检索tag中包含“水”的数据,检索不到就是正常的
curl -XGET 'http://localhost:9200/demo_v2/fruit/_search?pretty' -d '{
    "query" : {
        "term" : { "tag" : "" }
   }
}'

#检索tag中包含“水果”的数据,可以全部检索到
curl -XGET 'http://localhost:9200/demo_v2/fruit/_search?pretty' -d '{
    "query" : {
        "term" : { "tag" : "水果" }
   }
}'

8、移除索引demo_v1的别名demo,同时设置索引demo_v2的别名为demo

curl -XPOST 'localhost:9200/_aliases?pretty' -d'{
  "actions" : [
  { "remove" : { "index" : "demo_v1", "alias" : "demo" } },
  { "add" : { "index" : "demo_v2", "alias" : "demo" } }
]}'

9、删除索引demo_v1

curl -XDELETE 'localhost:9200/demo_v1'

至此整个迁移完成

ok!

 

转载于:https://www.cnblogs.com/fhen/p/7060143.html

相关文章:

  • 2014百度之星第一题Energy Conversion
  • 【点播系列之二】如何快速接入视频点播服务
  • $.each()与$(selector).each()
  • Maven常见异常及解决方法
  • Linux - Redmine使用方式 | SVN提交代码
  • (转)真正的中国天气api接口xml,json(求加精) ...
  • redis/memcached可视化客户端工具TreeNMS
  • angular下如何绑定form表单的change事件
  • 实现两次点击超过0.5s之后执行某方法(不使用定时器)
  • 百度分页效果之纯jsp版
  • 腾讯云服务器 Centos6.5 安装 nginx1.12.0
  • mysql 基础
  • 关于抵御Petya勒索病毒的最新办法
  • 销售趋势分析折线图
  • How do we build TiDB
  • [译]CSS 居中(Center)方法大合集
  • 【干货分享】SpringCloud微服务架构分布式组件如何共享session对象
  • 【知识碎片】第三方登录弹窗效果
  • Angular 4.x 动态创建组件
  • Angularjs之国际化
  • ES6系统学习----从Apollo Client看解构赋值
  • Java超时控制的实现
  • JAVA多线程机制解析-volatilesynchronized
  • Java方法详解
  • Laravel核心解读--Facades
  • mockjs让前端开发独立于后端
  • python大佬养成计划----difflib模块
  • Redis 懒删除(lazy free)简史
  • SegmentFault 2015 Top Rank
  • SpingCloudBus整合RabbitMQ
  • vue2.0一起在懵逼的海洋里越陷越深(四)
  • 坑!为什么View.startAnimation不起作用?
  • 罗辑思维在全链路压测方面的实践和工作笔记
  • 面试遇到的一些题
  • 首页查询功能的一次实现过程
  • 通过获取异步加载JS文件进度实现一个canvas环形loading图
  • 微信小程序填坑清单
  • 小程序上传图片到七牛云(支持多张上传,预览,删除)
  • 一些基于React、Vue、Node.js、MongoDB技术栈的实践项目
  • 用简单代码看卷积组块发展
  • 主流的CSS水平和垂直居中技术大全
  • 看到一个关于网页设计的文章分享过来!大家看看!
  • PostgreSQL 快速给指定表每个字段创建索引 - 1
  • 说说我为什么看好Spring Cloud Alibaba
  • ​LeetCode解法汇总2583. 二叉树中的第 K 大层和
  • #laravel 通过手动安装依赖PHPExcel#
  • #中的引用型是什么意识_Java中四种引用有什么区别以及应用场景
  • $(function(){})与(function($){....})(jQuery)的区别
  • (HAL)STM32F103C6T8——软件模拟I2C驱动0.96寸OLED屏幕
  • (考研湖科大教书匠计算机网络)第一章概述-第五节1:计算机网络体系结构之分层思想和举例
  • (蓝桥杯每日一题)平方末尾及补充(常用的字符串函数功能)
  • (每日持续更新)信息系统项目管理(第四版)(高级项目管理)考试重点整理 第13章 项目资源管理(七)
  • (求助)用傲游上csdn博客时标签栏和网址栏一直显示袁萌 的头像
  • (十三)Maven插件解析运行机制
  • (原)本想说脏话,奈何已放下