最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

GO語言操作Elasticsearch示例分享

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

Elasticsearch簡(jiǎn)介

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

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

相關(guān)文章

  • golang構(gòu)建HTTP服務(wù)的實(shí)現(xiàn)步驟

    golang構(gòu)建HTTP服務(wù)的實(shí)現(xiàn)步驟

    其實(shí)很多框架都是在 最簡(jiǎn)單的http服務(wù)上做擴(kuò)展的的,基本上都是遵循h(huán)ttp協(xié)議,本文主要介紹了golang構(gòu)建HTTP服務(wù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • GO 語言運(yùn)行環(huán)境的基礎(chǔ)知識(shí)

    GO 語言運(yùn)行環(huán)境的基礎(chǔ)知識(shí)

    這篇文章主要介紹了GO 語言運(yùn)行環(huán)境的基礎(chǔ)知識(shí)的相關(guān)資料,需要的朋友可以參考下
    2022-09-09
  • 對(duì)Golang import 導(dǎo)入包語法詳解

    對(duì)Golang import 導(dǎo)入包語法詳解

    今天小編就為大家分享一篇對(duì)Golang import 導(dǎo)入包語法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • Go語言中的range用法實(shí)例分析

    Go語言中的range用法實(shí)例分析

    這篇文章主要介紹了Go語言中的range用法,實(shí)例分析了range的功能與使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-02-02
  • 最新評(píng)論

    滦南县| 扬中市| 南开区| 大安市| 方正县| 黄冈市| 抚州市| 乌拉特前旗| 连南| 余庆县| 锡林郭勒盟| 建昌县| 郓城县| 湘西| 道真| 莆田市| 宜黄县| 池州市| 南京市| 桐乡市| 九台市| 夏河县| 泊头市| 五家渠市| 全南县| 抚松县| 水城县| 彭泽县| 治多县| 定边县| 琼结县| 通河县| 蒙山县| 新龙县| 柏乡县| 九江市| 封丘县| 井陉县| 永康市| 澄迈县| 延寿县|