校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁(yè) > 知識(shí)庫(kù) > golang操作elasticsearch的實(shí)現(xiàn)

golang操作elasticsearch的實(shí)現(xiàn)

熱門(mén)標(biāo)簽:智能電銷(xiāo)機(jī)器人營(yíng)銷(xiāo) 福州鐵通自動(dòng)外呼系統(tǒng) 賺地圖標(biāo)注的錢(qián)犯法嗎 地圖標(biāo)注測(cè)試 澳門(mén)防封電銷(xiāo)卡 長(zhǎng)沙ai機(jī)器人電銷(xiāo) 烏魯木齊人工電銷(xiāo)機(jī)器人系統(tǒng) 濮陽(yáng)自動(dòng)外呼系統(tǒng)代理 廣東語(yǔ)音外呼系統(tǒng)供應(yīng)商

1、前提

1.1 docker 安裝elasticsearch

查詢elasticsearch 版本

docker search elasticsearch

將對(duì)應(yīng)的版本拉到本地

docker.elastic.co/elasticsearch/elasticsearch:7.3.0

創(chuàng)建一個(gè)網(wǎng)絡(luò)

docker network create esnet

啟動(dòng)容器

docker run --name es -p 9200:9200 -p 9300:9300 --network esnet -e "discovery.type=single-node" bdaab402b220

1.2這里過(guò)后就可以去寫(xiě)go代碼 為了直觀搞了個(gè)可視化工具 ElisticHD 這里使用docker 部署

docker run -p 9800:9800 -d --link es:demo --network esnet -e "discovery.type=single-node" containerize/elastichd

可以試一下界面還是很美觀的

2、golang 實(shí)現(xiàn)elasticsearch 簡(jiǎn)單的增刪改查

直接上代碼:

package main

import (
  "context"
  "encoding/json"
  "fmt"
  "github.com/olivere/elastic/v7"
  "reflect"
)

var client *elastic.Client
var host = "http://ip:port"

type Employee struct {
  FirstName string  `json:"first_name"`
  LastName string  `json:"last_name"`
  Age    int   `json:"age"`
  About   string  `json:"about"`
  Interests []string `json:"interests"`
}

//初始化
func init() {
  //errorlog := log.New(os.Stdout, "APP", log.LstdFlags)
  var err error
      //這個(gè)地方有個(gè)小坑 不加上elastic.SetSniff(false) 會(huì)連接不上 
  client, err = elastic.NewClient(elastic.SetSniff(false), elastic.SetURL(host))
  if err != nil {
    panic(err)
  }
  _,_,err = client.Ping(host).Do(context.Background())
  if err != nil {
    panic(err)
  }
  //fmt.Printf("Elasticsearch returned with code %d and version %s\n", code, info.Version.Number)

  _,err = client.ElasticsearchVersion(host)
  if err != nil {
    panic(err)
  }
  //fmt.Printf("Elasticsearch version %s\n", esversion)

}

/*下面是簡(jiǎn)單的CURD*/

//創(chuàng)建
func create() {

  //使用結(jié)構(gòu)體
  e1 := Employee{"Jane", "Smith", 32, "I like to collect rock albums", []string{"music"}}
  put1, err := client.Index().
    Index("megacorp").
    Type("employee").
    Id("1").
    BodyJson(e1).
    Do(context.Background())
  if err != nil {
    panic(err)
  }
  fmt.Printf("Indexed tweet %s to index s%s, type %s\n", put1.Id, put1.Index, put1.Type)

  //使用字符串
  e2 := `{"first_name":"John","last_name":"Smith","age":25,"about":"I love to go rock climbing","interests":["sports","music"]}`
  put2, err := client.Index().
    Index("megacorp").
    Type("employee").
    Id("2").
    BodyJson(e2).
    Do(context.Background())
  if err != nil {
    panic(err)
  }
  fmt.Printf("Indexed tweet %s to index s%s, type %s\n", put2.Id, put2.Index, put2.Type)

  e3 := `{"first_name":"Douglas","last_name":"Fir","age":35,"about":"I like to build cabinets","interests":["forestry"]}`
  put3, err := client.Index().
    Index("megacorp").
    Type("employee").
    Id("3").
    BodyJson(e3).
    Do(context.Background())
  if err != nil {
    panic(err)
  }
  fmt.Printf("Indexed tweet %s to index s%s, type %s\n", put3.Id, put3.Index, put3.Type)

}


//查找
func gets() {
  //通過(guò)id查找
  get1, err := client.Get().Index("megacorp").Type("employee").Id("2").Do(context.Background())
  if err != nil {
    panic(err)
  }
  if get1.Found {
    fmt.Printf("Got document %s in version %d from index %s, type %s\n", get1.Id, get1.Version, get1.Index, get1.Type)
    var bb Employee
    err:=json.Unmarshal(get1.Source,bb)
    if err!=nil{
      fmt.Println(err)
    }
    fmt.Println(bb.FirstName)
    fmt.Println(string(get1.Source))
  }

}
//
//刪除
func delete() {

  res, err := client.Delete().Index("megacorp").
    Type("employee").
    Id("1").
    Do(context.Background())
  if err != nil {
    println(err.Error())
    return
  }
  fmt.Printf("delete result %s\n", res.Result)
}
//
//修改
func update() {
  res, err := client.Update().
    Index("megacorp").
    Type("employee").
    Id("2").
    Doc(map[string]interface{}{"age": 88}).
    Do(context.Background())
  if err != nil {
    println(err.Error())
  }
  fmt.Printf("update age %s\n", res.Result)

}
//
////搜索
func query() {
  var res *elastic.SearchResult
  var err error
  //取所有
  res, err = client.Search("megacorp").Type("employee").Do(context.Background())
  printEmployee(res, err)

  //字段相等
  q := elastic.NewQueryStringQuery("last_name:Smith")
  res, err = client.Search("megacorp").Type("employee").Query(q).Do(context.Background())
  if err != nil {
    println(err.Error())
  }
  printEmployee(res, err)



  //條件查詢
  //年齡大于30歲的
  boolQ := elastic.NewBoolQuery()
  boolQ.Must(elastic.NewMatchQuery("last_name", "smith"))
  boolQ.Filter(elastic.NewRangeQuery("age").Gt(30))
  res, err = client.Search("megacorp").Type("employee").Query(q).Do(context.Background())
  printEmployee(res, err)

  //短語(yǔ)搜索 搜索about字段中有 rock climbing
  matchPhraseQuery := elastic.NewMatchPhraseQuery("about", "rock climbing")
  res, err = client.Search("megacorp").Type("employee").Query(matchPhraseQuery).Do(context.Background())
  printEmployee(res, err)

  //分析 interests
  aggs := elastic.NewTermsAggregation().Field("interests")
  res, err = client.Search("megacorp").Type("employee").Aggregation("all_interests", aggs).Do(context.Background())
  printEmployee(res, err)

}
//
////簡(jiǎn)單分頁(yè)
func list(size,page int) {
  if size  0 || page  1 {
    fmt.Printf("param error")
    return
  }
  res,err := client.Search("megacorp").
    Type("employee").
    Size(size).
    From((page-1)*size).
    Do(context.Background())
  printEmployee(res, err)

}
//
//打印查詢到的Employee
func printEmployee(res *elastic.SearchResult, err error) {
  if err != nil {
    print(err.Error())
    return
  }
  var typ Employee
  for _, item := range res.Each(reflect.TypeOf(typ)) { //從搜索結(jié)果中取數(shù)據(jù)的方法
    t := item.(Employee)
    fmt.Printf("%#v\n", t)
  }
}

func main() {
  create()
  delete()
  update()
  gets()
  query()
  list(2,1)
}

有一個(gè)小坑要注意在代碼中已經(jīng)注釋了,如果沒(méi)有添加就會(huì)有下面錯(cuò)誤

no active connection found: no Elasticsearch node available

解決

Docker No Elastic Node Aviable

關(guān)閉sniff模式;或者設(shè)置es的地址為 publish_address 地址

代碼設(shè)置 sniff 為false

到此這篇關(guān)于golang 操作 elasticsearch的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)golang操作elasticsearch內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • Django對(duì)接elasticsearch實(shí)現(xiàn)全文檢索的示例代碼
  • golang elasticsearch Client的使用詳解
  • Django利用elasticsearch(搜索引擎)實(shí)現(xiàn)搜索功能
  • Django項(xiàng)目之Elasticsearch搜索引擎的實(shí)例
  • django使用haystack調(diào)用Elasticsearch實(shí)現(xiàn)索引搜索
  • Go語(yǔ)言Elasticsearch數(shù)據(jù)清理工具思路詳解

標(biāo)簽:調(diào)研邀請(qǐng) 阿克蘇 廣西 慶陽(yáng) 太原 德州 西雙版納 貴陽(yáng)

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《golang操作elasticsearch的實(shí)現(xiàn)》,本文關(guān)鍵詞  golang,操作,elasticsearch,的,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《golang操作elasticsearch的實(shí)現(xiàn)》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于golang操作elasticsearch的實(shí)現(xiàn)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 吴忠市| 承德县| 托克托县| 林周县| 惠来县| 方正县| 芜湖县| 绥宁县| 富裕县| 邹城市| 台南县| 淄博市| 大连市| 遵义市| 宜州市| 重庆市| 定南县| 和龙市| 谢通门县| 柯坪县| 京山县| 广南县| 昆山市| 拉孜县| 浏阳市| 孙吴县| 天水市| 崇阳县| 遵义县| 淄博市| 慈利县| 巢湖市| 瓦房店市| 宿迁市| 西华县| 汾西县| 静宁县| 棋牌| 新蔡县| 沾益县| 道真|