Categorygithub.com/gjbae1212/go-esworker
modulepackage
1.1.2
Repository: https://github.com/gjbae1212/go-esworker.git
Documentation: pkg.go.dev

# README

go-esworker

go-esworker is an async worker that documents can bulk insert, update, delete to the elasticsearch using Golang. It is support to an infrastructure on AWS, GCP, Elastic Cloud, and so on.

license

Installation

go get -u github.com/gjbae1212/go-esworker

Usage

import (
	"context"
	"log"

	"github.com/gjbae1212/go-esworker"
)

func main() {

	// Create dispatcher
	dispatcher, err := esworker.NewDispatcher(
    		esworker.WithESVersionOption(esworker.V6),
    		esworker.WithAddressesOption([]string{"http://localhost:9200"}),
    		esworker.WithUsernameOption("user"),
    		esworker.WithPasswordOption("password"),
    		esworker.WithErrorHandler(func(err error) {
    			log.Println(err)
    		}),
    	)
    	if err != nil {
    		log.Panic(err)
    	}

	// Start dispatcher
	if err := dispatcher.Start(); err != nil {
		log.Panic(err)
	}

	// Process operations in bulk.
	ctx := context.Background()
	// create doc
	dispatcher.AddAction(ctx, &esworker.StandardAction{
		op:    esworker.ES_CREATE,
		index: "allan",
		id:    "1",
		doc:   map[string]interface{}{"field1": 10},
	})

	// update doc
	dispatcher.AddAction(ctx, &esworker.StandardAction{
		op:    esworker.ES_UPDATE,
		index: "allan",
		id:    "1",
		doc:   map[string]interface{}{"field1": 20},
	})

	// delete doc
	dispatcher.AddAction(ctx, &esworker.StandardAction{
		op:    esworker.ES_DELETE,
		index: "allan",
		id:    "1",
	})
}

Dispatcher Parameters

It should pass parameters for dependency injection when you are creating a go-esworker dispatcher.
A list to support the parameters below.

method namedescriptionvaluestate
WithESVersionOptionElasticSearch Versionesworker.V5, esworker.V6, esworker.V7default V6
WithAddressesOptionElasticSearch Addressdefault http://localhost:9200
WithUsernameOptionElasticSearch Username for HTTP basic authenticationoptional
WithPasswordOptionElasticSearch Password for HTTP basic authenticationoptional
WithCloudIdOptionID for Elastic Cloudoptional
WithApiKeyOptionBase64-Encoded value for authorization(api-key)optional(if set, overrides username and password)
WithTransportOptionHttp transportdefault http default transport
WithLoggerOptionLoggeroptional
WithGlobalQueueSizeOptionGlobal queue max sizedefault 5000
WithWorkerSizeOptionWorker sizedefault 5
WithWorkerQueueSizeOptionWorker max queue sizedefault 5
WithWorkerWaitIntervalDeal with data in worker queue after every interval timedefault 2 * time.Second
WithErrorHandlerA function that deals with an error when an error is raisedoptional

Action Interface

To deal with operation as insert and update and delete to, you would use to the StandardAction struct or a struct which is implementing esworker.Action interface.

// generate and start dispatcher 
dispatcher, _ := esworker.NewDispatcher()
dispatcher.Start()

// Ex) Standard Action Example
act := &esworker.StandardAction{
	Op: ES_CREATE
	Index: "sample",
	DocType: "_doc",
	Id: "test-id",
	Doc: map[string]interface{}{"field": 1},
}
dispatcher.AddAction(context.Background(), act)


// Ex) Custom Action Example
sampleAction struct {}

func (act *sampleAction) GetOperation() esworker.ESOperation {
	// return esworker.ES_CREATE
	// return esworker.ES_INDEX
	// return esworker.ES_UPDATE
	// return esworker.ES_DELETE    
}

func (act *sampleAction) GetIndex() string {
	// return "your index name"
}

func (act *sampleAction) GetDocType() string {
	//return ""
	//return "doc type"	
}

func (act *sampleAction) GetID() string {
	//return ""
	//return "doc id"		
}

func (act *sampleAction) GetDoc() map[string]interface{} {
	//return map[string]interface{}{}
}
dispatcher.AddAction(context.Background(), &sampleAction{})

If you will make to a custom struct which is implementing esworker.Action interface, it must implement 5 methods.

namedescription
GetOperationES_CREATE, ES_INDEX, ES_UPDATE, ES_DELETE
GetIndexindex name
GetDocTypedoc type (if it is returned an empty string, default _doc or doc)
GetIDdoc id (if an operation is ES_INDEX, possible empty string)
GetDocdoc data

Elastic Cloud

If you use to infrastructure on Elastic Cloud, you could access to ElasticSearch without endpoint and basic authentication. (How to use API-KEY)

dispatcher, err := esworker.NewDispatcher(
		esworker.WithESVersionOption(esworker.V7),
		esworker.WithCloudIdOption("your-cloud-id"),
		esworker.WithApiKeyOption("api-key"),
)
dispatcher.Start()

LICENSE

This project is following The MIT.

# Functions

NewDispatcher is to make Dispatcher.
WithAddressesOption has associated a list of elastic search nodes.
WithApiKeyOption has associated apikey for authorization.
WithCloudIdOption has associated cloud-id about endpoint for elastic cloud.
WithErrorHandler has associated a handler called when an error is raised.
WithESVersionOption has associated version that elastic search nodes are running.
WithGlobalQueueSizeOption has associated queue size in global.
WithLoggerOption has associated logger object.
WithPasswordOption has associated password for HTTP basic authentication.
WithTransportOption has associated http transport object.
WithUsernameOption has associated username for HTTP basic authentication.
WithWorkerQueueSizeOption has associated queue size at a worker.
WithWorkerSizeOption has associated size for running workers.
WithWorkerWaitInterval has associated wait time at a worker.

# Constants

ESOperation supports to index, create, update, delete.
ESOperation supports to index, create, update, delete.
ESOperation supports to index, create, update, delete.
ESOperation supports to index, create, update, delete.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

it is response structs of elasticserach.
it is response structs of elasticserach.
it is response structs of elasticserach.
it is response structs of elasticserach.
it is response structs of elasticserach.
Logger is an intermediate struct to be changed elastic logger.
StandardAction is a struct to implement an interface of Action.

# Interfaces

No description provided by the author
No description provided by the author
ESProxy is an interface that actually request the elasticserach.
Option is something for dependency injection.

# Type aliases

ErrorHandler is called when an error is raised.
ESOperation is a type of elasticsearch.
ESVersion would identify a version about elastic search.
No description provided by the author
OptionFunc is a practical struct for Option.