esutils

package
v0.9.8 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2022 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// SHOULD represents should query
	SHOULD queryLogic = iota + 1
	// MUST represents must query
	MUST
	// MUSTNOT represents must_not query
	MUSTNOT
)
View Source
const (
	// TERMS represents terms query
	TERMS queryType = iota + 1
	// MATCHPHRASE represents match_phrase query
	MATCHPHRASE
	// RANGE represents range query
	RANGE
	// PREFIX represents prefix query
	PREFIX
	// WILDCARD https://www.elastic.co/guide/en/elasticsearch/reference/6.8/query-dsl-wildcard-query.html
	WILDCARD
	// EXISTS https://www.elastic.co/guide/en/elasticsearch/reference/6.8/query-dsl-prefix-query.html
	EXISTS
)
View Source
const (
	// TEXT represents text field type
	TEXT esFieldType = "text"
	// KEYWORD represents keyword field type
	KEYWORD esFieldType = "keyword"
	// DATE represents date field type
	DATE esFieldType = "date"
	// LONG represents long field type
	LONG esFieldType = "long"
	// INTEGER represents integer field type
	INTEGER esFieldType = "integer"
	// SHORT represents short field type
	SHORT esFieldType = "short"
	// DOUBLE represents double field type
	DOUBLE esFieldType = "double"
	// FLOAT represents float field type
	FLOAT esFieldType = "float"
	// BOOL represents bool field type
	BOOL esFieldType = "boolean"
)

Variables

This section is empty.

Functions

func NewMapping

func NewMapping(mp MappingPayload) string

NewMapping return es mapping json string from mp

Types

type Base

type Base struct {
	Index string `json:"index"`
	Type  string `json:"type"`
}

Base defines es index and type

func (*Base) GetIndex

func (b *Base) GetIndex() string

GetIndex return index name

func (*Base) GetType

func (b *Base) GetType() string

GetType return es type name

func (*Base) SetType

func (b *Base) SetType(s string)

SetType sets es type

type Es

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

Es defines properties for connecting to an es instance

func NewEs

func NewEs(esIndex, esType string, opts ...EsOption) *Es

NewEs creates an Es instance

func (*Es) BulkDelete

func (es *Es) BulkDelete(ctx context.Context, ids []string) error

BulkDelete delete es docs specified by ids in bulk

func (*Es) BulkSaveOrUpdate

func (es *Es) BulkSaveOrUpdate(ctx context.Context, docs []interface{}) error

BulkSaveOrUpdate save or update docs in bulk

func (*Es) CheckTypeExists

func (es *Es) CheckTypeExists(ctx context.Context) (b bool, err error)

CheckTypeExists assert the es type exists

func (*Es) ClearIndex

func (es *Es) ClearIndex(ctx context.Context) error

ClearIndex remove all docs

func (*Es) Count

func (es *Es) Count(ctx context.Context, paging *Paging) (int64, error)

Count counts docs by paging

func (*Es) DeleteIndex

func (es *Es) DeleteIndex(ctx context.Context) error

DeleteIndex removes the index

func (*Es) GetByID

func (es *Es) GetByID(ctx context.Context, id string) (map[string]interface{}, error)

GetByID gets a doc by id

func (*Es) List

func (es *Es) List(ctx context.Context, paging *Paging, callback func(message json.RawMessage) (interface{}, error)) ([]interface{}, error)

List fetch docs by paging

func (*Es) NewIndex

func (es *Es) NewIndex(ctx context.Context, mapping string) (exists bool, err error)

NewIndex creates a new index

func (*Es) Page

func (es *Es) Page(ctx context.Context, paging *Paging) (PageResult, error)

Page fetch pagination result

func (*Es) PutMapping

func (es *Es) PutMapping(ctx context.Context, mp MappingPayload) error

PutMapping updates mapping

func (*Es) Random

func (es *Es) Random(ctx context.Context, paging *Paging) ([]map[string]interface{}, error)

Random if paging is nil, randomly return 10 pcs of documents as default

func (*Es) SaveOrUpdate

func (es *Es) SaveOrUpdate(ctx context.Context, doc interface{}) (string, error)

SaveOrUpdate saves or updates doc

func (*Es) SetIndex added in v0.3.6

func (e *Es) SetIndex(index string)

SetIndex sets index

func (*Es) SetType added in v0.3.6

func (e *Es) SetType(estype string)

SetType sets type

func (*Es) Stat

func (es *Es) Stat(ctx context.Context, paging *Paging, aggr interface{}) (map[string]interface{}, error)

Stat aggr only accept map[string]interface{} or elastic.Aggregation

type EsOption

type EsOption func(*Es)

EsOption represents functions for changing Es properties

func WithClient

func WithClient(client *elastic.Client) EsOption

WithClient sets client

func WithLogger

func WithLogger(logger *logrus.Logger) EsOption

WithLogger sets logger

func WithPassword

func WithPassword(password string) EsOption

WithPassword sets password

func WithUrls

func WithUrls(urls []string) EsOption

WithUrls set urls

func WithUsername

func WithUsername(username string) EsOption

WithUsername sets username

type Field

type Field struct {
	Name   string      `json:"name"`
	Type   esFieldType `json:"type"`
	Format string      `json:"format"`
}

Field defines a es field

type IBase

type IBase interface {
	GetIndex() string
	GetType() string
	SetType(s string)
}

IBase wraps functions for getting es index and type

type MappingPayload

type MappingPayload struct {
	Base
	Fields []Field `json:"fields"`
}

MappingPayload defines request payload for es index mapping

type PageResult

type PageResult struct {
	Page        int           `json:"page"` // from 1
	PageSize    int           `json:"page_size"`
	Total       int           `json:"total"`
	Docs        []interface{} `json:"docs"`
	HasNextPage bool          `json:"has_next_page"`
}

PageResult represents result of pagination

type Paging

type Paging struct {
	StartDate  string      `json:"startDate"`
	EndDate    string      `json:"endDate"`
	DateField  string      `json:"dateField"`
	QueryConds []QueryCond `json:"queryConds"`
	Skip       int         `json:"skip"`
	Limit      int         `json:"limit"`
	Sortby     []Sort      `json:"sortby"`
	// https://www.elastic.co/guide/en/elasticsearch/reference/6.8/search-request-source-filtering.html
	Includes []string `json:"includes"`
	Excludes []string `json:"excludes"`
}

Paging defines pagination query conditions

func (Paging) String added in v0.7.9

func (p Paging) String() string

String prints query in json format for debug purpose

type QueryCond

type QueryCond struct {
	Pair       map[string][]interface{} `json:"pair"`
	QueryLogic queryLogic               `json:"queryLogic"`
	QueryType  queryType                `json:"queryType"`
	Children   []QueryCond              `json:"children"`
}

QueryCond defines query conditions

type Sort

type Sort struct {
	Field     string `json:"field"`
	Ascending bool   `json:"ascending"`
}

Sort defines sort condition

Jump to

Keyboard shortcuts

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