亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

elasticsearch中怎么使用update更新文檔

發布時間:2021-12-16 10:04:29 來源:億速云 閱讀:266 作者:iii 欄目:大數據

這篇文章主要介紹“elasticsearch中怎么使用update更新文檔”,在日常操作中,相信很多人在elasticsearch中怎么使用update更新文檔問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”elasticsearch中怎么使用update更新文檔”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

文檔

添加數據

如果數據不存在,會自動創建

rst, _ := client.Index().Index("user").BodyJson(&User{Name: "hnatao", Age: 20}).Do(ctx)
buf, _ := json.Marshal(rst)
fmt.Println(string(buf))

返回

{
    "_index": "user",
    "_type": "_doc",
    "_id": "iL1nWHQBIsMSghaJZ0p9",
    "_version": 1,
    "result": "created",
    "_shards": { "total": 4, "successful": 1, "failed": 0 },
    "_primary_term": 1
}

添加指定 id 的數據

指定 _id = "1"

rst, _ := client.Index().Index("user").Id("1").BodyJson(&User{Name: "lqt", Age: 22}).Do(ctx)
buf, _ := json.Marshal(rst)
fmt.Println(string(buf))

返回

{
    "_index": "user",
    "_type": "_doc",
    "_id": "1",
    "_version": 1,
    "result": "created",
    "_shards": { "total": 4, "successful": 1, "failed": 0 },
    "_seq_no": 1,
    "_primary_term": 1
}

更新數據

_id = "1" 已經存在,修改年齡,該操作會導致其他字段為空,因為該操作屬于覆蓋操作

rst, _ := client.Index().Index("user").Id("1").BodyJson(&User{Age: 23}).Do(ctx)
buf, _ := json.Marshal(rst)
fmt.Println(string(buf))

返回

{
    "_index": "user",
    "_type": "_doc",
    "_id": "1",
    "_version": 2,
    "result": "updated",
    "_shards": { "total": 4, "successful": 1, "failed": 0 },
    "_seq_no": 2,
    "_primary_term": 1
}

使用_update更新文檔

使用map[string]interface{}更新字段

rst, _ := client.Update().Index("user").Id("1").Doc(map[string]interface{}{"age":25}).Do(ctx)
buf, _ := json.Marshal(rst)
fmt.Println(string(buf))

返回

{
    "_index": "user",
    "_type": "_doc",
    "_id": "1",
    "_version": 7,
    "result": "updated",
    "_shards": { "total": 4, "successful": 1, "failed": 0 },
    "_seq_no": 7,
    "_primary_term": 1
}

使用_update_by_query更新文檔

q := elastic.NewMatchQuery("_id", "1")
sc := elastic.NewScript("ctx._source.age=21")
rst, _ := client.UpdateByQuery("user").Query(q).Script(sc).Do(ctx)
buf, _ := json.Marshal(rst)
fmt.Println(string(buf))

返回

{
    "took": 5,
    "timed_out": false,
    "total": 1,
    "updated": 1,
    "deleted": 0,
    "batches": 1,
    "version_conflicts": 0,
    "noops": 0,
    "retries": { "bulk": 0, "search": 0 },
    "throttled": "",
    "throttled_millis": 0,
    "requests_per_second": -1,
    "throttled_until": "",
    "throttled_until_millis": 0,
    "failures": []
}

通過 _bulk 批量添加文檔

bulkReq1 := elastic.NewBulkIndexRequest().Id("2").Doc(&User{Name: "張三", Age: 21})
bulkReq2 := elastic.NewBulkIndexRequest().Id("3").Doc(&User{Name: "李四", Age: 22})
rst, _ := client.Bulk().Index("user").Add(bulkReq1, bulkReq2).Do(ctx)
buf, _ := json.Marshal(rst)
fmt.Println(string(buf))

返回

{
    "took": 3,
    "items": [
        {
            "index": {
                "_index": "user",
                "_type": "_doc",
                "_id": "2",
                "_version": 1,
                "result": "created",
                "_shards": { "total": 4, "successful": 1, "failed": 0 },
                "_seq_no": 19,
                "_primary_term": 1,
                "status": 201
            }
        },
        {
            "index": {
                "_index": "user",
                "_type": "_doc",
                "_id": "3",
                "_version": 1,
                "result": "created",
                "_shards": { "total": 4, "successful": 1, "failed": 0 },
                "_seq_no": 20,
                "_primary_term": 1,
                "status": 201
            }
        }
    ]
}

通過 _bulk 批量更新文檔

bulkReq1 := elastic.NewBulkUpdateRequest().Index("user").Id("2").Doc(map[string]interface{}{"age": 31})
bulkReq2 := elastic.NewBulkUpdateRequest().Index("user").Id("3").Doc(map[string]interface{}{"age": 31})
rst, _ := client.Bulk().Add(bulkReq1, bulkReq2).Do(ctx)
buf, _ := json.Marshal(rst)
fmt.Println(string(buf))

返回

{
    "took": 7,
    "items": [
        {
            "update": {
                "_index": "user",
                "_type": "_doc",
                "_id": "2",
                "_version": 2,
                "result": "updated",
                "_shards": { "total": 4, "successful": 1, "failed": 0 },
                "_seq_no": 21,
                "_primary_term": 1,
                "status": 200
            }
        },
        {
            "update": {
                "_index": "user",
                "_type": "_doc",
                "_id": "3",
                "_version": 2,
                "result": "updated",
                "_shards": { "total": 4, "successful": 1, "failed": 0 },
                "_seq_no": 22,
                "_primary_term": 1,
                "status": 200
            }
        }
    ]
}

通過 _bulk 批量刪除文檔

bulkReq1 := elastic.NewBulkDeleteRequest().Index("user").Id("2")
bulkReq2 := elastic.NewBulkDeleteRequest().Index("user").Id("3")
rst, _ := client.Bulk().Add(bulkReq1, bulkReq2).Do(ctx)
buf, _ := json.Marshal(rst)
fmt.Println(string(buf))

返回

{
    "took": 130,
    "items": [
        {
            "delete": {
                "_index": "user",
                "_type": "_doc",
                "_id": "2",
                "_version": 3,
                "result": "deleted",
                "_shards": { "total": 4, "successful": 1, "failed": 0 },
                "_seq_no": 23,
                "_primary_term": 1,
                "status": 200
            }
        },
        {
            "delete": {
                "_index": "user",
                "_type": "_doc",
                "_id": "3",
                "_version": 3,
                "result": "deleted",
                "_shards": { "total": 4, "successful": 1, "failed": 0 },
                "_seq_no": 24,
                "_primary_term": 1,
                "status": 200
            }
        }
    ]
}

按照 _id 升序排序,取前 2 個數據

rst, _ := client.Search().Index("user").Sort("_id", false).Size(2).From(0).Do(ctx)
buf, _ := json.Marshal(rst)
fmt.Println(string(buf))

返回

{
    "took": 439,
    "hits": {
        "total": { "value": 8, "relation": "eq" },
        "hits": [
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "5",
                "_seq_no": null,
                "_primary_term": null,
                "sort": ["1"],
                "_source": { "name": "lqt", "age": 21 }
            },
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "4",
                "_seq_no": null,
                "_primary_term": null,
                "sort": ["2"],
                "_source": { "name": "張三", "age": 21 }
            }
        ]
    },
    "_shards": { "total": 1, "succeful": 1, "failed": 0 }
}

按照字段值排序

年齡降序,_id升序,前 5 條數據

rst, _ := client.Search().Index("user").SortBy(elastic.NewFieldSort("age").Desc(), elastic.NewFieldSort("_id").Asc()).Size(5).From(0).Do(ctx)
buf, _ := json.Marshal(rst)
fmt.Println(string(buf))

返回

{
    "hits": {
        "total": { "value": 8, "relation": "eq" },
        "hits": [
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "5",
                "_seq_no": null,
                "_primary_term": null,
                "sort": [24, "5"],
                "_source": { "name": "張學友", "age": 24 }
            },
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "4",
                "_seq_no": null,
                "_primary_term": null,
                "sort": [23, "4"],
                "_source": { "name": "劉德華", "age": 23 }
            },
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "3",
                "_seq_no": null,
                "_primary_term": null,
                "sort": [22, "3"],
                "_source": { "name": "李四", "age": 22 }
            },
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "1",
                "_seq_no": null,
                "_primary_term": null,
                "sort": [21, "1"],
                "_source": { "name": "lqt", "age": 21 }
            },
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "2",
                "_seq_no": null,
                "_primary_term": null,
                "sort": [21, "2"],
                "_source": { "name": "張三", "age": 21 }
            }
        ]
    },
    "_shards": { "total": 1, "successful": 1, "failed": 0 }
}

查詢結果只展示部分字段

rst, _ := client.Search().Index("user").FilterPath("hits.hits._id", "hits.hits._source.name").Do(ctx)
buf, _ := json.Marshal(rst)
fmt.Println(string(buf))

返回

{
    "hits": {
        "hits": [
            {
                "_id": "1",
                "_seq_no": null,
                "_primary_term": null,
                "_source": { "name": "lqt" }
            },
            {
                "_id": "2",
                "_seq_no": null,
                "_primary_term": null,
                "_source": { "name": "張三" }
            },
            {
                "_id": "3",
                "_seq_no": null,
                "_primary_term": null,
                "_source": { "name": "李四" }
            },
            {
                "_id": "4",
                "_seq_no": null,
                "_primary_term": null,
                "_source": { "name": "劉德華" }
            },
            {
                "_id": "5",
                "_seq_no": null,
                "_primary_term": null,
                "_source": { "name": "張學友" }
            }
        ]
    }
}

根據_id查詢數據

rst, _ := client.Get().Index("user").Id("1").Do(ctx)
buf, _ := json.Marshal(rst)
fmt.Println(string(buf))

返回

{
    "_index": "user",
    "_type": "_doc",
    "_id": "1",
    "_uid": "",
    "_routing": "",
    "_parent": "",
    "_version": 5,
    "_seq_no": 5,
    "_primary_term": 1,
    "_source": { "name": "", "age": 23 },
    "found": true
}

到此,關于“elasticsearch中怎么使用update更新文檔”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

乐陵市| 礼泉县| 洛川县| 高青县| 阳城县| 裕民县| 松原市| 醴陵市| 桐柏县| 新安县| 明光市| 广元市| 连州市| 东港市| 石柱| 古蔺县| 神农架林区| 肥城市| 建宁县| 鹿泉市| 岳普湖县| 建德市| 依安县| 米泉市| 盐城市| 屏山县| 临泽县| 龙海市| 邓州市| 封丘县| 湘阴县| 上饶县| 茶陵县| 廉江市| 景德镇市| 佛山市| 平阳县| 漯河市| 辉县市| 灌南县| 肃南|