美文网首页
Es6 - 2基本创建、新增、修改

Es6 - 2基本创建、新增、修改

作者: 阿尔卡雷特 | 来源:发表于2018-11-28 16:26 被阅读0次

索引:默认为非结构化,可通过设置设置mappings将其转为结构化存储

  1. 建立结构化数据
    结构:http://ip:9200/book/novel/_mappings [put]
    novel - properties - 属性 : type: 属性类型
    反馈:{"acknowledged": true} 表示成功
    建立结构化数据

在 概览-信息-索引信息 中可查看_mappings内容

{
    "settings": { //指定索引的配置
        "number_of_shards": 3, //索引分片数
        "number_of_replicas": 1 //索引备份数
    },
    "mappings": { // 索引映射定义
        "man": { // 类型定义
            "properties": { //属性定义
                "name": {
                    "type": "text"
                },
                "country": {
                    "type": "keyword" // 关键词是不可切分的
                },
                "age": {
                    "type": "integer"
                },
                "date": {
                    "type": "date",
                    "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" // 指定格式,多个用||分隔,epoch_millis为时间戳
                }
            }
        }
    }
}
  1. 数据插入
    结构:http://127.0.0.1:9200/people/man/1 [put]
    http://127.0.0.1:9200/people/man/ [post]
    people为索引,man为类型,1为指定id(不设置则es自动生成),提交内容为属性json,如下:
{
    "name": "张三丰",
    "country": "China",
    "age": 30,
    "date": "1987-9-1"
}

反馈结果:

{
    "_index": "people",
    "_type": "man",
    "_id": "1",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}
  1. 数据修改
    结构:http://127.0.0.1:9200/people/man/1/_update [post]
    3.1 按结构直接修改 请求内容为要修改的属性值:
{
    "doc": {
        "country": "shaoLin"
    }
}

3.2 按脚本动态修改,请求内容:
直接修改

{
    "script": {
        "lang": "painless",
        "inline": "ctx._source.age += 10"
    }
}

或外置参数:

{
    "script": {
        "lang": "painless",
        "inline": "ctx._source.age = params.age",
        "params": {
            "age": 100
        }
    }
}

反馈结果:

{
    "_index": "people",
    "_type": "man",
    "_id": "1",
    "_version": 2,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    },
    "_seq_no": 1,
    "_primary_term": 1
}
  1. 删除数据
    4.1 删除文档
    结构:http://127.0.0.1:9200/people/man/1 [delete]
    请求结果:
{
    "_index": "people",
    "_type": "man",
    "_id": "1",
    "_version": 5,
    "result": "deleted",
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    },
    "_seq_no": 4,
    "_primary_term": 1
}

4.2 删除索引
结构:http://127.0.0.1:9200/people [delete]

相关文章

网友评论

      本文标题:Es6 - 2基本创建、新增、修改

      本文链接:https://www.haomeiwen.com/subject/trkaqqtx.html