美文网首页
Elasticsearch基本操作-增删改查

Elasticsearch基本操作-增删改查

作者: 洛奇lodge | 来源:发表于2019-06-27 16:39 被阅读0次

创建索引

一般创建索引,我们要指定分片和复制的数量,以及指定对字段进行mapping设置,mapping设置很重要,会影响后期的查询效率。

PUT test_create_index
{
    "settings": {
        "number_of_shards": 3,  // 分片
        "number_of_replicas": 1  // 复制
    },
    "mappings": {
        "test_type":{  // 指定类型
            "properties":{  // 定义字段的mapping
                "name": {
                    "type": "keyword"
                },
                "time": {
                    "type": "date",
                    "format": "epoch_millis"
                }
            }
        }
     }    
}

增加字段,并配置mapping

// 已配置mapping的字段,一般不可修改
PUT test_create_index/_mapping/test_type
{
  "properties": {
    "padd":{
      "type":"keyword"
     }
   }
}

删除索引

DELETE  website

插入文档

// 指定文档的id,如果索引不存在,会自动重新建索引,在插入
PUT test_create_index/test_type/1
{
    "name": "lodge",
    "time": "20190627142300"
}

// 由es自动生成id,如果索引不存在,会自动重新建索引,在插入
POST test_create_index/test_type
{
    "name": "lodge1",
    "time": "20190627122300"
}

更新文档

// 更新操作原理,先查询这个文档,进行修改,删除旧文档,插入新的文档
// 如果原来没有文档存在,那么这操作伟插入操作。如果原来没有文档存在,那么这操作为替换覆盖
PUT test_create_index/test_type/1
{
    "name": "lodge2",
    "time": "20190727122300"
}

// 这个更新操作,加上_create,是不覆盖原有的文档,会抛异常409
PUT test_create_index/test_type/1/_create
{
    "name": "lodge2",
    "time": "20190727122300"
}

//  这个更新操作,加上_update,是在原有的文档上进行修改,增加字段和值
POST test_create_index/test_type/1/_update
{
    "doc": {
        "passed": "20190631"
    }
}
//  这个更新操作,加上_update,是在原有的文档上进行修改,替换字段的某个值
POST test_create_index/test_type/1/_update
{
   "script" : "ctx._source.passed='swdhkf'"
}

// 更新操作,在高并发时候,经常会遇到冲突,数据幻读的问题
// 可以采取乐观锁并发控制,使用es自带的版本号控制。更新时候发现版本号不对,会抛异常409
PUT test_create_index/test_type/1/_update?version=2
{
    "name": "lodge2",
    "time": "20190727122300"
}

删除文档

// 删除指定id的文档
DELETE test_create_index/test_type/1

查询

// 查询索引,这操作只能使用curl
curl IP地址:端口号/_cat/indices?v

// 查询指定索引的信息(settings,mappings)
GET test_create_index

// 查询索引下指定类型的mapping配置
GET test_create_index/test_type/_mapping

// 查询文档数据量
GET test_create_index/test_type/_count

// 查询指定id文档,只显示文档数据
GET test_create_index/test_type/1/_source

// 查询一条指定id文档
GET test_create_index/test_type/1

// 查询多条指定id文档
POST test_create_index/_mget
{
   "ids" : [ "AWuXm5qe5IAaRF19nMIv", "1" ]
}

// 查询所有文档
GET test_create_index/_search

参考文档:
中文版:https://es.xiaoleilu.com/index.html
英文版:https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html

相关文章

  • Elasticsearch基本操作-增删改查

    创建索引 一般创建索引,我们要指定分片和复制的数量,以及指定对字段进行mapping设置,mapping设置很重要...

  • mongoDB数据库的简单CRUD操作

    mongodb数据库的操作 基本的增删改查 增 增加一条文档:db.sf.insert({ :" "}) 增加多条...

  • Java操作Hbase的基本操作

    最基本的入门展示 基本的增删改查操作集合

  • 增删改

    对于表中的操作,就是增删改查,查内容较多,这里先说增删改。 1.增(insert或者load) 即插入数据,多行插...

  • golang elasticsearch入门教程 --- 202

    本教程从go语言角度讲解如何对elasticsearch进行增删改查。 目前golang操作elasticsear...

  • 增删改查

    增删改查,是实现数据操作的基本功能。 在数据库的操作中,习惯把它分为两大类,数据查询(查)和数据更新(增、删、改)...

  • SQL查询结构总结

    SQL 增删改查 对数据库有修改的操作是:增删改 增 insert into 表名 values(); 删 del...

  • 函数和增删改查(运用)

    增删改查 (基本命令) 1 . 增 inset(position,value)append(value)exten...

  • 数组

    基本操作 数组的增删改查 二维数组

  • PDO操作

    基本操作 预处理(增、删、改)操作 预处理(查)操作

网友评论

      本文标题:Elasticsearch基本操作-增删改查

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