栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

常规操作elasticSearch映射Mapping

常规操作elasticSearch映射Mapping

常规操作elasticSearch映射Mapping

定义映射相当于定义数据字段的类型

查看指定索引的映射:
GET /bank/_mapping
创建索引指定数据的映射
## create mapping
PUT /my_index
{
  "mappings": {
    "properties": {
      "age":{"type":"integer"},
      "email":{"type": "keyword"},
      "name":{"type": "text"}
    }
  }
}

返回值
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "my_index"
}



索引增加属性映射
PUT /my_index/_mapping
{ 
  "properties": { 
    "employee-id": { 
      "type": "keyword", 
      "index": false
    }
  }
}


返回值:
{
  "acknowledged" : true
}

映射属性的index是控制是否被索引。不填写,默认是被索引。即不被检索。

对于已存在的字段不支持更新
{
  "error": {
    "root_cause": [
      {
        "type": "mapper_parsing_exception",
        "reason": "No type specified for field [desc]"
      }
    ],
    "type": "mapper_parsing_exception",
    "reason": "No type specified for field [desc]"
  },
  "status": 400
}
我们操作已存在的字段发现会报错,也就是如果需要调整映射,不能直接操作,需要新建索引,指定好索引后,迁移数据完成。 修改映射

修改elasticSearch的bank测试索引库

1.查看原先数据mapping
GET /bank/_mapping

得到

{
  "bank" : {
    "mappings" : {
      "properties" : {
        "account_number" : {
          "type" : "long"
        },
        "address" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "age" : {
          "type" : "long"
        },
        "balance" : {
          "type" : "long"
        },
        "city" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "email" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "employer" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "firstname" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "gender" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "lastname" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "state" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}

2.构建新索引映射
先起一个新索引名字  写好mapping 把上面查询得到的properties 折叠后粘贴下来 放入mapping中 修改成我们需要的映射
PUT /newbank
{
  "mappings": {}
}

粘贴进去修改:
PUT /newbank
{
  "mappings": {
    "properties": {
      "account_number": {
        "type": "long"
      },
      "address": {
        "type": "text"
      },
      "age": {
        "type": "integer"
      },
      "balance": {
        "type": "long"
      },
      "city": {
        "type": "keyword"
      },
      "email": {
        "type": "keyword"
      },
      "employer": {
        "type": "keyword"
      },
      "firstname": {
        "type": "text"
      },
      "gender": {
        "type": "keyword"
      },
      "lastname": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "state": {
        "type": "text"
      }
    }
  }
}

执行得到:

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "newbank"
}

3.数据迁移
POST _reindex
{
  "source": {
    "index":"bank",
    "type": "account"
  },
  "dest":{
    "index":"newbank"
  }
}
结果:
#! Deprecation: [types removal] Specifying types in reindex requests is deprecated.
{
  "took" : 1073,
  "timed_out" : false,
  "total" : 1000,
  "updated" : 0,
  "created" : 1000,
  "deleted" : 0,
  "batches" : 1,
  "version_conflicts" : 0,
  "noops" : 0,
  "retries" : {
    "bulk" : 0,
    "search" : 0
  },
  "throttled_millis" : 0,
  "requests_per_second" : -1.0,
  "throttled_until_millis" : 0,
  "failures" : [ ]
}

迁移语句解释:

POST _reindex #执行迁移语句
{
“source”: { #数据源
“index”:“bank”, #原数据的索引
“type”: “account” #原数据的类型(如果原先数据有类型就要对应上源数据的类型8.0以后不再有类型 迁移数据)
},
“dest”:{ #目标源
“index”:“newbank” #目标索引
}
}

检验迁移数据

GET /newbank/_search
{
  "took" : 625,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1000,
      "relation" : "eq"
    },
   ...   

数据迁移成功

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/389476.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号