Go Elasticsearch (CRUD) quick start

Article catalogue

1. Introduction

Introduction to Elasticsearch

2. Client Library

The popular Go client of ES includes the official library of Elastic company go-elasticsearch And third party libraries olivere/elastic , the latter is more popular.

Because the project uses olivere/elastic/v7 Therefore, this paper will introduce how to complete the addition, deletion, modification and query of ES through this library.

Note that different versions of ES need to use the Oliver / elastic package of the corresponding version, and the corresponding relationship is as follows:

Elasticsearch version

Elastic version

Package URL

Remarks

7.x

7.0

github.com/olivere/elastic/v7

Use Go modules

6.x

6.0

github.com/olivere/elastic

Use a dependency manager

5.x

5.0

gopkg.in/olivere/elastic.v5

Actively maintained

This time, 7.5% of ES is used X version, so use GitHub COM / Oliver / elastic / V7 as the client library, use go Mod to manage dependencies:

require(
	github.com/olivere/elastic/v7 v7.0.24
)

3. Create client

So many basic concepts have been laid in front. Now we officially start the addition, deletion, modification and inspection of Go ES.

Before starting the actual combat, first introduce the functions to be realized in the code example of this article:

  • Add user information
  • Update user information
  • Delete user information
  • Query individual users by ID
  • Query relevant users by page according to user information

Before development, you need to create a client to operate ES. The singleton pattern is used here.

// ES client
var (
	esOnce sync.Once
	esCli  *elastic.Client
)

// GetESClient get ES client
func GetESClient() *elastic.Client {
	if esCli != nil {
		return esCli
	}

	esOnce.Do(func() {
		cli, err := elastic.NewSimpleClient(
			elastic.SetURL("http://test.es.db "), / / service address
			elastic.SetBasicAuth("user", "secret"), // Account password
			elastic.SetErrorLog(log.New(os.Stderr, "", log.LstdFlags)), 	// Set error log output
			elastic.SetInfoLog(log.New(os.Stdout, "", log.LstdFlags)),  	// Set info log output
		)
		if err != nil {
			panic("new es client failed, err=" + err.Error())
		}
		esCli = cli
	})
	return esCli
}

The ES client is created using the NewSimpleClient() method. Of course, two other methods can be used:

// NewClient creates a new client to work with Elasticsearch.
func NewClient(options ...ClientOptionFunc) (*Client, error)

// NewClientFromConfig initializes a client from a configuration
func NewClientFromConfig(cfg *config.Config) (*Client, error)

ES connection parameters can be provided during creation. The above list is incomplete. Let's introduce it to you.

elastic.SetURL(url) Used to set ES The service address, if local, is 127.0.0.1:9200. Multiple addresses are supported, separated by commas
elastic.SetBasicAuth("user", "secret") This is based on http base auth Account and password of authentication mechanism
elastic.SetGzip(true) start-up gzip compress
elastic.SetHealthcheckInterval(10*time.Second) Used to set the monitoring inspection interval
elastic.SetMaxRetries(5) Set the maximum number of retries for failed requests, v7 Version has been deprecated since
elastic.SetSniff(false) Set whether to check the cluster regularly (the default is true)
elastic.SetErrorLog(log.New(os.Stderr, " ", log.LstdFlags)) Set error log output
elastic.SetInfoLog(log.New(os.Stdout, "", log.LstdFlags)) set up info Log output

4.index addition, deletion, modification and query

Go Elasticsearch index

5. Increase

Go Elasticsearch add QuickStart

6. Delete

Go Elasticsearch delete quick start

7. Modification

Go Elasticsearch update quick start

8. Query

Go Elasticsearch query quick start

9. Summary

This paper starts with the basic concept of ES, then completes the basic addition, deletion, modification and query of ES through the package GitHub / Oliver / elastic / V7, and gives the corresponding RESTful API operation statements.

The function of ES is so powerful that there are so many client interfaces. Coupled with the lack of easy to understand documentation, it is really dazzling and not so friendly for beginners.

As for the search ability of ES, this article does not give too many relevant examples, and there will be another opportunity to continue the blog post later.

reference

github/elastic/elasticsearch
github/olivere/elastic/v7
pkg.go.dev/github.com/olivere/elastic/v7
DB-Engines Ranking
Baidu Encyclopedia elasticsearch
Cloud + community Comprehensive analysis of Elasticsearch data update
Introduction to golang elasticsearch - ladder tutorial network
golang elasticsearch query tutorial - ladder tutorial network
Elasticsearch: authoritative guide
Getting started with go elastic search (1)
Golang DreamWorks Go elastic search practical chapter, take you to learn to add, delete, modify and check
Elasticsearch - three states of dynamic in mappings
Elasticsearch Guide [7.13] ? REST APIs ? Document APIs ? ?refresh
Elasticsearch Guide [7.13] ? REST APIs ? Document APIs ? Delete by query API
Elasticsearch Guide [7.14] ? Search your data ? Retrieve selected fields from a search
Elasticsearch Guide [7.14] ? Query DSL ? Compound queries ? Boolean query
Elasticsearch Guide [8.0] ? Mapping ? Field data types

Tags: Java Back-end

Posted by jf3000 on Mon, 18 Apr 2022 11:39:30 +0930