欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

GO語(yǔ)言操作Elasticsearch示例分享

 更新時(shí)間:2023年01月17日 08:42:26   作者:93年的老男孩  
這篇文章主要介紹了GO語(yǔ)言操作Elasticsearch示例分享的相關(guān)資料,需要的朋友可以參考下

Elasticsearch簡(jiǎn)介

Elasticsearch 是一個(gè)開(kāi)源的搜索引擎,建立在一個(gè)全文搜索引擎庫(kù) Apache Lucene™ 基礎(chǔ)之上。 Lucene 可以說(shuō)是當(dāng)下最先進(jìn)、高性能、全功能的搜索引擎庫(kù)–無(wú)論是開(kāi)源還是私有。

連接Elasticsearch

// 引入g~
~~~~~-elasticsearch
import (
    es8 "github.com/elastic/go-elasticsearch/v8"
)

// go-es配置
conf := es8.Config{
    Addresses: "http://127.0.0.1:9200",
    Username:"elastic",
    Password:"jloMQ7ZCTlcZUr_hmDoB",
}

// 創(chuàng)建
client, err := es8.NewClient(conf);
if err != nil{
    fmt.Println("=============  創(chuàng)建 elasticsearch 失敗  =============")
    return 
}

// 連接
_, err1 := client.Info()
if err1 != nil{
    fmt.Println("=============  連接 elasticsearch 失敗  =============")
    return 
}

創(chuàng)建索引

創(chuàng)建model結(jié)構(gòu)體

type Admin struct{
    Id int `gorm:"<-" json:"id"` 
    UserName string `gorm:"<-" json:"user_name"` 
    RealName string `gorm:"<-" json:"real_name"` 
    Mobile string `gorm:"<-" json:"mobile"` 
}

初始化model

admin := Admin{
    Id: 1,
    UserName: "test",
    RealName: "測(cè)試",
    Mobile: "15222222222",
}

創(chuàng)建索引

// 結(jié)構(gòu)體json序列化
str,err := json.Marshal(admin);
if err != nil{
    return ;
}

// 創(chuàng)建索引
res1, err1 := client.Index(
    "db.table",
    bytes.NewReader(str),
    client.Index.WithDocumentID("1"), // 索引ID
    client.Index.WithRefresh("true") //是否立即創(chuàng)建
);

搜索數(shù)據(jù)

創(chuàng)建返回結(jié)構(gòu)體

type EsResponse struct{
    Hits struct{
        Total struct{
            Value int `json:"value"` 
        } `json:"total"` 
        Hits []struct{
            Index string `json:"_index"` 
            Id string `json:"_id"` 
            Score float32 `json:"_score"` 
            Source map[string]any `json:"_source"` 
        } `json:"hits"` 
    } `json:"hits"` 
}

type EsData struct{
    Total int `json:"total"` 
    List []map[string]any `json:"list"` 
}

搜索數(shù)據(jù)

query := map[string]any{
    "query":map[string]any{
        "bool":map[string]any{
            "must":[]map[string]any{
                map[string]any{
                    "match_phrase":map[string]any{
                        "user_name": "test",
                    },
                },
                map[string]any{
                    "match_phrase":map[string]any{
                        "mobile": "15222222222",
                    },
                },
            },
        },
    },
}

str,err := json.Marshal(query);
if err != nil{
    return ;
}

res1,err1 := client.Search(client.Search.WithBody(bytes.NewReader(str)));
    
if err1 != nil{
    return ;
}

解析數(shù)據(jù)

var resData EsResponse;
err2 := json.NewDecoder(res1.Body).Decode(&resData);
if err2 != nil{
    return ;
}

var esData EsData;
esData.Total = resData.Hits.Total.Value;
    
for _, v := range resData.Hits.Hits {
    cache := v.Source
    cache["_index"] = v.Index;
    cache["_id"] = v.Id;
    cache["_score"] = v.Score;
    esData.List = append(esData.List,cache)
}

修改數(shù)據(jù)

單條修改

 update := map[string]any{
    "script": map[string]any{
        "source":"ctx._source.user_name='test1';ctx._source.mobile='15211111111';",
        "lang": "painless",
    },
}

str,err1 := json.Marshal(update)
if err1 != nil{
    return ;
}

res2,err2 := client.Update(
    "db.table",
    "1",
    bytes.NewReader(str),
    client.Update.WithRefresh("true")
)

if err2 != nil{
    return ;
}

批量修改

update := map[string]any{
    "query":map[string]any{
        "bool":map[string]any{
            "must":[]map[string]any{
                map[string]any{
                    "match_phrase":map[string]any{
                        "user_name": "test",
                    },
                },
                map[string]any{
                    "match_phrase":map[string]any{
                        "mobile": "15222222222",
                    },
                },
            },
        },
    },
    "script": map[string]any{
        "source":"ctx._source.user_name='test1';ctx._source.mobile='15211111111';",
        "lang": "painless",
    },
}

str,err1 := json.Marshal(update)
if err1 != nil{
    return ;
}

res2,err2 := client.UpdateByQuery(
    []string{
        "db.table",
    },
    client.UpdateByQuery.WithBody(bytes.NewReader(str)),
    client.UpdateByQuery.WithRefresh(true)
)

if err2 != nil{
    return ;
}

刪除數(shù)據(jù)

單條刪除

res,err := client.Delete(
    "db.table",
    "1",
    client.Delete.WithRefresh("true")
)
if err != nil{
    return ;
}

批量刪除

query := map[string]any{
    "query":map[string]any{
        "bool":map[string]any{
            "must":[]map[string]any{
                map[string]any{
                    "match_phrase":map[string]any{
                        "user_name": "test",
                    },
                },
                map[string]any{
                    "match_phrase":map[string]any{
                        "mobile": "15222222222",
                    },
                },
            },
        },
    },
}

str,err := json.Marshal(query);
if err != nil{
    return ;
}

res,err := client.DeleteByQuery(
    []string{
        "db.table",
    },
    bytes.NewReader(str),
    client.DeleteByQuery.WithRefresh(true)
)

到此這篇關(guān)于GO語(yǔ)言操作Elasticsearch示例分享的文章就介紹到這了,更多相關(guān)GO語(yǔ)言操作Elasticsearch內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • golang中bufio.SplitFunc的深入理解

    golang中bufio.SplitFunc的深入理解

    這篇文章主要給大家介紹了關(guān)于golang中bufio.SplitFunc的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用golang具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-10-10
  • 聊聊Golang的語(yǔ)言結(jié)構(gòu)和變量問(wèn)題

    聊聊Golang的語(yǔ)言結(jié)構(gòu)和變量問(wèn)題

    這篇文章主要介紹了Golang的語(yǔ)言結(jié)構(gòu)和變量問(wèn)題,在golang中定義變量的一般形式是使用 var 關(guān)鍵字,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2021-11-11
  • 使用gopkg.in/yaml.v3?解析YAML數(shù)據(jù)詳解

    使用gopkg.in/yaml.v3?解析YAML數(shù)據(jù)詳解

    這篇文章主要為大家介紹了使用gopkg.in/yaml.v3?解析YAML數(shù)據(jù)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • golang中單向channel的語(yǔ)法介紹

    golang中單向channel的語(yǔ)法介紹

    通過(guò)消息來(lái)共享數(shù)據(jù)是golang的一種設(shè)計(jì)哲學(xué),channel則是這種哲理的體現(xiàn)。下面這篇文章主要給大家介紹了關(guān)于golang中單向channel語(yǔ)法的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面來(lái)一起看看吧。
    2017-07-07
  • golang避免內(nèi)存溢出的方法

    golang避免內(nèi)存溢出的方法

    內(nèi)存溢出(Memory Overflow)是指程序在運(yùn)行時(shí)超出了分配給它的內(nèi)存限制,從而導(dǎo)致程序異?;虮罎⒌默F(xiàn)象,內(nèi)存溢出的問(wèn)題在任何編程語(yǔ)言中都可能出現(xiàn),Go 語(yǔ)言也不例外,本文給大家介紹了golang是如何解決內(nèi)存溢出的,需要的朋友可以參考下
    2024-09-09
  • Golang開(kāi)發(fā)之接口的具體使用詳解

    Golang開(kāi)發(fā)之接口的具體使用詳解

    在 Golang 中,接口是一種類型,它是由一組方法簽名組成的抽象集合。這篇文章主要為大家介紹了Golang接口的具體使用,希望對(duì)大家有所幫助
    2023-04-04
  • go?打包運(yùn)行文件在windows,liunx運(yùn)行

    go?打包運(yùn)行文件在windows,liunx運(yùn)行

    這篇文章主要介紹了go?打包運(yùn)行文件在windows,liunx運(yùn)行的相關(guān)資料,需要的朋友可以參考下
    2023-11-11
  • Golang項(xiàng)目在github創(chuàng)建release后自動(dòng)生成二進(jìn)制文件的方法

    Golang項(xiàng)目在github創(chuàng)建release后自動(dòng)生成二進(jìn)制文件的方法

    這篇文章主要介紹了Golang項(xiàng)目在github創(chuàng)建release后如何自動(dòng)生成二進(jìn)制文件,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-03-03
  • golang微服務(wù)框架kratos實(shí)現(xiàn)Socket.IO服務(wù)的方法

    golang微服務(wù)框架kratos實(shí)現(xiàn)Socket.IO服務(wù)的方法

    本文主要介紹了golang微服務(wù)框架kratos實(shí)現(xiàn)Socket.IO服務(wù)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • go語(yǔ)言題解LeetCode1128等價(jià)多米諾骨牌對(duì)的數(shù)量

    go語(yǔ)言題解LeetCode1128等價(jià)多米諾骨牌對(duì)的數(shù)量

    這篇文章主要為大家介紹了go語(yǔ)言題解LeetCode1128等價(jià)多米諾骨牌對(duì)的數(shù)量示例詳解,
    2022-12-12

最新評(píng)論