zoukankan      html  css  js  c++  java
  • elasticsearch 基础语句

    1.  doucument id 的两种生成方式

    自动生成document id
    自动生成的id,长度为20个字符,URL安全,base64编码,GUID,分布式系统并行生成时不可能会发生冲突

    POST /test_index/test_type (这里没有标识id)
    {
    "test_content": "my test"
    }

    GET /test_index/test_type/_search


    手动指定document id

    PUT /test_index/test_type/2
    {
    "test_content":"ding-jiang"
    }

    GET /test_index/test_type/2

    注意点:1 我们的{}不能和 PUT写到一行 否则会报错 failed to parse, document is empty
        2 GET /test_index/test_type/_search 会得到该节点下所有的数据列表
    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
    2.    doucument的_source元数据以及定制返回结果解析

    put /test_index/test_type/1
    {
    "test":"test11",
    "test1":"test22"
    }
    GET /test_index/test_type/1

    返回
    {
    "_index": "test_index",
    "_type": "test_type",
    "_id": "1",
    "_version": 2,
    "found": true,
    "_source": {
    "test": "test11",
    "test1": "test22"
    }
    }

    _source元数据:就是创建document时我们存入的数据

    如果我们不想返回全部的数据,只想返回一部分数据,比如有test 和test11 而我们只用test那么

    GET /test_index/test_type/1?_source=test 则_source只返回
    {
    "_index": "test_index",
    "_type": "test_type",
    "_id": "1",
    "_version": 2,
    "found": true,
    "_source": {
    "test": "test11"
    }
    }

    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------

    3.    doucument的全量替换 强制创建和 delete 机制

    1、document的全量替换
    (1)语法与创建文档是一样的,如果document id不存在,那么就是创建;如果document id已经存在,那么就是全量替换操作,替换document的json串内容
    (2)document是不可变的,如果要修改document的内容,第一种方式就是全量替换,直接对document重新建立索引,替换里面所有的内容
    (3)es会将老的document标记为deleted,然后新增我们给定的一个document,当我们创建越来越多的document的时候,es会在适当的时机在后台自动删除标记为deleted的document

    就是说全量替换会将_source的内容替换,但是之前的内容并没有从库中删除而是被标记为了deleted,es在恰当的时候会在动删除这些deleted数据(document)
    创建的标识就是GET /test_index/test_type/1 中的_version会加1
    {
    "_index": "test_index",
    "_type": "test_type",
    "_id": "1",
    "_version": 3,
    "found": true,
    "_source": {
    "test": "test11",
    "test1": "test22",
    "test_version": 3
    }
    }

    2. 强制创建
    (1)创建文档与全量替换的语法是一样的,有时我们只是想新建文档,不想替换文档,如果强制进行创建呢?
    PUT /test_index/test_type/1/_create
    {
    "test333":"ding"
    }
    会报错 因为id是不能重复的 如果我们想创建那么id必须修改
    {
    "error": {
    "root_cause": [
    {
    "type": "version_conflict_engine_exception",
    "reason": "[test_type][1]: version conflict, document already exists (current version [3])",
    "index_uuid": "XtO4uL9HTo2v34qmximdLg",
    "shard": "3",
    "index": "test_index"
    }
    ],
    "type": "version_conflict_engine_exception",
    "reason": "[test_type][1]: version conflict, document already exists (current version [3])",
    "index_uuid": "XtO4uL9HTo2v34qmximdLg",
    "shard": "3",
    "index": "test_index"
    },
    "status": 409
    }

    3. 删除

    DELETE /test_index/test_type/11
    GET /test_index/test_type/11

    这个和全量替换一样不会立即物理删除,只会将其标记为deleted,当数据越来越多的时候,在后台自动删除

    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------

    4   partial update

    什么是partial update?

    PUT /index/type/id,创建文档&替换文档,一样的语法

    post /index/type/id/_update
    {
    "doc": {
    "要修改的少数几个field即可,不需要全量的数据"
    }
    }

     例如:

    POST /test_index/test_type/8/_update
    {
    "doc":{
    "test_field":"partial update",
    "test2":"test22"
    }
    }

    GET /test_index/test_type/8

    {
    "_index": "test_index",
    "_type": "test_type",
    "_id": "8",
    "_version": 7,
    "found": true,
    "_source": {
    "test_field": "partial update",
    "test2": "test22"
    }
    }

  • 相关阅读:
    产品经理之PRD详解(非原创)
    编写代码的「八荣八耻」- 以开关上线为荣,以自信编码为耻
    安装社区版git仓库
    【干货分享】大话团队的GIT分支策略进化史
    Android IPC机制(一)开启多进程
    使用adb命令通过IP地址连接手机
    一篇文章了解Github和Git教程-AndroidStudio上传Github教程
    sublime实现markdown浏览器预览
    idea上maven使用心得(三)——用pom.xml添加jar包
    idea解决Maven jar依赖冲突(四)
  • 原文地址:https://www.cnblogs.com/studyitskill/p/7804451.html
Copyright © 2011-2022 走看看