es之索引别名的使⽤

  在开发中,随着业务需求的迭代,较⽼的业务逻辑就要⾯临更新甚⾄是重构,⽽对于es来说,为了 适应新的业务逻辑,可能就要对原有的索引做⼀些修改,⽐如对某些字段做调整,甚⾄是重建索 引。

  ⽽做这些操作的时候,可能会对业务造成影响,甚⾄是停机调整等问题。

  由此,es提供了索引 别名来解决这些问题。 索引别名就像⼀个快捷⽅式或是软连接,可以指向⼀个或多个索引,也可 以给任意⼀个需要索引名的API来使⽤。别名的应⽤为程序提供了极⼤地灵活性。

1.查询别名

GET /_alias

  效果:

{
  "nba" : {
    "aliases" : { }
  },
  "test" : {
    "aliases" : { }
  },
  "wba" : {
    "aliases" : { }
  },
  "cba" : {
    "aliases" : { }
  },
  "contractinfo" : {
    "aliases" : { }
  },
  "book" : {
    "aliases" : { }
  },
  ".kibana_1" : {
    "aliases" : {
      ".kibana" : { }
    }
  }
}

  

2.新增别名

POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "nba",
        "alias": "nba_aliase"
      }
    }
  ]
}

  然后查询:

  GET /_alias

{
  "test" : {
    "aliases" : { }
  },
  "book" : {
    "aliases" : { }
  },
  ".kibana_1" : {
    "aliases" : {
      ".kibana" : { }
    }
  },
  "contractinfo" : {
    "aliases" : { }
  },
  "wba" : {
    "aliases" : { }
  },
  "cba" : {
    "aliases" : { }
  },
  "nba" : {
    "aliases" : {
      "nba_aliase" : { }
    }
  }
}

  针对性查询:

  GET /nba/_alias

{
  "nba" : {
    "aliases" : {
      "nba_aliase" : { }
    }
  }
}

  

3.删除索引

POST /_aliases
{
  "actions": [
    {
      "remove": {
        "index": "nba",
        "alias": "nba_aliase"
      }
    }
  ]
}

  

4.重命名索引

POST /_aliases
{
  "actions": [
    {
      "remove": {
        "index": "nba",
        "alias": "nba_aliase"
      }
    },
    {
      "add": {
        "index": "nba",
        "alias": "nba_aliase_2"
      }
    }
  ]
}

  

5.为多个索引新建一个别名

POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "cba",
        "alias": "new_aliase"
      }
    },
    {
      "add": {
        "index": "wba",
        "alias": "new_aliase"
      }
    }
  ]
}

  效果:

{
  "cba" : {
    "aliases" : {
      "new_aliase" : { }
    }
  },
  ".kibana_1" : {
    "aliases" : {
      ".kibana" : { }
    }
  },
  "test" : {
    "aliases" : { }
  },
  "wba" : {
    "aliases" : {
      "new_aliase" : { }
    }
  },
  "book" : {
    "aliases" : { }
  },
  "contractinfo" : {
    "aliases" : { }
  },
  "nba" : {
    "aliases" : {
      "nba_aliase_2" : { }
    }
  }
}

  

6.为一个索引建立多个别名

POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "nba",
        "alias": "nba_aliase"
      }
    },
    {
      "add": {
        "index": "nba",
        "alias": "nba_aliase_2"
      }
    }
  ]
}

  效果:

{
  "nba" : {
    "aliases" : {
      "nba_aliase" : { },
      "nba_aliase_2" : { }
    }
  }
}

  

7.通过别名查询索引

  分两种情况。

  别名对应一个固定的索引:

GET /nba_aliase

  效果:

{
  "nba" : {
    "aliases" : {
      "nba_aliase" : { },
      "nba_aliase_2" : { }
    },
    "mappings" : {
      "properties" : {
        "age" : {
          "type" : "integer"
        },
        "birthDay" : {
          "type" : "date"
        },
        "birthDayStr" : {
          "type" : "keyword"
        },
        "code" : {
          "type" : "text"
        },
        "country" : {
          "type" : "text"
        },
        "countryEn" : {
          "type" : "text"
        },
        "displayAffiliation" : {
          "type" : "text"
        },
        "displayName" : {
          "type" : "text"
        },
        "displayNameEn" : {
          "type" : "text"
        },
        "draft" : {
          "type" : "long"
        },
        "heightValue" : {
          "type" : "float"
        },
        "jerseyNo" : {
          "type" : "text"
        },
        "playYear" : {
          "type" : "long"
        },
        "playerId" : {
          "type" : "keyword"
        },
        "position" : {
          "type" : "text"
        },
        "schoolType" : {
          "type" : "text"
        },
        "teamCity" : {
          "type" : "text"
        },
        "teamCityEn" : {
          "type" : "text"
        },
        "teamConference" : {
          "type" : "keyword"
        },
        "teamConferenceEn" : {
          "type" : "keyword"
        },
        "teamName" : {
          "type" : "keyword"
        },
        "teamNameEn" : {
          "type" : "keyword"
        },
        "weight" : {
          "type" : "text"
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1586363166694",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "0smbQX5cRY-vyyHWMLtPXA",
        "version" : {
          "created" : "7020099"
        },
        "provided_name" : "nba"
      }
    }
  }
}

  一个别名对应多个索引:

GET /new_aliase

  效果:

{
  "cba" : {
    "aliases" : {
      "new_aliase" : { }
    },
    "mappings" : {
      "properties" : {
        "date" : {
          "type" : "date"
        },
        "jerse_no" : {
          "type" : "keyword"
        },
        "name" : {
          "type" : "text"
        },
        "play_year" : {
          "type" : "long"
        },
        "position" : {
          "type" : "text"
        },
        "team_name" : {
          "type" : "text"
        },
        "title" : {
          "type" : "text"
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1586188945307",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "vFg2Y6mMTZKyjEKNnT_Tvw",
        "version" : {
          "created" : "7020099"
        },
        "provided_name" : "cba"
      }
    }
  },
  "wba" : {
    "aliases" : {
      "new_aliase" : { }
    },
    "mappings" : {
      "properties" : {
        "age_range" : {
          "type" : "integer_range"
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1586187671398",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "p0SCnNhJQTyF1ZM5UMZHdg",
        "version" : {
          "created" : "7020099"
        },
        "provided_name" : "wba"
      }
    }
  }
}

 

二:使用别名

1.使用别名进行修改信息

  使用nba,因为,索引nba的别名,可以找到唯一的索引是nba

  找到要修改的数据:

GET /nba/_doc/1

  数据:

{
  "_index" : "nba",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "countryEn" : "United States",
    "teamName" : "老鹰",
    "birthDay" : 831182400000,
    "country" : "美国",
    "teamCityEn" : "Atlanta",
    "code" : "jaylen_adams",
    "displayAffiliation" : "United States",
    "displayName" : "杰伦 亚当斯",
    "schoolType" : "College",
    "teamConference" : "东部",
    "teamConferenceEn" : "Eastern",
    "weight" : "86.2 公斤",
    "teamCity" : "亚特兰大",
    "playYear" : 1,
    "jerseyNo" : "10",
    "teamNameEn" : "Hawks",
    "draft" : 2018,
    "displayNameEn" : "Jaylen Adams",
    "heightValue" : 1.88,
    "birthDayStr" : "1996-05-04",
    "position" : "后卫",
    "age" : 23,
    "playerId" : "1629121"
  }
}

  使用别名进行修改:

  讲年龄调大到25:

POST /nba_aliase/_doc/1
{
    "countryEn" : "United States",
    "teamName" : "老鹰",
    "birthDay" : 831182400000,
    "country" : "美国",
    "teamCityEn" : "Atlanta",
    "code" : "jaylen_adams",
    "displayAffiliation" : "United States",
    "displayName" : "杰伦 亚当斯",
    "schoolType" : "College",
    "teamConference" : "东部",
    "teamConferenceEn" : "Eastern",
    "weight" : "86.2 公斤",
    "teamCity" : "亚特兰大",
    "playYear" : 1,
    "jerseyNo" : "10",
    "teamNameEn" : "Hawks",
    "draft" : 2018,
    "displayNameEn" : "Jaylen Adams",
    "heightValue" : 1.88,
    "birthDayStr" : "1996-05-04",
    "position" : "后卫",
    "age" : 25,
    "playerId" : "1629121"
  }

  效果:

{
  "_index" : "nba",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 2,
  "_seq_no" : 566,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "countryEn" : "United States",
    "teamName" : "老鹰",
    "birthDay" : 831182400000,
    "country" : "美国",
    "teamCityEn" : "Atlanta",
    "code" : "jaylen_adams",
    "displayAffiliation" : "United States",
    "displayName" : "杰伦 亚当斯",
    "schoolType" : "College",
    "teamConference" : "东部",
    "teamConferenceEn" : "Eastern",
    "weight" : "86.2 公斤",
    "teamCity" : "亚特兰大",
    "playYear" : 1,
    "jerseyNo" : "10",
    "teamNameEn" : "Hawks",
    "draft" : 2018,
    "displayNameEn" : "Jaylen Adams",
    "heightValue" : 1.88,
    "birthDayStr" : "1996-05-04",
    "position" : "后卫",
    "age" : 25,
    "playerId" : "1629121"
  }
}

  

2.当别名对应多个索引,进行修改数据

  对将要写的索引添加一个标识:

POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "cba",
        "alias": "new_aliase",
        "is_write_index": true
      }
    },
    {
      "add": {
        "index": "wba",
        "alias": "new_aliase"
      }
    }
  ]
}

  要修改的数据如下:

{
  "_index" : "cba",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "蔡x坤",
    "team_name" : "勇⼠",
    "position" : "得分后卫",
    "play_year" : 10,
    "jerse_no" : "31",
    "title" : "打球最帅的明星",
    "date" : "2020-01-01"
  }
}

  修改:

  age提高:

POST /new_aliase/_doc/1
{
    "name" : "蔡x坤",
    "team_name" : "勇⼠",
    "position" : "得分后卫",
    "play_year" : 13,
    "jerse_no" : "31",
    "title" : "打球最帅的明星",
    "date" : "2020-01-01"
}

  查询效果:

{
  "_index" : "cba",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 2,
  "_seq_no" : 2,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "蔡x坤",
    "team_name" : "勇⼠",
    "position" : "得分后卫",
    "play_year" : 13,
    "jerse_no" : "31",
    "title" : "打球最帅的明星",
    "date" : "2020-01-01"
  }
}

  

原文地址:https://www.cnblogs.com/juncaoit/p/12741845.html