go語言操作Elasticsearch

2021-10-05 09:28:45 字數 4242 閱讀 4907

package main

import

("context"

"encoding/json"

"fmt"

"gopkg.in/olivere/elastic.v5"

//這裡使用的是版本5,最新的是6,有改動

"log"

"os"

"reflect"

)var client *elastic.client

var host =

""type employee struct

//初始化

func

init()

info, code, err := client.

ping

(host).do

(context.

background()

)if err !=

nil fmt.

printf

("elasticsearch returned with code %d and version %s\n"

, code, info.version.number)

esversion, err := client.

elasticsearchversion

(host)

if err !=

nil fmt.

printf

("elasticsearch version %s\n"

, esversion)

}/*下面是簡單的curd*/

//建立

func

create()

} put1, err := client.

index()

.index

("megacorp").

type

("employee").

id("1")

.bodyjson

(e1).do

(context.

background()

)if err !=

nil fmt.

printf

("indexed tweet %s to index s%s, type %s\n"

, put1.id, put1.index, put1.type)

//使用字串

e2 :=

`` put2, err := client.

index()

.index

("megacorp").

type

("employee").

id("2")

.bodyjson

(e2).do

(context.

background()

)if err !=

nil fmt.

printf

("indexed tweet %s to index s%s, type %s\n"

, put2.id, put2.index, put2.type)

e3 :=

`` put3, err := client.

index()

.index

("megacorp").

type

("employee").

id("3")

.bodyjson

(e3).do

(context.

background()

)if err !=

nil fmt.

printf

("indexed tweet %s to index s%s, type %s\n"

, put3.id, put3.index, put3.type)

}//刪除

func

delete()

fmt.

printf

("delete result %s\n"

, res.result)

}//修改

func

update()

).do(context.

background()

)if err !=

nil fmt.

printf

("update age %s\n"

, res.result)

}//查詢

func

gets()

if get1.found

}//搜尋

func

query()

printemployee

(res, err)

if res.hits.totalhits >0

fmt.

printf

("employee name %s : %s\n"

, t.firstname, t.lastname)}}

else

//條件查詢

//年齡大於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)

//短語搜尋 搜尋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)

}//簡單分頁

func

list

(size,page int

) 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

)var typ employee

for_, item :=

range res.

each

(reflect.

typeof

(typ))}

func

main()

Go語言基礎(十六) Go語言檔案操作

package main import fmt os bufio io ioutil 錯誤處理方法 func handle why string,e error func main handle 檔案讀取失敗!err fmt.println str fmt.println 檔案讀取完畢!讀檔案方式二...

Go語言操作MySQL

go語言安裝mysql資料庫驅動步驟 安裝驅動 go get github.com go sql driver mysql檢測安裝是否成功 sudo find name go sql driver 進入mysql目錄。ls l wc l一共32個檔案 匯入mysql資料庫驅動import 測試連線資...

go語言操作kafka

go語言中連線kafka使用第三方庫 github.com shopify sarama。go get github.com shopify saramasarama v1.20之後的版本加入了zstd壓縮演算法,需要用到cgo,在windows平台編譯時會提示類似如下錯誤 所以在windows平台...