- 安装
// elasticsearch sdk
go get -u github.com/elastic/go-elasticsearch/v7
//操作json
go get "github.com/tidwall/gjson"
- demo
package esexample
import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"strings"
"time"
"github.com/elastic/go-elasticsearch/v7"
"github.com/elastic/go-elasticsearch/v7/esapi"
"github.com/tidwall/gjson"
)
var es_client *elasticsearch.Client
func InitEs() {
cfg := elasticsearch.Config{
Addresses: []string{
"http://192.168.214.133:31200",
},
Username: "elastic",
Password: "ellischen",
}
es_client, _ = elasticsearch.NewClient(cfg)
}
func ListIndex() {
res, err := esapi.CatIndicesRequest{Format: "json"}.Do(context.Background(), es_client)
if err != nil {
return
}
defer res.Body.Close()
fmt.Println(res.String())
}
func CalculateIndexDocCount() float64 {
res, err := esapi.CountRequest{Index: []string{"ellis"}}.Do(context.Background(), es_client)
if err != nil {
return 0
}
defer res.Body.Close()
var resMap map[string]interface{}
json.NewDecoder(res.Body).Decode(&resMap)
fmt.Printf("resMap: %v\n", resMap["count"])
return resMap["count"].(float64)
}
func IndexOneDocument() {
document := Ellis{Name: "haha"}
data, _ := json.Marshal(document)
r, err2 := es_client.Index("ellis", strings.NewReader(string(data)), es_client.Index.WithDocumentID("4"))
if err2 != nil {
fmt.Printf("err2: %v\n", err2)
} else {
defer r.Body.Close()
var value map[string]interface{}
json.NewDecoder(r.Body).Decode(&value)
vv, _ := json.Marshal(value)
fmt.Printf("string(vv): %v\n", string(vv))
}
}
func GetByID(id string) (value any) {
r, err := es_client.Get("ellis", id, es_client.Get.WithRefresh(true))
if err != nil {
return nil
} else {
defer r.Body.Close()
var value map[string]interface{}
json.NewDecoder(r.Body).Decode(&value)
fmt.Printf("value: %v\n", value)
vv, _ := json.Marshal(value)
fmt.Printf("string(vv): %v\n", string(vv))
return value
}
}
func SearchByDSL() {
var buf bytes.Buffer
query := map[string]interface{}{
"query": map[string]interface{}{
"term": map[string]interface{}{
"name": "haha",
},
},
}
if err := json.NewEncoder(&buf).Encode(query); err != nil {
log.Fatalf("Error encoding query: %s", err)
}
res, err := es_client.Search(
es_client.Search.WithContext(context.Background()),
es_client.Search.WithIndex("ellis"),
es_client.Search.WithBody(&buf),
es_client.Search.WithTrackTotalHits(true),
es_client.Search.WithPretty(),
)
if err != nil {
log.Fatalf("Error getting response: %s", err)
}
defer res.Body.Close()
var r map[string]interface{}
if err := json.NewDecoder(res.Body).Decode(&r); err != nil {
log.Fatalf("Error parsing the response body: %s", err)
}
log.Printf(
"[%s] %d hits; took: %dms",
res.Status(),
int(r["hits"].(map[string]interface{})["total"].(map[string]interface{})["value"].(float64)),
int(r["took"].(float64)),
)
for _, hit := range r["hits"].(map[string]interface{})["hits"].([]interface{}) {
log.Printf(" * ID=%s, %s", hit.(map[string]interface{})["_id"], hit.(map[string]interface{})["_source"])
}
log.Println(strings.Repeat("=", 37))
}
func UpdateByQuery() {
var buf bytes.Buffer
query := map[string]interface{}{
"query": map[string]interface{}{
"term": map[string]interface{}{
"name": "haha",
},
},
"script": map[string]interface{}{
"source": "ctx._source[\"name\"]=params.name",
"params": map[string]string{
"name": "ellis",
},
"lang": "painless",
},
}
if err := json.NewEncoder(&buf).Encode(query); err != nil {
log.Fatalf("Error encoding query: %s", err)
}
es_client.UpdateByQuery([]string{"ellis"}, es_client.UpdateByQuery.WithBody(&buf))
}
func Delete() {
r, err := es_client.Delete("ellis", "1", es_client.Delete.WithRefresh("true"))
if err != nil {
fmt.Printf("err: %v\n", err)
} else {
defer r.Body.Close()
var value map[string]interface{}
json.NewDecoder(r.Body).Decode(&value)
vv, _ := json.Marshal(value)
fmt.Printf("string(vv): %v\n", string(vv))
}
}
func SearchAfter() {
query := `{
"query": {
"match_all": {}
},
"sort": [
{
"_id": {
"order": "desc"
}
}
],
"size": 1,
"search_after":["3"]
}`
res, err := es_client.Search(
es_client.Search.WithIndex("ellis"),
es_client.Search.WithBody(strings.NewReader(query)),
)
if err != nil {
log.Fatalf("Error getting response: %s", err)
}
defer res.Body.Close()
var r map[string]interface{}
if err := json.NewDecoder(res.Body).Decode(&r); err != nil {
log.Fatalf("Error parsing the response body: %s", err)
}
log.Printf(
"[%s] %d hits; took: %dms",
res.Status(),
int(r["hits"].(map[string]interface{})["total"].(map[string]interface{})["value"].(float64)),
int(r["took"].(float64)),
)
for _, hit := range r["hits"].(map[string]interface{})["hits"].([]interface{}) {
log.Printf(" * ID=%s, %s", hit.(map[string]interface{})["_id"], hit.(map[string]interface{})["_source"])
}
log.Println(strings.Repeat("=", 37))
}
func Scroll() {
query := `{
"query": {
"match_all": {}
}
}`
log.Println("Scrolling the index...")
log.Println(strings.Repeat("-", 80))
res, err := es_client.Search(
es_client.Search.WithBody(strings.NewReader(query)),
es_client.Search.WithIndex("ellis"),
es_client.Search.WithSize(1),
es_client.Search.WithScroll(time.Minute),
)
if err != nil {
fmt.Printf("err: %v\n", err)
}
var mapvalue map[string]interface{}
json.NewDecoder(res.Body).Decode(&mapvalue)
value1, _ := json.Marshal(mapvalue)
jsonvalue := string(value1)
defer res.Body.Close()
scrollID := gjson.Get(jsonvalue, "_scroll_id").String()
log.Println("ScrollID", scrollID)
log.Println("IDs ", gjson.Get(jsonvalue, "hits.hits.#._id"))
log.Println(strings.Repeat("-", 80))
for _, hit := range mapvalue["hits"].(map[string]interface{})["hits"].([]interface{}) {
log.Printf(" * ID=%s, %s", hit.(map[string]interface{})["_id"], hit.(map[string]interface{})["_source"])
}
for {
res, err := es_client.Scroll(es_client.Scroll.WithScrollID(scrollID), es_client.Scroll.WithScroll(time.Minute))
if err != nil {
log.Fatalf("Error: %s", err)
}
if res.IsError() {
log.Fatalf("Error response: %s", res)
}
defer res.Body.Close()
var mapvalue map[string]interface{}
json.NewDecoder(res.Body).Decode(&mapvalue)
value1, _ := json.Marshal(mapvalue)
jsonvalue := string(value1)
scrollID = gjson.Get(jsonvalue, "_scroll_id").String()
hits := gjson.Get(jsonvalue, "hits.hits")
if len(hits.Array()) < 1 {
log.Println("Finished scrolling")
break
} else {
log.Println("ScrollID", scrollID)
log.Println("IDs ", gjson.Get(hits.Raw, "#._id"))
log.Println(strings.Repeat("-", 80))
for _, v := range hits.Array() {
fmt.Printf("v.Raw: %v\n", v.Raw)
}
}
}
}