golang常用库之olivere elastic包 | go操作elasticsearch

golang常用库之elastic包 | 操作elasticsearch

olivere/elastic/​​ 这个版本被广泛使用,我们也用这个。

olivere elastic包

官网: https://olivere.github.io/elastic/
github: https://github.com/olivere/elastic

Go的选择很少:最著名的是olivere/elastic ,而官方的则是elastic/go-elasticsearch。
olivere elastic作为golang比较常用的elasticsearch工具包。

Elastic是GO编程语言的Elasticsearch的客户端。自2012年以来,我们在生产中使用它。它支持Elasticsearch版本1.x,2.x,5.x,6.x和7.x。

注意:es版本不同,要导入不同的包。

olivere elastic包 使用

官方文档:https://pkg.go.dev/github.com/olivere/elastic

Go语言操作ElasticSearch(基本操作)
参考URL: https://blog.csdn.net/my_miuye/article/details/110496025
golang操作elasticsearch(oliver/elastic使用文档)
参考URL: https://blog.csdn.net/p1049990866/article/details/117254708
Go操作elasticsearch
参考URL: https://www.putianhui.cn/posts/1e4561eceb2d/
数据操作(3)go操作ElasticSearch
参考URL: https://zhuanlan.zhihu.com/p/347419756

在 Elasticsearch中,索引类似于数据库。 以前,elasticsearch 中有一个叫做 type 的表。 但是由于类型已在当前版本中删除,因此现在只有索引。
我们创建了一个名为 indexName 的索引。在接下的步骤中,我们将想这个索引里写入一些数据,也就是一些文档。
我们现在要做的是用文档填充我们的 Elasticsearch 索引。 如果你不熟悉该定义,请知道它与数据库中的行非常相似。

go 测试demo代码

我们使用第三方库https://github.com/olivere/elastic 来连接ES并进行操作。

官方https://github.com/olivere/elastic ,就有对应 _test.go 测试文件,参考相关代码即可。

如下:测试demo:
json入库es

setup_test.go文件

package es_demo

import (
	"fmt"
	"time"
)

const (
	testIndexName      = "elastic-test"
)

type tweet struct {
	Uuid     string        `json:"uuid"`
	User     string        `json:"user"`
	Message  string        `json:"message"`
	Retweets int           `json:"retweets"`
	Image    string        `json:"image,omitempty"`
	Created  time.Time     `json:"created,omitempty"`
	Tags     []string      `json:"tags,omitempty"`
	Location string        `json:"location,omitempty"`
}

func (t tweet) String() string {
	return fmt.Sprintf("tweet{User:%q,Message:%q,Retweets:%d}", t.User, t.Message, t.Retweets)
}

es_test.go

package es_demo

import (
	"context"
	"fmt"
	elastic "github.com/olivere/elastic/v7"
	uuid "github.com/satori/go.uuid"
	"testing"
	"time"
)



func TestIndex(t *testing.T)  {
	Client, err := elastic.NewClient(elastic.SetURL("http://127.0.0.1:9200"))
	fmt.Println(Client, err)

	tweet1 := tweet{Uuid: uuid.NewV4().String(),User: "olivere", Message: "Welcome to Golang and Elasticsearch.",
		Created: time.Date(2014, 1, 18, 23, 59, 58, 0, time.UTC)}
	// Add a document
	indexResult, err := Client.Index().
		Index(testIndexName).
		//Id("1").
		BodyJson(&tweet1).
		Do(context.TODO())
	if err != nil {
		t.Fatal(err)
	}
	if indexResult == nil {
		t.Errorf("expected result to be != nil; got: %v", indexResult)
	}

}

ES基本概念与关系型数据库的比较

在这里插入图片描述以前,elasticsearch 中有一个叫做 type 的表。 但是由于类型已在当前版本中删除,因此现在只有索引。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Elasticsearch是一个开源的分布式搜索和分析引擎,它提供了强大的全文搜索、结构化查询、分析能力和实时数据分析等功能。而Golang是一种编程语言,也被称为Go语言,它具有高效、简洁、并发安全等特点。 在Golang中使用Elasticsearch可以通过第三方进行操作,最常用的是官方提供的Elasticsearch客户端——"github.com/elastic/go-elasticsearch"。这个提供了与Elasticsearch进行交互的API,可以进行索引、搜索、聚合等操作。 使用go-elasticsearch,你可以通过以下步骤来使用Elasticsearch: 1. 安装go-elasticsearch:在终端中执行命令`go get github.com/elastic/go-elasticsearch/v8`来安装该。 2. 导入:在你的Go代码中导入"go.elastic.co/elasticsearch/v8"。 3. 创建Elasticsearch客户端:使用提供的`elasticsearch.NewClient()`函数创建一个Elasticsearch客户端实例。 4. 执行操作:通过客户端实例调用相应的API方法来执行索引、搜索、聚合等操作。 以下是一个简单的示例代码,展示了如何使用go-elasticsearch进行基本的索引和搜索操作: ```go package main import ( "context" "fmt" "log" "github.com/elastic/go-elasticsearch/v8" ) func main() { // 创建Elasticsearch客户端 cfg := elasticsearch.Config{ Addresses: []string{"http://localhost:9200"}, } es, err := elasticsearch.NewClient(cfg) if err != nil { log.Fatalf("Error creating the client: %s", err) } // 索引文档 doc := `{"title" : "Elasticsearch Golang Example"}` res, err := es.Index("my-index", strings.NewReader(doc)) if err != nil { log.Fatalf("Error indexing document: %s", err) } defer res.Body.Close() // 搜索文档 var buf bytes.Buffer query := map[string]interface{}{ "query": map[string]interface{}{ "match": map[string]interface{}{ "title": "example", }, }, } if err := json.NewEncoder(&buf).Encode(query); err != nil { log.Fatalf("Error encoding query: %s", err) } res, err = es.Search( es.Search.WithContext(context.Background()), es.Search.WithIndex("my-index"), es.Search.WithBody(&buf), es.Search.WithTrackTotalHits(true), es.Search.WithPretty(), ) if err != nil { log.Fatalf("Error searching for documents: %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) } fmt.Println(r) } ``` 这只是一个简单的示例,你可以根据自己的需求进行更复杂的操作。你可以参考go-elasticsearch的文档(https://pkg.go.dev/github.com/elastic/go-elasticsearch/v8)来了解更多关于使用ElasticsearchGolang的信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

西京刀客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值