zoukankan      html  css  js  c++  java
  • ES(Elastic Search)update操作设置无 docment时进行insert

    最近使用一套数据加工中间工具,查看es操作中的update操作。其中方法命名为updateOrInsert。但是没发现代码中有ES的insert方法调用。于是仔细分析了代码逻辑。

    经过一路追溯,直至ES java客户端请求发送代码。没找到insert相关内容。

    于是到官网查看究竟,可官网对 java Client相关说明比较少。查看不到具体api的说明。于是回到代码调用处:

    String jsonText = jsonBuild.endObject().string();
                    UpdateRequest request = (UpdateRequest)esClient.prepareUpdate(xxx.getDatabase(), xxx.getTable(), docId).setDoc(jsonText).setDetectNoop(true).setDocAsUpsert(true).setRetryOnConflict(this.retryOnConflict).request();
                    esClient.update(request).get();

    代码中属于链式调用,由于太长没有换行,竟然没看到后边的setDetectNoop,setDocAsUpsert参数的调用,于是思考,javaClient只是封装和转换了调用请求,于是再回到官网查看Document APIs,找到update操作的说明,就有了下边关于 Detecting Noop Updates 以及 Upserts说明:

    Detecting noop updatesedit

    If doc is specified its value is merged with the existing _source. By default the document is only reindexed if the new _source field differs from the old. Setting detect_noop to false will cause Elasticsearch to always update the document even if it hasn’t changed. For example:

    curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
        "doc" : {
            "name" : "new_name"
        },
        "detect_noop": false
    }'

    上边这段的意思是当更新的文档发生变化时进行更新,如果为fasle,则始终更新。

    Upsertsedit

    If the document does not already exist, the contents of the upsert element will be inserted as a new document. If the document does exist, then the script will be executed instead:

    curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
        "script" : {
            "inline": "ctx._source.counter += count",
            "params" : {
                "count" : 4
            }
        },
        "upsert" : {
            "counter" : 1
        }
    }'

    scripted_upsertedit

    If you would like your script to run regardless of whether the document exists or not — i.e. the script handles initializing the document instead of the upsert element — then set scripted_upsert to true:

    curl -XPOST 'localhost:9200/sessions/session/dh3sgudg8gsrgl/_update' -d '{
        "scripted_upsert":true,
        "script" : {
            "id": "my_web_session_summariser",
            "params" : {
                "pageViewEvent" : {
                    "url":"foo.com/bar",
                    "response":404,
                    "time":"2014-01-01 12:32"
                }
            }
        },
        "upsert" : {}
    }'

    doc_as_upsertedit

    Instead of sending a partial doc plus an upsert doc, setting doc_as_upsert to true will use the contents of doc as the upsert value:

    curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
        "doc" : {
            "name" : "new_name"
        },
        "doc_as_upsert" : true
    }'

     

      上边描述:upsert参数的使用,有三种方式:

    1. 指定upsert内容
    2. 指定打开脚本upsert开关使用脚本处理upsert
    3. 使用文档内容做为upsert参数,则打开 doc_as_upsert。显然我们上边所说的javaClient调用中就是使用的 doc_as_upsert,这样当文档不存在时候,就会将传递过来的文档内容insert进去。达到update or Insert 目的。

      因此,对于ES java Clent使用不熟的完全可以参照 api命名查找官网的 api说明,java客户端是用java语言对其进行了封装。仔细阅读便知道调用代码逻辑的含义了。仅此记录,为不熟悉ES的其他使用者 引个路子。

    官网内容链接:https://www.elastic.co/guide/en/elasticsearch/reference/2.1/docs-update.html#upserts

    可以根据自己使用的ES版本进行切换查看。

  • 相关阅读:
    VysorPro助手
    Play 2D games on Pixel running Android Nougat (N7.1.2) with Daydream View VR headset
    Play 2D games on Nexus 6P running Android N7.1.1 with Daydream View VR headset
    Native SBS for Android
    ADB和Fastboot最新版的谷歌官方下载链接
    How do I install Daydream on my phone?
    Daydream Controller手柄数据的解析
    蓝牙BLE传输性能及延迟分析
    VR(虚拟现实)开发资源汇总
    Android(Java)控制GPIO的方法及耗时分析
  • 原文地址:https://www.cnblogs.com/sunlightlee/p/10858556.html
Copyright © 2011-2022 走看看