第一章第十五节:Elasticsearch之mapping

1、创建带属性的索引

keyword:不会被分词
text:可以子类型,进行精确匹配
date:要被格式化存储
PUT /myindex
{
  "mappings": {
    "properties": {
      "id":{"type": "long"},
      "age":{"type": "keyword"},
      "name":{"type": "text","fields": {"keyword":{"type":"keyword"}}},
      "createtime":{"type": "date","format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"}
    }
  }
}

#插入一条文档
POST /myindex/_doc
{
  "id":1001,
  "age":"男",
  "name":"menghb",
  "createtime":"2020-09-09 12:28:22"
}

2、查看索引的属性信息

GET /myindex/_mapping

响应数据:

{
  "myindex" : {
    "mappings" : {
      "properties" : {
        "age" : {
          "type" : "keyword"
        },
        "createtime" : {
          "type" : "date",
          "format" : "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
        },
        "id" : {
          "type" : "long"
        },
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword"
            }
          }
        }
      }
    }
  }
}

3、给索引新增一个字段

index:表示该字段是否能被索引到
PUT /myindex/_mapping
{
  "properties": {
    "email": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword"
        }
      },
      "index":true
    }
  }
}

4、更新索引字段

索引一旦创建,则不能更新,只能新建索引,把数据从旧索引中迁移到新索引中
#创建新索引
PUT /newbank/
{
  "mappings": {
    "properties": {
      "account_number": {
        "type": "long"
      },
      "address": {
        "type": "text"
      },
      "age": {
        "type": "integer"
      },
      "balance": {
        "type": "long"
      },
      "city": {
        "type": "text"
      },
      "email": {
        "type": "keyword"
      },
      "employer": {
        "type": "keyword"
      },
      "firstname": {
        "type": "keyword",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "gender": {
        "type": "keyword"
      },
      "lastname": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "state": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      }
    }
  }
}

#数据迁移,从bank索引迁移到newbank索引
POST _reindex
{
  "source": {
    "index": "bank",
    "type": "account"
  },
  "dest": {
    "index": "newbank"
  }
}
posted @ 2021-07-04 14:38  努力的校长  阅读(53)  评论(0编辑  收藏  举报