美文网首页
elasticsearch 数据管理

elasticsearch 数据管理

作者: 蜗牛ICU | 来源:发表于2019-03-30 00:07 被阅读0次

一、 文档:

 现在的开发过程中基本上都是基于将对象转 json 用来数据传输。通常将对象和文档是等价的。

二、 文档元数据:

节点 说明
_index 文档存储的地方
_type 文档代表的对象类
_id 文档的唯一标识

详细说明:

① _index :
    类似数据库
    
② _type :
    类似数据库中的表
    
三: _id :
    通过组合能够找到在库中的数据

详细使用方式:

① 使用自己的ID生成标识:
命令:

            PUT /{index}/{type}/{id}
            {
              "field": "value",
              ...
            }
    案例:
            PUT /website/blog/123
            {
              "title": "My first blog entry",
              "text":  "Just trying this out...",
              "date":  "2014/01/01"
            }

② 自增的ID:

        命令:
            POST /website/blog/
            {
              "title": "My second blog entry",
              "text":  "Still trying this out...",
              "date":  "2014/01/01"
            }

三 获取数据:

        命令:
            GET /website/blog/123?pretty
            //pretty 这个是将返回的数据格式化

如果只是想获取数据库中指定的字段:

        命令:
            GET /website/blog/123?_source=title,text

如果只想要获取属于自己的值:

        命令:  
            GET /website/blog/123/_source

④ 更新文档:

在数据库中数据是不可改变的,我们需要重建索引或替换。
        命令:
            PUT /website/blog/123
            {
              "title": "My first blog entry",
              "text":  "I am starting to get the hang of this...",
              "date":  "2014/01/02"
            }
        相应体:
            {
              "_index" :   "website",
              "_type" :    "blog",
              "_id" :      "123",
              "_version" : 2,
              "created":   false <1>
            }
        _version :版本会增加

五 创建文档:

确定文档是否是新建的。

第一种方法使用op_type查询参数:

    命令:
        PUT /website/blog/123?op_type=create
        { ... }

第二种方法是在URL后加/_create做为端点:

    命令:
        PUT /website/blog/123/_create
        { ... }

返回值说明:
201 created : 新建文档成功
409 Conflict :文档已经存在

⑥ 删除文档:

    命令:
        DELETE /website/blog/123

⑦ 更新文档:

    命令:  
        POST /website/blog/1/_update
        {
           "doc" : {
              "tags" : [ "testing" ],
              "views": 0
           }
        }

⑧ 批量获取文档:

    命令:
        POST /_mget
        {
           "docs" : [
              {
                 "_index" : "website",
                 "_type" :  "blog",
                 "_id" :    2
              },
              {
                 "_index" : "website",
                 "_type" :  "pageviews",
                 "_id" :    1,
                 "_source": "views"
              }
           ]
        }
    响应体:
    {
       "docs" : [
          {
             "_index" :   "website",
             "_id" :      "2",
             "_type" :    "blog",
             "found" :    true,
             "_source" : {
                "text" :  "This is a piece of cake...",
                "title" : "My first external blog entry"
             },
             "_version" : 10
          },
          {
             "_index" :   "website",
             "_id" :      "1",
             "_type" :    "pageviews",
             "found" :    true,
             "_version" : 2,
             "_source" : {
                "views" : 2
             }
          }
       ]
    }

如果查询的数据在一个_index中:


    命令:
        POST /website/blog/_mget
        {
           "docs" : [
              { "_id" : 2 },
              { "_type" : "pageviews", "_id" :   1 }
           ]
        }

如果在一个_index 和 _type 中,使用这种方式:

    命令:
        POST /website/blog/_mget
        {
           "ids" : [ "2", "1" ]
        }

⑨ 批量更新操作:
后期补充 !!!!

相关文章

网友评论

      本文标题:elasticsearch 数据管理

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