Documentation
¶
Overview ¶
Package elasticsearch wraps the Elasticsearch client with helpers for building index mappings and settings and for querying.
Index ¶
Constants ¶
View Source
const ( EsTypeKeyword = "keyword" EsTypeText = "text" EsTypeDate = "date" EsTypeBool = "boolean" EsTypeLong = "long" EsTypeInteger = "integer" EsTypeSearchAsYouType = "search_as_you_type" EsTypeFlattened = "flattened" EsTypeNested = "nested" )
View Source
const ( EsSortRequestMissingFirst = "_first" EsSortRequestMissingLast = "_last" )
Variables ¶
View Source
var ( ErrCodeEsNewClient = "ES-001" ErrCodeEsIdxExists = "ES-002" ErrCodeEsIdx = "ES-003" ErrCodeEsIdxAsync = "ES-004" ErrCodeEsIdxCreate = "ES-007" ErrCodeEsBulkIdx = "ES-008" ErrCodeEsExists = "ES-009" ErrCodeEsInvalidModel = "ES-011" ErrCodeEsInvalidModelType = "ES-012" ErrCodeEsGetMapping = "ES-013" ErrCodeEsNoMappingFound = "ES-014" ErrCodeEsMappingSchemaNotExpected = "ES-015" ErrCodeEsMappingExistentFieldsModified = "ES-016" ErrCodeEsPutMapping = "ES-017" ErrCodeEsDel = "ES-018" ErrCodeEsIndexBuilderAliasAndIndexEmpty = "ES-019" ErrCodeEsIndexBuilderModelEmpty = "ES-020" ErrCodeEsGetIndexesByAlias = "ES-021" ErrCodeEsNoIndicesForAlias = "ES-022" ErrCodeEsNoWriteIndexForAlias = "ES-023" ErrCodeEsRefresh = "ES-024" ErrCodeEsBasicAuthInvalid = "ES-025" ErrCodeEsBulkDel = "ES-026" ErrCodeEsSortRequestFieldEmpty = "ES-028" ErrCodeEsDeleteIdx = "ES-029" )
View Source
var ( ErrEsNewClient = func(cause error) error { return jet.NewAppErrBuilder(ErrCodeEsNewClient, "es: new cent").Wrap(cause).Err() } ErrEsIdxExists = func(ctx context.Context, cause error, index string) error { return jet.NewAppErrBuilder(ErrCodeEsIdxExists, "es: index exists (%s)", index).C(ctx).Wrap(cause).Err() } ErrEsGetMapping = func(ctx context.Context, cause error, index string) error { return jet.NewAppErrBuilder(ErrCodeEsGetMapping, "es: get mapping (%s)", index).C(ctx).Wrap(cause).Err() } ErrEsGetIndexesByAlias = func(ctx context.Context, cause error, alias string) error { return jet.NewAppErrBuilder(ErrCodeEsGetIndexesByAlias, "es: get index by alias (%s)", alias).C(ctx).Wrap(cause).Err() } ErrEsMappingSchemaNotExpected = func(ctx context.Context, cause error, index string) error { return jet.NewAppErrBuilder(ErrCodeEsMappingSchemaNotExpected, "es: mapping schema not expected %s", index).C(ctx).F(jet.KV{"idx": index}).Err() } ErrEsPutMapping = func(ctx context.Context, cause error, index string) error { return jet.NewAppErrBuilder(ErrCodeEsPutMapping, "es: mapping").C(ctx).Wrap(cause).F(jet.KV{"idx": index}).Err() } ErrEsIdx = func(ctx context.Context, cause error, index, id string) error { return jet.NewAppErrBuilder(ErrCodeEsIdx, "es: index").C(ctx).Wrap(cause).F(jet.KV{"idx": index, "id": id}).Err() } ErrEsDel = func(ctx context.Context, cause error, index, id string) error { return jet.NewAppErrBuilder(ErrCodeEsDel, "es: delete").C(ctx).Wrap(cause).F(jet.KV{"idx": index, "id": id}).Err() } ErrEsRefresh = func(ctx context.Context, cause error, index string) error { return jet.NewAppErrBuilder(ErrCodeEsRefresh, "es: refresh").C(ctx).Wrap(cause).F(jet.KV{"idx": index}).Err() } ErrEsBulkIdx = func(ctx context.Context, cause error, index string) error { return jet.NewAppErrBuilder(ErrCodeEsBulkIdx, "es: bulk index").C(ctx).Wrap(cause).F(jet.KV{"idx": index}).Err() } ErrEsDeleteIdx = func(ctx context.Context, cause error, index string) error { return jet.NewAppErrBuilder(ErrCodeEsDeleteIdx, "es: delete index").C(ctx).Wrap(cause).F(jet.KV{"idx": index}).Err() } ErrEsBulkDel = func(ctx context.Context, cause error, index string) error { return jet.NewAppErrBuilder(ErrCodeEsBulkDel, "e: bulk delete").C(ctx).Wrap(cause).F(jet.KV{"idx": index}).Err() } ErrEsIdxCreate = func(ctx context.Context, cause error, index string) error { return jet.NewAppErrBuilder(ErrCodeEsIdxCreate, "es: create").C(ctx).Wrap(cause).F(jet.KV{"idx": index}).Err() } ErrEsExists = func(ctx context.Context, cause error, index, id string) error { return jet.NewAppErrBuilder(ErrCodeEsExists, "es: exsits").C(ctx).Wrap(cause).F(jet.KV{"idx": index, "id": id}).Err() } ErrEsNoMappingFound = func(ctx context.Context, index string) error { return jet.NewAppErrBuilder(ErrCodeEsNoMappingFound, "no mapping found").C(ctx).F(jet.KV{"idx": index}).Err() } ErrEsMappingExistentFieldsModified = func(ctx context.Context, index string, fields []string) error { return jet.NewAppErrBuilder(ErrCodeEsMappingExistentFieldsModified, "ES doesn't allow changing mapping for existent fields.").C(ctx).F(jet.KV{"idx": index, "fields": fields}).Err() } ErrEsInvalidModel = func(ctx context.Context) error { return jet.NewAppErrBuilder(ErrCodeEsInvalidModel, "invalid model, check tags").C(ctx).Err() } ErrEsInvalidModelType = func(ctx context.Context) error { return jet.NewAppErrBuilder(ErrCodeEsInvalidModelType, "model must be pointer of struct").C(ctx).Err() } ErrEsIndexBuilderAliasAndIndexEmpty = func(ctx context.Context) error { return jet.NewAppErrBuilder(ErrCodeEsIndexBuilderAliasAndIndexEmpty, "neither alias name nor index name specified").C(ctx).Err() } ErrEsIndexBuilderModelEmpty = func(ctx context.Context) error { return jet.NewAppErrBuilder(ErrCodeEsIndexBuilderModelEmpty, "model not specified").C(ctx).Err() } ErrEsNoIndicesForAlias = func(ctx context.Context, alias string) error { return jet.NewAppErrBuilder(ErrCodeEsNoIndicesForAlias, "model not specified").F(jet.KV{"alias": alias}).C(ctx).Err() } ErrEsNoWriteIndexForAlias = func(ctx context.Context, alias string) error { return jet.NewAppErrBuilder(ErrCodeEsNoWriteIndexForAlias, "no write index").F(jet.KV{"alias": alias}).C(ctx).Err() } ErrEsBasicAuthInvalid = func() error { return jet.NewAppErrBuilder(ErrCodeEsBasicAuthInvalid, "basic auth invalid").Err() } ErrEsSortRequestFieldEmpty = func(ctx context.Context) error { return jet.NewAppErrBuilder(ErrCodeEsSortRequestFieldEmpty, "sort request field parameter empty").C(ctx).Err() } )
Functions ¶
func ToSortRequestEs ¶
Types ¶
type Config ¶
type Config struct {
Url string // Url - ES url
Trace bool // Trace enables tracing mode
Sniff bool // Sniff - read https://github.com/olivere/elastic/issues/387
Shards int // Shards - how many shards to be created for index
Replicas int // Replicas - how many replicas to eb created for index
Username string // Username - ES basic auth (if not set, no auth applied)
Password string // Password - ES basic auth
Refresh bool // Refresh - enforces refresh after each change. It helpful for tests but MUST NOT BE USED ON PROD
}
Config - model of ES configuration
type EsMapping ¶
type EsMapping struct {
Settings EsSettings `json:"settings"`
Mappings struct {
Properties EsProperties `json:"properties"`
} `json:"mappings"`
}
type EsProperties ¶
type EsProperties map[string]*EsProperty
type EsProperty ¶
type EsSettings ¶
type IndexBuilder ¶
type IndexBuilder interface {
// WithAlias specifies an index with alias
WithAlias(name string) IndexBuilder
// WithIndex specifies an index name
// if alias specified with WithAlias call, you don't need specify index name explicitly
WithIndex(name string) IndexBuilder
// WithMappingModel specifies index mapping based on model provided
// if index doesn't exist, a new index is created
// if index exists it checks whether existent mapping is modified and if it is, it fails. If only new fields added, it handles them as PUT
// Note, "json" tag must be specified together with "es" tag
// example:
// type IndexModel struct {
// Field1 string `json:"field1" es:"type:text"` // field is mapped with text type
// Field2 string `json:"field2" es:"type:keyword"` // field is mapped with keyword type
// Field3 time.Time `json:"field3" es:"type:date"` // field is mapped with date type
// Field4 time.Time `json:"field4" es:"-"` // field is mapped with "Index=false"
// }
//
// model must be pointer type
WithMappingModel(model interface{}) IndexBuilder
// WithExplicitMapping specifies index mapping explicitly as a serialized json mapping object
// see ES doc about https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html
// it checks if index exists, if not creates it
WithExplicitMapping(mapping string) IndexBuilder
// Build builds a new alias/index or modifies mapping of an existent index
Build(ctx context.Context) error
}
IndexBuilder allows creating / modification a ES index
type Search ¶
type Search interface {
// Index indexes a document
Index(ctx context.Context, index string, id string, data interface{}) error
// IndexAsync indexes a document async
IndexAsync(ctx context.Context, index string, id string, data interface{})
// IndexBulk allows indexing bulk of documents in one hit
IndexBulk(ctx context.Context, index string, docs map[string]interface{}) error
// IndexBulkAsync allows indexing bulk of documents in one hit
IndexBulkAsync(ctx context.Context, index string, docs map[string]interface{})
// GetClient provides an access to ES client
GetClient() *elastic.Client
// Close closes client
Close(ctx context.Context)
//Ping pings server
Ping() bool
// Exists checks if a document exists in the index
Exists(ctx context.Context, index, id string) (bool, error)
// Delete deletes a document
Delete(ctx context.Context, index string, id string) error
// DeleteBulk deletes bulk of documents
DeleteBulk(ctx context.Context, index string, ids []string) error
// NewBuilder creates a new builder object
NewBuilder() IndexBuilder
// Refresh refreshes data in index (don't use in production code)
Refresh(ctx context.Context, index string) error
// DeleteIndex removes index
DeleteIndex(ctx context.Context, index string) error
}
Search allows indexing and searching with ES
Click to show internal directories.
Click to hide internal directories.