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

Golang操作ES進行交互的實現(xiàn)實例

 更新時間:2025年11月21日 09:45:31   作者:PPPsych  
本文詳細(xì)介紹了如何使用Golang與Elasticsearch進行交互,包括創(chuàng)建項目、安裝包、連接ES、操作索引、插入、查詢和刪除文檔,具有一定的參考價值,感興趣的可以了解一下

創(chuàng)建項目

創(chuàng)建一個項目demo,初始化項目go mod init demo

安裝包

go get github.com/elastic/go-elasticsearch/v7

連接到ES

package main

import (
	"log"

	"github.com/elastic/go-elasticsearch/v7"
)

func main() {
	es, err := elasticsearch.NewDefaultClient()// 連接到es服務(wù)器
	if err != nil {
		panic(err)
	}
	log.Println(elasticsearch.Version)
	log.Println(es.Info())
}

運行結(jié)果

[Running] go run "e:\golang開發(fā)學(xué)習(xí)\es\main.go"
2022/09/24 13:16:22 7.17.1
2022/09/24 13:16:22 [200 OK] {
  "name" : "MORAX",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "HoU0Tw3WTiSMD2GRzezqnw",
  "version" : {
    "number" : "7.8.1",
    "build_flavor" : "default",
    "build_type" : "zip",
    "build_hash" : "b5ca9c58fb664ca8bf9e4057fc229b3396bf3a89",
    "build_date" : "2020-07-21T16:40:44.668009Z",
    "build_snapshot" : false,
    "lucene_version" : "8.5.1",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}
 <nil>

[Done] exited with code=0 in 2.516 seconds

Golang操作ES索引

es API

我們獲得es客戶端實例后,通過客戶端實例可以獲得es api,內(nèi)容如下:

package esapi

// API contains the Elasticsearch APIs
//
type API struct {
	Cat         *Cat
	Cluster     *Cluster
	Indices     *Indices
	Ingest      *Ingest
	Nodes       *Nodes
	Remote      *Remote
	Snapshot    *Snapshot
	Tasks       *Tasks
	AsyncSearch *AsyncSearch
	CCR         *CCR
	ILM         *ILM
	License     *License
	Migration   *Migration
	ML          *ML
	Monitoring  *Monitoring
	Rollup      *Rollup
	Security    *Security
	SQL         *SQL
	SSL         *SSL
	Watcher     *Watcher
	XPack       *XPack

	AutoscalingDeleteAutoscalingPolicy            AutoscalingDeleteAutoscalingPolicy
	AutoscalingGetAutoscalingCapacity             AutoscalingGetAutoscalingCapacity
	AutoscalingGetAutoscalingDecision             AutoscalingGetAutoscalingDecision
	AutoscalingGetAutoscalingPolicy               AutoscalingGetAutoscalingPolicy
	AutoscalingPutAutoscalingPolicy               AutoscalingPutAutoscalingPolicy
	Bulk                                          Bulk
	ClearScroll                                   ClearScroll
	ClosePointInTime                              ClosePointInTime
	Count                                         Count
	Create                                        Create
	DanglingIndicesDeleteDanglingIndex            DanglingIndicesDeleteDanglingIndex
	DanglingIndicesImportDanglingIndex            DanglingIndicesImportDanglingIndex
	DanglingIndicesListDanglingIndices            DanglingIndicesListDanglingIndices
	DataFrameTransformDeprecatedDeleteTransform   DataFrameTransformDeprecatedDeleteTransform
	DataFrameTransformDeprecatedGetTransform      DataFrameTransformDeprecatedGetTransform
	DataFrameTransformDeprecatedGetTransformStats DataFrameTransformDeprecatedGetTransformStats
	DataFrameTransformDeprecatedPreviewTransform  DataFrameTransformDeprecatedPreviewTransform
	DataFrameTransformDeprecatedPutTransform      DataFrameTransformDeprecatedPutTransform
	DataFrameTransformDeprecatedStartTransform    DataFrameTransformDeprecatedStartTransform
	DataFrameTransformDeprecatedStopTransform     DataFrameTransformDeprecatedStopTransform
	DataFrameTransformDeprecatedUpdateTransform   DataFrameTransformDeprecatedUpdateTransform
	DeleteByQuery                                 DeleteByQuery
	DeleteByQueryRethrottle                       DeleteByQueryRethrottle
	Delete                                        Delete
	DeleteScript                                  DeleteScript
	EnrichDeletePolicy                            EnrichDeletePolicy
	EnrichExecutePolicy                           EnrichExecutePolicy
	EnrichGetPolicy                               EnrichGetPolicy
	EnrichPutPolicy                               EnrichPutPolicy
	EnrichStats                                   EnrichStats
	EqlDelete                                     EqlDelete
	EqlGet                                        EqlGet
	EqlGetStatus                                  EqlGetStatus
	EqlSearch                                     EqlSearch
	Exists                                        Exists
	ExistsSource                                  ExistsSource
	Explain                                       Explain
	FeaturesGetFeatures                           FeaturesGetFeatures
	FeaturesResetFeatures                         FeaturesResetFeatures
	FieldCaps                                     FieldCaps
	FleetGlobalCheckpoints                        FleetGlobalCheckpoints
	FleetMsearch                                  FleetMsearch
	FleetSearch                                   FleetSearch
	Get                                           Get
	GetScriptContext                              GetScriptContext
	GetScriptLanguages                            GetScriptLanguages
	GetScript                                     GetScript
	GetSource                                     GetSource
	GraphExplore                                  GraphExplore
	Index                                         Index
	Info                                          Info
	LogstashDeletePipeline                        LogstashDeletePipeline
	LogstashGetPipeline                           LogstashGetPipeline
	LogstashPutPipeline                           LogstashPutPipeline
	Mget                                          Mget
	Msearch                                       Msearch
	MsearchTemplate                               MsearchTemplate
	Mtermvectors                                  Mtermvectors
	OpenPointInTime                               OpenPointInTime
	Ping                                          Ping
	PutScript                                     PutScript
	RankEval                                      RankEval
	Reindex                                       Reindex
	ReindexRethrottle                             ReindexRethrottle
	RenderSearchTemplate                          RenderSearchTemplate
	ScriptsPainlessExecute                        ScriptsPainlessExecute
	Scroll                                        Scroll
	SearchMvt                                     SearchMvt
	Search                                        Search
	SearchShards                                  SearchShards
	SearchTemplate                                SearchTemplate
	SearchableSnapshotsCacheStats                 SearchableSnapshotsCacheStats
	SearchableSnapshotsClearCache                 SearchableSnapshotsClearCache
	SearchableSnapshotsMount                      SearchableSnapshotsMount
	SearchableSnapshotsRepositoryStats            SearchableSnapshotsRepositoryStats
	SearchableSnapshotsStats                      SearchableSnapshotsStats
	ShutdownDeleteNode                            ShutdownDeleteNode
	ShutdownGetNode                               ShutdownGetNode
	ShutdownPutNode                               ShutdownPutNode
	SlmDeleteLifecycle                            SlmDeleteLifecycle
	SlmExecuteLifecycle                           SlmExecuteLifecycle
	SlmExecuteRetention                           SlmExecuteRetention
	SlmGetLifecycle                               SlmGetLifecycle
	SlmGetStats                                   SlmGetStats
	SlmGetStatus                                  SlmGetStatus
	SlmPutLifecycle                               SlmPutLifecycle
	SlmStart                                      SlmStart
	SlmStop                                       SlmStop
	TermsEnum                                     TermsEnum
	Termvectors                                   Termvectors
	TextStructureFindStructure                    TextStructureFindStructure
	TransformDeleteTransform                      TransformDeleteTransform
	TransformGetTransform                         TransformGetTransform
	TransformGetTransformStats                    TransformGetTransformStats
	TransformPreviewTransform                     TransformPreviewTransform
	TransformPutTransform                         TransformPutTransform
	TransformStartTransform                       TransformStartTransform
	TransformStopTransform                        TransformStopTransform
	TransformUpdateTransform                      TransformUpdateTransform
	TransformUpgradeTransforms                    TransformUpgradeTransforms
	UpdateByQuery                                 UpdateByQuery
	UpdateByQueryRethrottle                       UpdateByQueryRethrottle
	Update                                        Update
}

API結(jié)構(gòu)體里面包含了Indices,內(nèi)容如下:

// Indices contains the Indices APIs
type Indices struct {
	AddBlock              IndicesAddBlock
	Analyze               IndicesAnalyze
	ClearCache            IndicesClearCache
	Clone                 IndicesClone
	Close                 IndicesClose
	CreateDataStream      IndicesCreateDataStream
	Create                IndicesCreate
	DataStreamsStats      IndicesDataStreamsStats
	DeleteAlias           IndicesDeleteAlias
	DeleteDataStream      IndicesDeleteDataStream
	DeleteIndexTemplate   IndicesDeleteIndexTemplate
	Delete                IndicesDelete
	DeleteTemplate        IndicesDeleteTemplate
	DiskUsage             IndicesDiskUsage
	ExistsAlias           IndicesExistsAlias
	ExistsDocumentType    IndicesExistsDocumentType
	ExistsIndexTemplate   IndicesExistsIndexTemplate
	Exists                IndicesExists
	ExistsTemplate        IndicesExistsTemplate
	FieldUsageStats       IndicesFieldUsageStats
	Flush                 IndicesFlush
	FlushSynced           IndicesFlushSynced
	Forcemerge            IndicesForcemerge
	Freeze                IndicesFreeze
	GetAlias              IndicesGetAlias
	GetDataStream         IndicesGetDataStream
	GetFieldMapping       IndicesGetFieldMapping
	GetIndexTemplate      IndicesGetIndexTemplate
	GetMapping            IndicesGetMapping
	Get                   IndicesGet
	GetSettings           IndicesGetSettings
	GetTemplate           IndicesGetTemplate
	GetUpgrade            IndicesGetUpgrade
	MigrateToDataStream   IndicesMigrateToDataStream
	ModifyDataStream      IndicesModifyDataStream
	Open                  IndicesOpen
	PromoteDataStream     IndicesPromoteDataStream
	PutAlias              IndicesPutAlias
	PutIndexTemplate      IndicesPutIndexTemplate
	PutMapping            IndicesPutMapping
	PutSettings           IndicesPutSettings
	PutTemplate           IndicesPutTemplate
	Recovery              IndicesRecovery
	Refresh               IndicesRefresh
	ReloadSearchAnalyzers IndicesReloadSearchAnalyzers
	ResolveIndex          IndicesResolveIndex
	Rollover              IndicesRollover
	Segments              IndicesSegments
	ShardStores           IndicesShardStores
	Shrink                IndicesShrink
	SimulateIndexTemplate IndicesSimulateIndexTemplate
	SimulateTemplate      IndicesSimulateTemplate
	Split                 IndicesSplit
	Stats                 IndicesStats
	Unfreeze              IndicesUnfreeze
	UpdateAliases         IndicesUpdateAliases
	Upgrade               IndicesUpgrade
	ValidateQuery         IndicesValidateQuery
}

該結(jié)構(gòu)體包含了操作索引的一些方法,例如:創(chuàng)建索引Create、查詢索引Get、刪除索引Delete

創(chuàng)建索引

創(chuàng)建索引函數(shù)如下:

type IndicesCreate func(index string, o ...func(*IndicesCreateRequest)) (*Response, error)

只要傳入一個創(chuàng)建索引的名稱即可,也可以通過IndicesCreateRequest傳遞一些創(chuàng)建索引的額外信息

實例演示:

package main

import (
	"fmt"
	"log"

	"github.com/elastic/go-elasticsearch/v7"
)

// 創(chuàng)建索引
func CreatIndex(client *elasticsearch.Client, name string) {
	r, _ := client.API.Indices.Create(name)
	fmt.Printf("r: %v\n", r)
}

func main() {
	es, err := elasticsearch.NewDefaultClient()
	if err != nil {
		log.Fatalf("獲取es客戶端錯誤: %s", err)
	}
	CreatIndex(es, "es_test")
}

運行結(jié)果:

[Running] go run "e:\golang開發(fā)學(xué)習(xí)\es\main.go"
r: [200 OK] {"acknowledged":true,"shards_acknowledged":true,"index":"es_test"}

[Done] exited with code=0 in 3.902 seconds

查詢索引

查詢索引函數(shù)如下:

type IndicesGet func(index []string, o ...func(*IndicesGetRequest)) (*Response, error)

傳入需要查詢的字符串切片即可

實例演示:

package main

import (
	"fmt"
	"log"

	"github.com/elastic/go-elasticsearch/v7"
)

// 查詢索引
func GetIndex(client *elasticsearch.Client, name string) {
	r, _ := client.API.Indices.Get([]string{name})
	fmt.Printf("r: %v\n", r)
}

func main() {
	es, err := elasticsearch.NewDefaultClient()
	if err != nil {
		log.Fatalf("獲取es客戶端錯誤: %s", err)
	}
	GetIndex(es, "es_test")
}

運行結(jié)果:

[Running] go run "e:\golang開發(fā)學(xué)習(xí)\es\main.go"
r: [200 OK] {"es_test":{"aliases":{},"mappings":{},"settings":{"index":{"creation_date":"1663999135597","number_of_shards":"1","number_of_replicas":"1","uuid":"Mp9txec1R1Co4AONcoofGw","version":{"created":"7080199"},"provided_name":"es_test"}}}}

[Done] exited with code=0 in 1.751 seconds

刪除索引

刪除索引函數(shù)如下:

type IndicesDelete func(index []string, o ...func(*IndicesDeleteRequest)) (*Response, error)

傳入需要刪除的索引的字符串切片即可

實例演示:

package main

import (
	"fmt"
	"log"

	"github.com/elastic/go-elasticsearch/v7"
)

// 刪除索引
func DelIndex(client *elasticsearch.Client, name string) {
	r, _ := client.API.Indices.Delete([]string{name})
	fmt.Printf("r: %v\n", r)
}

func main() {
	es, err := elasticsearch.NewDefaultClient()
	if err != nil {
		log.Fatalf("獲取es客戶端錯誤: %s", err)
	}

	DelIndex(es, "es_test")
}

運行結(jié)果:

[Running] go run "e:\golang開發(fā)學(xué)習(xí)\es\main.go"
r: [200 OK] {"acknowledged":true}

[Done] exited with code=0 in 1.95 seconds

Golang操作ES文檔

插入文檔

在這個章節(jié)中,我將一步一步地指導(dǎo)如何如何使用 go-elasticsearch 驅(qū)動來把文檔導(dǎo)入到 Elasticsearch 中。

步驟一:導(dǎo)包

package main
 
import (
"context"
"encoding/json"
"fmt"
"log"
"reflect"
"strconv"
"strings"
 
// Import the Elasticsearch library packages
"github.com/elastic/go-elasticsearch/v7"
"github.com/elastic/go-elasticsearch/v7/esapi"
)

在上面,我們使用 v7 版本,它對應(yīng)于 Elastic Stack 7.x 版本的發(fā)布。

步驟二:為文檔字段創(chuàng)捷結(jié)構(gòu)體

使用 Golang struct 數(shù)據(jù)類型為要編制索引的 Elasticsearch 文檔以及索引的相應(yīng)字段創(chuàng)建框架:

// Declare a struct for Elasticsearch fields
type ElasticDocs struct {
  SomeStr string
  SomeInt int
  SomeBool bool
}

步驟三:將結(jié)構(gòu)體轉(zhuǎn)換為JSON字符串

接下來,讓我們看一個簡單的函數(shù),它將 Elasticsearch struct 文檔實例轉(zhuǎn)換為 JSON 字符串。 下面顯示的代碼可能看起來有些復(fù)雜,但是實際上發(fā)生的事情很簡單–所有功能所做的就是將結(jié)構(gòu)轉(zhuǎn)換為字符串文字,然后將該字符串傳遞給 Golang 的 json.Marshal() 方法以使其返回字符串的JSON編碼:

// structs to JSON 
func jsonStruct(doc ElasticDocs) string {
	// Create struct instance of the Elasticsearch fields struct object
	docStruct := &ElasticDocs{
		SomeStr: doc.SomeStr,
		SomeInt: doc.SomeInt,
		SomeBool: doc.SomeBool,
	}
	
	fmt.Println("\ndocStruct:", docStruct)
	fmt.Println("docStruct TYPE:", reflect.TypeOf(docStruct))
	
	// Marshal the struct to JSON and check for errors
	b, err := json.Marshal(docStruct)
	if err != nil {
		fmt.Println("json.Marshal ERROR:", err)
		return string(err.Error())
	}
	
	return string(b)
}

步驟四:創(chuàng)建 Elasticsearch Golang 客戶端實例

為 API 調(diào)用創(chuàng)建一個新的上下文對象,并為 Elasticsearch 文檔創(chuàng)建一個 map 對象:

func main() {
 
	// 允許日志輸出的自定義格式
	log.SetFlags(0)
	
	// 為API調(diào)用創(chuàng)建上下文對象
	ctx := context.Background()
	
	// 為Elasticsearch文檔創(chuàng)建映射
	var (
		docMap map[string]interface{}
	)
 
	fmt.Println("docMap:", docMap)
	fmt.Println("docMap TYPE:", reflect.TypeOf(docMap))

步驟五:實例化 Elasticsearch 客戶端配置和 Golang 客戶端實例

實例化一個新的 Elasticsearch 配置對象。 確保將正確的主機和端口信息以及任何用戶名或密碼傳遞給其 “Adressess” 屬性。

	// 聲明Elasticsearch配置
	cfg := elasticsearch.Config{
		Addresses: []string{
		"http://localhost:9200",
		},
		Username: "Psych",
		Password: "666",
	}
	
	// 實例化新的Elasticsearch客戶端對象實例
	client, err := elasticsearch.NewClient(cfg)
	
	if err != nil {
		fmt.Println("Elasticsearch connection error:", err)
	}

步驟六:檢查與 Elasticsearch 的連接

	// 讓客戶端實例返回響應(yīng)
	res, err := client.Info()
 
	// 將響應(yīng)反序列化為映射。
	if err != nil {
		log.Fatalf("client.Info() ERROR:", err)
	} else {
		log.Printf("client response:", res)
	}

步驟七:創(chuàng)建 Elasticsearch 結(jié)構(gòu)文檔并將其放入數(shù)組

聲明一個空字符串?dāng)?shù)組,以存儲當(dāng)前以 JSON 字符串表示的 Elasticsearch 文檔。 以下代碼顯示了一些將用于索引的 Elasticsearch 文檔示例。 要設(shè)置其字段的值,你需要做的就是修改結(jié)構(gòu)實例的屬性:

	// 為文檔字符串聲明空數(shù)組
	var docs []string
 
	// 使用結(jié)構(gòu)聲明要索引的文檔
	doc1 := ElasticDocs{}
	doc1.SomeStr = "Some Value"
	doc1.SomeInt = 123456
	doc1.SomeBool = true
 
	doc2 := ElasticDocs{}
	doc2.SomeStr = "Another Value"
	doc2.SomeInt = 42
	doc2.SomeBool = false	

將這些文檔實例傳遞給我們先前聲明的 jsonStruct() 函數(shù),并使它們返回代表每個文檔的 JSON 字符串。 然后,我們將使用 Golang 的 append() 函數(shù)將 JSON 字符串添加到字符串?dāng)?shù)組中:

	// document struct objects to JSON string
	docStr1 := jsonStruct(doc1)
	docStr2 := jsonStruct(doc2)
 
	// Append the doc strings to an array
	docs = append(docs, docStr1)
	docs = append(docs, docStr2)

步驟八:迭代文檔數(shù)組并調(diào)用 Golang 客戶端的IndexRequest()方法

對已經(jīng)建立好的文檔數(shù)組進行迭代,并在進行過程中向 Elasticsearch 集群發(fā)出 API 請求。 這些API調(diào)用將通過調(diào)用 Golang 驅(qū)動程序的 esapi.IndexRequest() 方法來索引文檔:

	// 迭代文檔數(shù)組
	for i, bod := range docs {
		fmt.Println("\nDOC _id:", i+1)
		fmt.Println(bod)
		
		// 實例化請求對象
		req := esapi.IndexRequest {
			Index: "some_index",
			DocumentID: strconv.Itoa(i + 1),
			Body: strings.NewReader(bod),
			Refresh: "true",
		}
 
		fmt.Println(reflect.TypeOf(req))

在上面一定要注意的是:我們設(shè)置 Refresh 為 true。這在實際的使用中并不建議,原因是每次寫入的時候都會 refresh。當(dāng)我們面對大量的數(shù)據(jù)時,這樣的操作會造成效率的底下。

步驟九:檢查 IndexRequest() 方法調(diào)用是否返回任何錯誤

在文檔數(shù)組上進行迭代的最后一步是從 API 調(diào)用中獲取響應(yīng),并檢查是否存在錯誤:

		// 從請求返回API響應(yīng)對象
		res, err := req.Do(ctx, client)
		if err != nil {
			log.Fatalf("IndexRequest ERROR: %s", err)
		}
 
		defer res.Body.Close()

在下面顯示的代碼中,如果沒有錯誤返回,我們將解析 API 響應(yīng)返回的結(jié)果對象:

		if res.IsError() {
			log.Printf("%s ERROR indexing document ID=%d", res.Status(), i+1)
		} else {
			
			// 將響應(yīng)反序列化為映射。
			var resMap map[string]interface{}
			if err := json.NewDecoder(res.Body).Decode(&resMap); err != nil {
				log.Printf("Error parsing the response body: %s", err)
			} else {
				log.Printf("\nIndexRequest() RESPONSE:")
				// 打印響應(yīng)狀態(tài)和索引文檔版本。
				fmt.Println("Status:", res.Status())
				fmt.Println("Result:", resMap["result"])
				fmt.Println("Version:", int(resMap["_version"].(float64)))
				fmt.Println("resMap:", resMap)
				fmt.Println("\n")
			}
		}
	}
}

每個文檔迭代都應(yīng)打印出一個map[string] interface{} 對象響應(yīng),如下所示:

resMap: map[_id:2 _index:some_index _primary_term:1 _seq_no:1 _shards:map[failed:0 successful:1 total:2] _type:_doc _version:1 forced_refresh:true result:created]

代碼總結(jié)

package main
 
import (
"context"
"encoding/json"
"fmt"
"log"
"reflect"
"strconv"
"strings"
 
// Import the Elasticsearch library packages
"github.com/elastic/go-elasticsearch/v7"
"github.com/elastic/go-elasticsearch/v7/esapi"
)
 
// Declare a struct for Elasticsearch fields
type ElasticDocs struct {
	SomeStr string
	SomeInt int
	SomeBool bool
}
 
// A function for marshaling structs to JSON string
func jsonStruct(doc ElasticDocs) string {
	// Create struct instance of the Elasticsearch fields struct object
	docStruct := &ElasticDocs{
		SomeStr: doc.SomeStr,
		SomeInt: doc.SomeInt,
		SomeBool: doc.SomeBool,
	}
	
	fmt.Println("\ndocStruct:", docStruct)
	fmt.Println("docStruct TYPE:", reflect.TypeOf(docStruct))
	
	// Marshal the struct to JSON and check for errors
	b, err := json.Marshal(docStruct)
	if err != nil {
		fmt.Println("json.Marshal ERROR:", err)
		return string(err.Error())
	}
 
	return string(b)
}
 
func main() {
 
	// Allow for custom formatting of log output
	log.SetFlags(0)
	
	// Create a context object for the API calls
	ctx := context.Background()
	
	// Create a mapping for the Elasticsearch documents
	var (
		docMap map[string]interface{}
	)
 
	fmt.Println("docMap:", docMap)
	fmt.Println("docMap TYPE:", reflect.TypeOf(docMap))
 
	// Declare an Elasticsearch configuration
	cfg := elasticsearch.Config{
		Addresses: []string{
		"http://localhost:9200",
		},
		Username: "user",
		Password: "pass",
	}
	
	// Instantiate a new Elasticsearch client object instance
	client, err := elasticsearch.NewClient(cfg)
	
	if err != nil {
		fmt.Println("Elasticsearch connection error:", err)
	}
 
	// Have the client instance return a response
	res, err := client.Info()
 
	// Deserialize the response into a map.
	if err != nil {
		log.Fatalf("client.Info() ERROR:", err)
	} else {
		log.Printf("client response:", res)
	}
 
	// Declare empty array for the document strings
	var docs []string
 
	// Declare documents to be indexed using struct
	doc1 := ElasticDocs{}
	doc1.SomeStr = "Some Value"
	doc1.SomeInt = 123456
	doc1.SomeBool = true
 
	doc2 := ElasticDocs{}
	doc2.SomeStr = "Another Value"
	doc2.SomeInt = 42
	doc2.SomeBool = false	
 
	// Marshal Elasticsearch document struct objects to JSON string
	docStr1 := jsonStruct(doc1)
	docStr2 := jsonStruct(doc2)
 
	// Append the doc strings to an array
	docs = append(docs, docStr1)
	docs = append(docs, docStr2)
 
	// Iterate the array of string documents
	for i, bod := range docs {
		fmt.Println("\nDOC _id:", i+1)
		fmt.Println(bod)
		
		// Instantiate a request object
		req := esapi.IndexRequest {
			Index: "some_index",
			DocumentID: strconv.Itoa(i + 1),
			Body: strings.NewReader(bod),
			Refresh: "true",
		}
 
		fmt.Println(reflect.TypeOf(req))
 
		// Return an API response object from request
		res, err := req.Do(ctx, client)
		if err != nil {
			log.Fatalf("IndexRequest ERROR: %s", err)
		}
 
		defer res.Body.Close()
 
		if res.IsError() {
			log.Printf("%s ERROR indexing document ID=%d", res.Status(), i+1)
		} else {
			
			// Deserialize the response into a map.
			var resMap map[string]interface{}
			if err := json.NewDecoder(res.Body).Decode(&resMap); err != nil {
				log.Printf("Error parsing the response body: %s", err)
			} else {
				log.Printf("\nIndexRequest() RESPONSE:")
				// Print the response status and indexed document version.
				fmt.Println("Status:", res.Status())
				fmt.Println("Result:", resMap["result"])
				fmt.Println("Version:", int(resMap["_version"].(float64)))
				fmt.Println("resMap:", resMap)
				fmt.Println("\n")
			}
		}
	}
}

運行結(jié)果

[Running] go run "e:\golang開發(fā)學(xué)習(xí)\es\document\main.go"
docMap: map[]
docMap TYPE: map[string]interface {}
client response:%!(EXTRA *esapi.Response=[200 OK] {
  "name" : "MORAX",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "HoU0Tw3WTiSMD2GRzezqnw",
  "version" : {
    "number" : "7.8.1",
    "build_flavor" : "default",
    "build_type" : "zip",
    "build_hash" : "b5ca9c58fb664ca8bf9e4057fc229b3396bf3a89",
    "build_date" : "2020-07-21T16:40:44.668009Z",
    "build_snapshot" : false,
    "lucene_version" : "8.5.1",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}
)

docStruct: &{Some Value 123456 true}
docStruct TYPE: *main.ElasticDocs

docStruct: &{Another Value 42 false}
docStruct TYPE: *main.ElasticDocs

DOC _id: 1
{"SomeStr":"Some Value","SomeInt":123456,"SomeBool":true}
esapi.IndexRequest

IndexRequest() RESPONSE:
Status: 201 Created
Result: created
Version: 1
resMap: map[_id:1 _index:some_index _primary_term:1 _seq_no:0 _shards:map[failed:0 successful:1 total:2] _type:_doc _version:1 forced_refresh:true result:created]



DOC _id: 2
{"SomeStr":"Another Value","SomeInt":42,"SomeBool":false}
esapi.IndexRequest

IndexRequest() RESPONSE:
Status: 201 Created
Result: created
Version: 1
resMap: map[_id:2 _index:some_index _primary_term:1 _seq_no:1 _shards:map[failed:0 successful:1 total:2] _type:_doc _version:1 forced_refresh:true result:created]



[Done] exited with code=0 in 3.3 seconds

postman中查看插入的數(shù)據(jù)

查詢文檔

查詢在 SomeStr 這個字段含有 Another 的文檔,在 main.go 里添加如下的代碼:

    // Search for the indexed document
    // Build the request body
    var buf bytes.Buffer
    query := map[string]interface{}{
        "query": map[string]interface{}{
            "match": map[string]interface{}{
                "SomeStr": "Another",
            },
        },
    }
 
    if err := json.NewEncoder(&buf).Encode(query); err != nil {
        log.Fatalf("Error encoding query: %s", err)
    }
 
    // Perform the search request.
    res, err = client.Search(
        client.Search.WithContext(context.Background()),
        client.Search.WithIndex("some_index"),
        client.Search.WithBody(&buf),
        client.Search.WithTrackTotalHits(true),
        client.Search.WithPretty(),
    )
 
    if err != nil {
        log.Fatalf("Error getting response: %s", err)
    }
 
    defer res.Body.Close()
 
    if res.IsError() {
        var e map[string]interface{}
        if err := json.NewDecoder(res.Body).Decode(&e); err != nil {
            log.Fatalf("Error parsing the response body: %s", err)
        } else {
            // Print the response status and error information.
            log.Fatalf("[%s] %s: %s",
                res.Status(),
                e["error"].(map[string]interface{})["type"],
                e["error"].(map[string]interface{})["reason"],
            )
        }
    }
 
 
    var  r map[string]interface{}
    if err := json.NewDecoder(res.Body).Decode(&r); err != nil {
        log.Fatalf("Error parsing the response body: %s", err)
    }
 
    // Print the response status, number of results, and request duration.
    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)),
    )
 
    // Print the ID and document source for each hit.
    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"])
    }

運行代碼后,可以看到如下新添加的結(jié)果:

[200 OK] 1 hits; took: 10ms
 * ID=2, map[SomeBool:%!s(bool=false) SomeInt:%!s(float64=42) SomeStr:Another Value]

刪除文檔

在 main.go 文件中,我們添加如下的代碼來刪除文檔 id 為 1 的文檔:

  // Set up the request object.
    req := esapi.DeleteRequest{
        Index:      "some_index",
        DocumentID: strconv.Itoa(1),
    }
 
    res, err = req.Do(context.Background(), client)
    if err != nil {
      log.Fatalf("Error getting response: %s", err)
    }

重新運行 main.go 應(yīng)用。我們再到 postman 中去查詢一下:

查詢結(jié)果顯示刪除成功。

Gin集成ES

創(chuàng)建一個項目gin_es

mkdir gin_es
cd gin_es
code . 
go mod init gin_es
go get github.com/elastic/go-elasticsearch/v7
go get github.com/gin-gonic/gin

創(chuàng)建兩個頁面

創(chuàng)建一個目錄templates,在該目錄下創(chuàng)建兩個頁面,一個用來查詢,一個用拉顯示結(jié)果

templates\query.html:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>gin集成es</title>
</head>

<body>
    <form action="/query" method="post">
        keyword:<input type="text" name="keyword">
        <input type="submit" value="查詢">
    </form>
</body>

</html>

templates\result.html:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>查詢結(jié)果</title>
</head>

<body>
    結(jié)果:{{.result}}
</body>

</html>

處理集成邏輯

package main

import (
	"context"
	"fmt"
	"log"
	"strings"

	"github.com/elastic/go-elasticsearch/v7"
	"github.com/elastic/go-elasticsearch/v7/esapi"
	"github.com/gin-gonic/gin"
)

func Query(c *gin.Context) {
	c.HTML(200, "query.html", nil)
}

func ESQuery(keyword string) *esapi.Response {
	client, err := elasticsearch.NewDefaultClient()
	if err != nil {
		log.Fatalf("獲取es客戶端錯誤: %s", err)
	}

	// 查詢條件
	query := `{"query":{"match":{"category":"` + keyword + `"}},"size":3}`

	fmt.Printf("query: %v\n", query)

	var b strings.Builder
	b.WriteString(query)
	read := strings.NewReader(b.String())

	// 查詢請求
	sr := &esapi.SearchRequest{
		Index: []string{"golang"},
		Body:  read,
	}

	// 執(zhí)行查詢
	r, err := sr.Do(context.Background(), client)
	if err != nil {
		log.Fatalf("查詢錯誤: %s", err)
	}
	return r
}

func DoQuery(c *gin.Context) {
	keyword := c.PostForm("keyword")

	r := ESQuery(keyword)

	c.HTML(200, "result.html", gin.H{
		"result": r,
	})
}

func main() {
	e := gin.Default()
	e.LoadHTMLGlob("templates/*")

	e.GET("/query", Query)
	e.POST("/query", DoQuery)

	e.Run()
}

測試

瀏覽器輸入:http://localhost:8080/query進入查詢頁面輸入護膚品進行查詢:

點擊查詢獲取查詢結(jié)果:

在postman中查看shopping索引中的文檔:

全查詢結(jié)果如下:

{
    "took": 599,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 4,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.0,
                "_source": {
                    "title": "sk2爽膚水",
                    "category": "護膚品",
                    "image": "https://www.tb.com",
                    "price": 899.0
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "2",
                "_score": 1.0,
                "_source": {
                    "title": "眼霜",
                    "category": "化妝品",
                    "image": "https://www.tb.com",
                    "price": 249.0
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "3",
                "_score": 1.0,
                "_source": {
                    "title": "拍立得",
                    "category": "數(shù)碼產(chǎn)品",
                    "image": "https://www.tb.com",
                    "price": 299.0
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "4",
                "_score": 1.0,
                "_source": {
                    "title": "科研洗面奶",
                    "category": "護膚品",
                    "image": "https://www.tb.com",
                    "price": 119.0
                }
            }
        ]
    }
}

可以看出查詢護膚品結(jié)果正確。

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

相關(guān)文章

  • 詳解Go語言如何實現(xiàn)一個最簡化的協(xié)程池

    詳解Go語言如何實現(xiàn)一個最簡化的協(xié)程池

    這篇文章主要為大家詳細(xì)介紹了Go語言如何實現(xiàn)一個最簡化的協(xié)程池,文中的示例代碼講解詳細(xì),具有一定的參考價值,有需要的小伙伴可以了解一下
    2023-10-10
  • Golang 中 omitempty的作用

    Golang 中 omitempty的作用

    這篇文章主要介紹了Golang 中 omitempty的作用,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考一下,需要的小伙伴可以參考一下
    2022-07-07
  • golang高并發(fā)限流操作 ping / telnet

    golang高并發(fā)限流操作 ping / telnet

    這篇文章主要介紹了golang高并發(fā)限流操作 ping / telnet,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • Go string轉(zhuǎn)int,int64,int32及注意事項說明

    Go string轉(zhuǎn)int,int64,int32及注意事項說明

    這篇文章主要介紹了Go string轉(zhuǎn)int,int64,int32及注意事項說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • 徹底理解golang中什么是nil

    徹底理解golang中什么是nil

    這篇文章主要介紹了golang中的nil用法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • go中結(jié)構(gòu)體切片的實現(xiàn)示例

    go中結(jié)構(gòu)體切片的實現(xiàn)示例

    Go語言中的結(jié)構(gòu)體切片是一種結(jié)合了結(jié)構(gòu)體和切片特點的數(shù)據(jù)結(jié)構(gòu),用于存儲和操作多個結(jié)構(gòu)體實例,具有一定的參考價值,感興趣的可以了解一下
    2024-11-11
  • 淺談Go1.18中的泛型編程

    淺談Go1.18中的泛型編程

    本文主要介紹了Go1.18中的泛型編程,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • 基于Go語言實現(xiàn)分金幣游戲

    基于Go語言實現(xiàn)分金幣游戲

    這篇文章主要為大家詳細(xì)介紹了如何利用Go語言實現(xiàn)分金幣游戲,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • golang fmt占位符的使用詳解

    golang fmt占位符的使用詳解

    這篇文章主要介紹了golang fmt占位符的使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • 一文帶你掌握Go語言運算符的使用

    一文帶你掌握Go語言運算符的使用

    運算符用于在程序運行時執(zhí)行數(shù)學(xué)或邏輯運算。Go 語言內(nèi)置的運算符有:算術(shù)運算符、關(guān)系運算符、邏輯運算符、位運算符、賦值運算符、其他運算符。本文將帶大家詳細(xì)了解一下這些運算符的使用,感興趣的可以了解一下
    2022-04-04

最新評論

前郭尔| 增城市| 五寨县| 衡南县| 临猗县| 利津县| 东宁县| 新巴尔虎左旗| 鄂州市| 惠来县| 灌阳县| 阳东县| 远安县| 长治市| 安宁市| 贵阳市| 讷河市| 山丹县| 蒙山县| 海淀区| 安仁县| 太保市| 上思县| 全南县| 阿荣旗| 长寿区| 天气| 衢州市| 南和县| 永修县| 鄯善县| 和龙市| 延庆县| 乌拉特后旗| 大兴区| 玉山县| 定安县| 凯里市| 扎囊县| 竹北市| 南开区|