elasticsearch 索引,更新,增量更新,不停机修改升级 ,简单使用


举例: 根据商品名称搜索商品

  1. 创建索引 product_v1 为什么要带v1呢,后面方便升级

    #新增索引
    put product_v1?pretty
    
    #查看一下
    get _cat/indices?v
    
  2. 定义map,简单设置,只定义idname,还有updatetime

    #设置map
    POST product_v1/_mapping/doc
    {
      "properties": {
        "name":{
            "type": "text",
            "analyzer": "ik_max_word",
            "search_analyzer": "ik_max_word"
        },
        "id":{
            "type": "long"
        },
        "updatetime": {
            "type": "date",
            "format": "yyyy-MM-dd HH:mm:ss"
        }
      }
    }
    #查看map
    GET /product_v1/_mapping/
    
    
  3. 添加别名,将product_v1设置为product,后面就可以针对别名操作。设置别名最大的好处,是后期变更map时很方便,开发时连接product即可

    POST /_aliases
    {
        "actions" : [
            { "add" : { "index" : "product_v1",  "alias" : "product" } }
        ]
    }
    
    
  4. 添加一些索引随便添加,日期就不写了

    PUT product/doc/1?pretty
    {
     "name":"网络红外半球摄像机"
    }
    
    PUT product/doc/2?pretty
    {
     "name":"网络高清硬盘录像机"
    }
    
    PUT product/doc/3?pretty
    {
     "name":"网络红外高清中速球机"
    }
    
    
  5. 搜索测试数据

    GET  product/_search
    {
        "query": {
        "match": {
          "name": "网络"
        }
      }
    }
    
    
  6. 升级变更map, 比如name变更为productName。 添加新字段typeName

    #1.建立新索引
    put product_v2?pretty
    
    #2.设置新map
    POST product_v2/_mapping/doc
    {
      "properties": {
        "productName":{
            "type": "text",
            "analyzer": "ik_max_word",
            "search_analyzer": "ik_max_word"
        },
        "typeName":{
            "type": "text",
            "analyzer": "ik_max_word",
            "search_analyzer": "ik_max_word"
        }
        ...
        ...
      }
    }
        
    #3.得新添加索引到`product_v2`,
    PUT product_v2/doc/1?pretty
    {
     "productName":"网络红外半球摄像机",
      ...
      ...
    }
    
    #4.设置别名指向新索引,原索引自己决定是否删除
    POST /_aliases
    {
        "actions" : [
            { "add" : { "index" : "product_v2",  "alias" : "product" } }
        ]
    }
    
    #或者同时删除掉原索引
    POST /_aliases
    {
        "actions" : [
            { "remove" : { "index" : "product_v1",  "alias" : "product" } },
            { "add" : { "index" : "product_v2",  "alias" : "product" } }
        ]
    }
    #或者单独删除都可以
    
    DELETE   product_v1
        
    
  7. 升级变更map, 比如字段类型需要变更,如给typeName添加一个keyword的类型

    #1.新建索引
    put product_v3?pretty
    #2.设置新`map`
    POST product_v3/_mapping/doc
    {
      "properties": {
        "typeName":{
            "type": "text",
            "analyzer": "ik_max_word",
            "search_analyzer": "ik_max_word",
            "fields": {
                "keyword": {
                    "type": "keyword"
                }
            }
        }
        ...
        ...
      }
    }
    
    #3.使用`reindex`得做索引
    post _reindex
    {
      "source": {
        "index": "product"
      },
      "dest": {
        "index": "product_v2"
      }
    }
    
    #4.将别名切到新索引,同上
    POST /_aliases
    {
        "actions" : [
            { "add" : { "index" : "product_v2",  "alias" : "product" } }
        ]
    }
    ...
    
    


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM