如何在Go中实现Elasticsearch

 2023-09-06 阅读 25 评论 0

摘要:Today, I am going to show you how to implement Elasticsearch in Go.But of course, before that I am going to give a small introduction to Elasticsearch. If you have already gained a basic understanding of Elasticsearch, you can skip to the next part. 今天&

Today, I am going to show you how to implement Elasticsearch in Go.But of course, before that I am going to give a small introduction to Elasticsearch. If you have already gained a basic understanding of Elasticsearch, you can skip to the next part.

今天,我将向您展示如何在Go中实现Elasticsearch,但是,在此之前,我将先简要介绍一下Elasticsearch。 如果您已经对Elasticsearch有了基本的了解,则可以跳到下一部分。

弹性搜索 (Elasticsearch )

Elasticsearch has been gaining a lot of popularity lately. Searching in a Relational-Database always has issues around scalability and performance.

Elasticsearch最近获得了很大的欢迎。 在关系数据库中搜索始终会遇到有关可伸缩性和性能的问题。

Elasticsearch is a NoSQL database that has been very successful in tackling those issues. It provides great scalability and performance, and one of the most prominent features is the scoring system that allows a lot of flexibility in the search results. After all, it is not called Elastic-search for no reason!

Elasticsearch是NoSQL数据库,在解决这些问题方面非常成功。 它提供了出色的可伸缩性和性能,而最突出的功能之一就是评分系统,它使搜索结果具有很大的灵活性。 毕竟,它无缘无故被称为弹性搜索!

安装Elasticsearch (Installing Elasticsearch)

First, you will need to install Elasticsearch on your local machine. You can go to their website and get the installation guide for it. At the time I am writing this article, I am using Elasticsearch with the version number of 7.4.2 .

首先,您将需要在本地计算机上安装Elasticsearch。 您可以访问他们的网站并获取安装指南 。 在撰写本文时,我正在使用Elasticsearch,版本号为7.4.2。

Elasticsearch has been making a lot of changes in their versions, one of them being the removal of mapping type. So do not expect this to fully work if you are using another version of Elasticsearch.

Elasticsearch对其版本进行了大量更改,其中之一是删除映射类型。 因此,如果您正在使用其他版本的Elasticsearch,请不要期望这能够完全正常工作。

After finishing your installation, do not forget to run your elasticsearch service, which is mentioned quite clearly on their installation guide (for linux, in short do this ./bin/elasticsearch ).

完成安装后,请不要忘记运行您的elasticsearch服务,该服务在其安装指南中已明确提及(对于linux,简而言之,请执行./bin/elasticsearch )。

Make sure your elasticsearch is running by requesting into port 9200 in your local machine. GET localhost:9200

通过请求本地计算机上的端口9200 确保弹性搜索正在运行 。 GET localhost:9200

Hitting it should show something like below.

击中它应该显示类似下面的内容。

{"name": "204371","cluster_name": "elasticsearch","cluster_uuid": "8Aa0PznuR1msDL9-PYsNQg","version": {"number": "7.4.2","build_flavor": "default","build_type": "tar","build_hash": "2f90bbf7b93631e52bafb59b3b049cb44ec25e96","build_date": "2019-10-28T20:40:44.881551Z","build_snapshot": false,"lucene_version": "8.2.0","minimum_wire_compatibility_version": "6.8.0","minimum_index_compatibility_version": "6.0.0-beta1"},"tagline": "You Know, for Search"
}

If it's showing correctly then congratulations! You have successfully run your elasticsearch service in your local machine. Give yourself a clap and take a cup of coffee, since the day is still young.

如果显示正确,那么恭喜! 您已经在本地计算机上成功运行了elasticsearch服务。 鉴于自己还很年轻,请给自己鼓掌并喝杯咖啡。

使您的第一个索引 (Making your first index)

In Elasticsearch, index is similar to a database. Whereas before, there was table in elasticsearch called type. But since type has been removed in the current version, there are only index now.

在Elasticsearch中,索引类似于数据库。 以前,elasticsearch中有一个叫做type的表。 但是由于类型已在当前版本中删除,因此现在只有索引。

Confused now? Don't be. In a nutshell, just think that you only need index then afterwards you just need to insert your data into Elasticsearch.Now, we are going to make an index named students by doing the query below.PUT localhost/9200/students

现在感到困惑吗? 不用了 简而言之,只需考虑只需要索引,然后再将数据插入Elasticsearch即可。现在,我们将通过执行以下查询来创建一个名为students的索引.PUT localhost/9200/students

{"settings": {"number_of_shards": 1,"number_of_replicas": 1},"mappings": {"properties": {"name": {"type": "text"},"age": {"type": "integer"      },"average_score": {"type": "float"}}}
}

If nothing goes wrong, it should respond back by giving this.

如果没有任何问题,它应该通过给出此响应来进行响应。

{"acknowledged": true,"shards_acknowledged": true
}

Your index should be created. Now we will proceed to our next step: playing around with our Elasticsearch index.

您的索引应该被创建。 现在,我们将继续下一步:使用我们的Elasticsearch索引。

填充您的Elasticsearch (Populating your Elasticsearch)

First, what we will be doing now is filling in our Elasticsearch index with documents. If you are not familiar with that definition, just know that it is very similar to rows in a database.

首先,我们现在要做的是用文档填充我们的Elasticsearch索引。 如果您不熟悉该定义,请知道它与数据库中的行非常相似。

In a NoSQL database, it's actually possible for every document to contain different fields that don't match with the schema.

在NoSQL数据库中,实际上每个文档都可能包含与架构不匹配的不同字段。

But let's not do that – let's construct our column with a schema that we have defined before. The previous API will allow you to fill the document in your index.

但是,我们不要这样做–让我们使用之前定义的架构来构造我们的列。 先前的API将允许您将文档填充到索引中。

POST localhost:9200/students/doc

POST localhost:9200/students/doc

{"name":"Alice","age":17,"average_score":81.1
}

Your Elasticsearch should have one document by now. We will need to insert several more data into our Elasticsearch.  And of course, we are not going to insert our student data one by one - that would be quite a hassle!Elasticsearch has specifically prepared a bulk API in order to send multiple requests at once. Let's use that to insert multiple data at once.POST /students/_bulk

您的Elasticsearch现在应该有一个文档。 我们将需要在我们的Elasticsearch中插入更多数据。 当然,我们不会一个接一个地插入学生数据,这很麻烦!Elasticsearch专门准备了一个批量API,以便一次发送多个请求。 让我们用它一次插入多个数据。POST /students/_bulk

{ "index":{"_index": "students" } }
{ "name":"john doe","age":18, "average_score":77.7 }
{ "index":{"_index": "students" } }
{ "name":"bob","age":16, "average_score":65.5 }
{ "index":{"_index": "students" } }
{ "name":"mary doe","age":18, "average_score":97.7 }
{ "index":{"_index": "students" } }
{ "name":"eve","age":15, "average_score":98.9 }

让我们查询数据 (Let's query for the data)

We have finally populated our Elasticsearch with several more students' data. Now let's do what Elasticsearch is known for: we will try to search our Elasticsearch for the data that we just inserted.

我们终于在Elasticsearch中填充了更多学生的数据。 现在,让我们来做一下Elasticsearch众所周知的事情:我们将尝试在Elasticsearch中搜索刚刚插入的数据。

Elasticsearch supports many types of search mechanisms, but for this example we will be using a simple matching query.

Elasticsearch支持多种类型的搜索机制,但在此示例中,我们将使用简单的匹配查询。

Let's start our search by hitting this API:

让我们通过点击以下API开始搜索:

POST localhost:9200/_search

POST localhost:9200/_search

{"query" : {"match" : { "name" : "doe" }}
}

You will get back your response together with the students' data that matched with your corresponding query. Now you are officially a Search Engineer!

您将取回您的答案以及与您的相应查询匹配的学生数据。 现在您已正式成为搜索工程师!

{"took": 608,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 2,"relation": "eq"},"max_score": 0.74487394,"hits": [{"_index": "students","_type": "_doc","_id": "rgpef24BTFuh7kXolTpo","_score": 0.74487394,"_source": {"name": "john doe","age": 18,"average_score": 77.7}},{"_index": "students","_type": "_doc","_id": "sApef24BTFuh7kXolTpo","_score": 0.74487394,"_source": {"name": "mary doe","age": 18,"average_score": 97.7}}]}
}

现在开始吧! (Now let's get to Go!)

If you have reached this part, you should have grasped the very minimum concepts of using Elasticsearch. Now, we are going to implement Elasticsearch in Go.

如果您已经达到这一部分,那么您应该已经掌握了使用Elasticsearch的最低限度的概念。 现在,我们将在Go中实现Elasticsearch。

A very primitive way of implementing Elasticsearch is that you can keep doing http requests into your Elasticsearch IP. But we are not going to do that.

实施Elasticsearch的一种非常原始的方法是,您可以继续将HTTP请求发送到您的Elasticsearch IP中。 但是我们不会那样做。

I found this very helpful library for implementing Elasticsearch in Go. You should install that library before you proceed in your Go modules.

我发现这对在Go中实现Elasticsearch很有帮助。 在继续执行Go模块之前,应先安装该库。

做你的结构 (Make your struct)

First of all, you will definitely need to make a struct for your Model. In this example, we are going to use the same modeling as in our previous example which in this case is the Student struct.

首先,您肯定需要为模型制作一个结构。 在此示例中,我们将使用与先前示例相同的建模,在本例中为Student结构。

package maintype Student struct {Name         string  `json:"name"`Age          int64   `json:"age"`AverageScore float64 `json:"average_score"`
}

建立客户端连接 (Making a Client Connection)

Now, let's make a function that'll allow us to initialize our ES Client connection. If you have a running instance of Elasticsearch outside of your localhost, you can simply change the part inside SetURL.

现在,让我们创建一个函数,该函数将允许我们初始化ES Client连接。 如果您在本地主机外部有一个正在运行的Elasticsearch实例,则只需更改SetURL的部分SetURL

func GetESClient() (*elastic.Client, error) {client, err :=  elastic.NewClient(elastic.SetURL("http://localhost:9200"),elastic.SetSniff(false),elastic.SetHealthcheck(false))fmt.Println("ES initialized...")return client, err}

资料插入 (Data Insertion)

After that, the first thing we can do is try to insert our data into Elasticsearch via Go. We will be making a model of Student and inserting it into our Elasticsearch client.

之后,我们要做的第一件事就是尝试通过Go将数据插入到Elasticsearch中。 我们将制作一个Student模型,并将其插入到我们的Elasticsearch客户端中。

package mainimport ("context""encoding/json""fmt"elastic "gopkg.in/olivere/elastic.v7"
)func main() {ctx := context.Background()esclient, err := GetESClient()if err != nil {fmt.Println("Error initializing : ", err)panic("Client fail ")}//creating student objectnewStudent := Student{Name:         "Gopher doe",Age:          10,AverageScore: 99.9,}dataJSON, err := json.Marshal(newStudent)js := string(dataJSON)ind, err := esclient.Index().Index("students").BodyJson(js).Do(ctx)if err != nil {panic(err)}fmt.Println("[Elastic][InsertProduct]Insertion Successful")}

查询我们的数据 (Querying our Data)

Finally, we can do some searching. The below code might look a bit complex. But rest assured, it will make more sense to you after you go through it carefully. I will be using a basic matching query in the below example.

最后,我们可以进行一些搜索。 下面的代码可能看起来有些复杂。 但是请放心,在您仔细地进行操作之后,它对您会更有意义。 在下面的示例中,我将使用基本的匹配查询。

package mainimport ("context""encoding/json""fmt"elastic "gopkg.in/olivere/elastic.v7"
)func main() {ctx := context.Background()esclient, err := GetESClient()if err != nil {fmt.Println("Error initializing : ", err)panic("Client fail ")}var students []StudentsearchSource := elastic.NewSearchSource()searchSource.Query(elastic.NewMatchQuery("name", "Doe"))/* this block will basically print out the es query */queryStr, err1 := searchSource.Source()queryJs, err2 := json.Marshal(queryStr)if err1 != nil || err2 != nil {fmt.Println("[esclient][GetResponse]err during query marshal=", err1, err2)}fmt.Println("[esclient]Final ESQuery=\n", string(queryJs))/* until this block */searchService := esclient.Search().Index("students").SearchSource(searchSource)searchResult, err := searchService.Do(ctx)if err != nil {fmt.Println("[ProductsES][GetPIds]Error=", err)return}for _, hit := range searchResult.Hits.Hits {var student Studenterr := json.Unmarshal(hit.Source, &student)if err != nil {fmt.Println("[Getting Students][Unmarshal] Err=", err)}students = append(students, student)}if err != nil {fmt.Println("Fetching student fail: ", err)} else {for _, s := range students {fmt.Printf("Student found Name: %s, Age: %d, Score: %f \n", s.Name, s.Age, s.AverageScore)}}}

The query should be printed out like this:

查询应该像这样打印出来:

ES initialized...
[esclient]Final ESQuery={"query":{"match":{"name":{"query":"Doe"}}}}

And yes that query is what will be posted into the Elasticsearch.

是的,该查询就是将要发布到Elasticsearch中的内容。

The result of your query should also come out like this if you have followed my example since the very start:

如果从一开始就遵循我的示例,则查询结果也应如下所示:

Student found Name: john doe, Age: 18, Score: 77.700000 
Student found Name: mary doe, Age: 18, Score: 97.700000 
Student found Name: Gopher doe, Age: 10, Score: 99.900000

And there you go!

然后你去了!

That's the end of my tutorial about how to implement Elasticsearch in Go. I hope I have covered the very basic parts of using Elasticsearch in Go.

我的教程到此结束,有关如何在Go中实现Elasticsearch。 我希望我已经介绍了在Go中使用Elasticsearch的非常基本的部分。

To get further info on this topic, you should read about Query DSL and Function Scoring in Elasticsearch, which in my opinion one of the best things about Elasticsearch.

要获得有关此主题的更多信息,您应该阅读Elasticsearch中的Query DSL和Function Scoring ,在我看来,这是Elasticsearch最好的东西之一。

And fret not, the library used in this example also supports a lot of Elasticsearch features, even the Function Scoring query in Elasticsearch.

不用担心,此示例中使用的库还支持许多Elasticsearch功能,甚至包括Elasticsearch中的Function Scoring查询。

Thanks for reading through my article! I do hope it will be useful and can help you getting started using Elasticsearch.

感谢您阅读我的文章! 我确实希望它会有用,并且可以帮助您开始使用Elasticsearch。

Never stop learning; knowledge doubles every fourteen months. ~Anthony J.D'Angelo
从未停止学习; 知识每十四个月翻一番。 〜安东尼·安杰洛

翻译自: https://www.freecodecamp.org/news/go-elasticsearch/

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/7135.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息