elsearm

package module
v0.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 14, 2021 License: Apache-2.0 Imports: 14 Imported by: 0

README

elsearm

CircleCI Go Report Card PkgGoDev

elsearm is an Elasticsearch model library for Go.

Features

  • Only has elastic/go-elasticsearch as a dependency.
  • Easy update/remove to Elasticsearch when saving to DB.
  • Can set prefix and suffix of index name.

Usage

Automatic Document Updates (with GORM)

If the ORM library support hooks, you can use Automatic Document Updates with libraries other than GORM.
This is an example of using GORM.

package models

import (
	"github.com/soranoba/elsearm"
	"gorm.io/gorm"
)

const (
	elsearmIndexerKey = "elsearm:indexer"
)

func UseElsearmIndexer(db *gorm.DB, indexer *elsearm.Indexer) *gorm.DB {
	return db.InstanceSet(elsearmIndexerKey, indexer)
}

func getElsearmIndexer(db *gorm.DB) (*elsearm.Indexer, bool) {
	val, ok := db.InstanceGet(elsearmIndexerKey)
	if !ok {
		return nil, false
	}
	indexer, ok := val.(*elsearm.Indexer)
	return indexer, ok
}

type User struct {
	ID        uint      `gorm:"primarykey" json:"id"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
}

func (u *User) AfterDelete(db *gorm.DB) error {
	if indexer, ok := getElsearmIndexer(db); ok {
		indexer.Delete(u)
	}
	return nil
}

func (u *User) AfterSave(db *gorm.DB) error {
	if indexer, ok := getElsearmIndexer(db); ok {
		indexer.Update(u)
	}
	return nil
}
func main() {
	db := (func() *gorm.DB {
		return /* do anything */
	})()
	indexer := elsearm.NewIndexer(ct.es)
	db = models.UseElsearmIndexer(db, indexer)

	/* do anyting */
	db.Create(&models.User{})
}
Using another index name with tests.

You can set prefix and/or suffix in global config.
It makes it easy to avoid overwrite to the same index when it execute testings.

elsearm.SetGlobalConfig(elsearm.GlobalConfig{
	IndexNameSuffix: "_test",
})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultDocumentBody

func DefaultDocumentBody(model interface{}) (io.Reader, error)

DefaultDocumentBody returns a default DocumentBody.

func DefaultDocumentID

func DefaultDocumentID(model interface{}) string

DefaultDocumentID returns a default DocumentID.

func DefaultIndexName

func DefaultIndexName(model interface{}) string

DefaultIndexName returns a default IndexName.

func DefaultParseDocument

func DefaultParseDocument(model interface{}, reader io.Reader) error

DefaultParseDocument parse the content of reader, and update the model.

func DocumentBody

func DocumentBody(model interface{}) (io.Reader, error)

DocumentBody transforms the model into a data structure that is stored in Elasticsearch. By default, it execute json.Marshal.

func DocumentID

func DocumentID(model interface{}) (string, error)

DocumentID returns a document id of the model. By default, it returns value of id or ID field in the model. Otherwise, it returns an empty string.

func IndexName

func IndexName(model interface{}) string

IndexName returns an index name of the model. By default, it returns converted to snake case the struct name of model.

func IndexNameWithAffix added in v0.4.0

func IndexNameWithAffix(indexName string) string

IndexNameWithAffix returns an index name appending prefix and suffix.

func IndexNamesWithAffix added in v0.9.0

func IndexNamesWithAffix(indexNames []string) []string

IndexNamesWithAffix returns index names appending prefix and suffix.

func MustDocumentBody

func MustDocumentBody(model interface{}) io.Reader

MustDocumentBody is similar to DocumentBody. It will panic if the DocumentBody returns an error.

func ParseDocument

func ParseDocument(model interface{}, reader io.Reader) error

ParseDocument parses and applies the value to the model. By default, it execute json.Unmarshal.

func SearchIndexName added in v0.4.0

func SearchIndexName(model interface{}) []string

SearchIndexName returns an index name of the model when searching. By default, it returns the same index name as the return value of IndexName.

func SetDocumentID added in v0.8.0

func SetDocumentID(model interface{}, id string) error

SetDocumentID set the DocumentID to the model. By default, no executed.

func SetGlobalConfig

func SetGlobalConfig(cfg GlobalConfig)

SetGlobalConfig sets the config that applies globally.

func Zero added in v0.3.0

func Zero() *int

Zero returns a pointer of int. The value of address is zero.

Types

type AutomaticIDModel added in v0.8.0

type AutomaticIDModel interface {
	CustomDocumentIdModel
	// SetDocumentID set the DocumentID. If it failed, it returns an error.
	SetDocumentID(id string) error
}

AutomaticIDModel is an interface to implement when Elasticsearch automatically creates id of the model.

type BulkIndexer added in v0.6.0

type BulkIndexer struct {
	// contains filtered or unexported fields
}

BulkIndexer provides functions to bulk insert/update document in Elasticsearch.

func NewBulkIndexer added in v0.6.0

func NewBulkIndexer(bulk esutil.BulkIndexer) *BulkIndexer

NewIndexer creates an Indexer.

func (*BulkIndexer) CreateWithoutID added in v0.6.0

func (indexer *BulkIndexer) CreateWithoutID(model interface{}) error

CreateWithoutID create a document in index without DocumentID. Returns an error if the addition to the bulk indexer fails.

func (*BulkIndexer) Delete added in v0.6.0

func (indexer *BulkIndexer) Delete(model interface{}) error

Delete a document from Index. Returns an error if the addition to the bulk indexer fails.

func (*BulkIndexer) Update added in v0.6.0

func (indexer *BulkIndexer) Update(model interface{}) error

Update (or create) the document in index. Returns an error if the addition to the bulk indexer fails.

func (*BulkIndexer) WithContext added in v0.6.0

func (indexer *BulkIndexer) WithContext(ctx context.Context) *BulkIndexer

WithContext specifies a context to use and returns a new BulkIndexer.

type CustomDocumentBodyModel

type CustomDocumentBodyModel interface {
	GetDocumentBody() (io.Reader, error)
	ParseDocument(io.Reader) error
}

CustomDocumentBodyModel is an interface to implement when customizing DocumentBody of model.

type CustomDocumentIdModel

type CustomDocumentIdModel interface {
	// GetDocumentID returns an DocumentID. If the DocumentID is not exist, it returns an error.
	GetDocumentID() (string, error)
}

CustomDocumentIdModel is an interface to implement when customizing DocumentID of model.

type CustomIndexNameModel

type CustomIndexNameModel interface {
	// GetIndexName returns an IndexName.
	GetIndexName() string
}

CustomIndexNameModel is an interface to implement when customizing IndexName of model.

type CustomSearchIndexNameModel added in v0.4.0

type CustomSearchIndexNameModel interface {
	// GetSearchIndexName returns an IndexName which is search target.
	GetSearchIndexName() []string
}

CustomSearchIndexNameModel is an interface to implement when customizing IndexName of model when searching.

type ErrorResponse added in v0.2.0

type ErrorResponse struct {
	Status uint `json:"status"`
	Err    struct {
		Type      string `json:"type"`
		Reason    string `json:"reason"`
		RootCause []struct {
			Type   string `json:"type"`
			Reason string `json:"reason"`
		} `json:"root_cause"`
		CausedBy struct {
			Type   string `json:"type"`
			Reason string `json:"reason"`
		} `json:"caused_by"`
	} `json:"error"`
}

ErrorResponse is an error response format of Elasticsearch.

func (*ErrorResponse) Error added in v0.2.0

func (err *ErrorResponse) Error() string

type GlobalConfig

type GlobalConfig struct {
	// A prefix of index names. It is included in the return value of elsearm.IndexName.
	IndexNamePrefix string
	// A suffix of index names. It is included in the return value of elsearm.IndexName.
	IndexNameSuffix string
}

type Indexer

type Indexer struct {
	Q *esapi.API
	// contains filtered or unexported fields
}

Indexer provides functions to update/delete document in Elasticsearch.

func NewIndexer

func NewIndexer(client *elasticsearch.Client) *Indexer

NewIndexer creates an Indexer.

func (*Indexer) Count added in v0.3.0

func (indexer *Indexer) Count(model interface{}, reqFuncs ...func(*esapi.CountRequest)) (int, error)

Count returns count of documents saved in index.

func (*Indexer) CreateIndex

func (indexer *Indexer) CreateIndex(model interface{}, reqFuncs ...func(*esapi.IndicesCreateRequest)) error

CreateIndex creates an index that to save the model. If it already exists, it returns an error.

func (*Indexer) CreateIndexIfNotExist

func (indexer *Indexer) CreateIndexIfNotExist(model interface{}, reqFuncs ...func(*esapi.IndicesCreateRequest)) error

CreateIndexIfNotExist creates an index, if it to save the model does not exist.

func (*Indexer) CreateWithoutID added in v0.3.0

func (indexer *Indexer) CreateWithoutID(model interface{}, reqFuncs ...func(*esapi.IndexRequest)) error

CreateWithoutID create a document in index without DocumentID.

func (*Indexer) Delete

func (indexer *Indexer) Delete(model interface{}, reqFuncs ...func(*esapi.DeleteRequest)) error

Delete a document from Index.

func (*Indexer) DeleteIndex

func (indexer *Indexer) DeleteIndex(model interface{}, reqFuncs ...func(*esapi.IndicesDeleteRequest)) error

DeleteIndex deletes an index that to save the model.

func (*Indexer) Do added in v0.3.0

func (indexer *Indexer) Do(req Request, models ...interface{}) error

Do execute the request. When models specified, it parses and set a model if succeeded.

func (*Indexer) Get

func (indexer *Indexer) Get(model interface{}, reqFuncs ...func(*esapi.GetRequest)) error

Get a document from Index.

func (*Indexer) Scroll added in v0.5.0

func (indexer *Indexer) Scroll(model interface{}, reqFuncs ...func(*esapi.ScrollRequest)) (*SearchResult, error)

Scroll the search results, and set results to the model.

func (*Indexer) Search added in v0.3.0

func (indexer *Indexer) Search(models interface{}, reqFuncs ...func(*esapi.SearchRequest)) (*SearchResult, error)

Search documents in the index, and set results to the model.

func (*Indexer) Update

func (indexer *Indexer) Update(model interface{}, reqFuncs ...func(*esapi.IndexRequest)) error

Update (or create) the document in index.

func (*Indexer) WithContext added in v0.3.0

func (indexer *Indexer) WithContext(ctx context.Context) *Indexer

WithContext specifies a context to use and returns a new Indexer.

type Request added in v0.3.0

type Request interface {
	Do(ctx context.Context, transport esapi.Transport) (*esapi.Response, error)
}

A interface of Request defined by esapi.

type SearchResponse added in v0.5.0

type SearchResponse struct {
	ScrollID string `json:"_scroll_id"`
	Hits     struct {
		Total struct {
			Value    int    `json:"value"`
			Relation string `json:"relation"`
		} `json:"total"`
		Hits []struct {
			ID     string          `json:"_id"`
			Source json.RawMessage `json:"_source"`
		} `json:"hits"`
	} `json:"hits"`
}

SearchResponse is an response format of search API. ref: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html

func (*SearchResponse) SetResult added in v0.5.0

func (res *SearchResponse) SetResult(models interface{}) error

SetResult copies the hit result to models.

type SearchResult added in v0.5.0

type SearchResult struct {
	// A search context
	ScrollID string
	// Total number of hits
	Total int
	// Accuracy of the total. The value is `eq` or `gte`.
	// ref: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html
	TotalAccuracy string
}

SearchResult is the metadata of the search result.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL