elasticorm

package module
v0.0.0-...-dc3a43e Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2018 License: MIT Imports: 8 Imported by: 0

README

elasticorm - elastic object RESTful mapper

This is going to be a ORM like helper for elasticsearch in GO

I'm collecting the functions of elasticsearch I'm using the most in go. This is going to be a little toolbox to get you started faster with elasticsearch.

The API is not alpha and totally unstable.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidOption is returned when a not valid option is used in a elasticorm tag to configure the mapping of a struct
	ErrInvalidOption = errors.New(`Invalid elasticorm option is used`)

	// ErrInvalidType is returned when you try to save a struct with a datastore which has been initialized for another struct
	ErrInvalidType = errors.New(`Invalid type for this datastore`)

	// ErrInvalidResultType is returned when no pointer is passed to a find method
	ErrInvalidResultType = errors.New(`invalid result type`)

	// ErrInvalidIDField is returned when the defined ID field can't be set
	ErrInvalidIDField = errors.New(`invalid ID field`)

	// ErrNotFound is returned when no record could be found
	ErrNotFound = errors.New(`not found`)

	// ErrCreationFailed is returned by the Create method, when there was no error by the elastic client, but the record could not have been created - TODO when does this happen?
	ErrCreationFailed = errors.New(`creation of new elasticsearch record failed`)
)

Functions

This section is empty.

Types

type Analyzer

type Analyzer struct {
	Type       string   `json:"type"`
	Tokenizer  string   `json:"tokenizer"`
	CharFilter []string `json:"char_filter,omitempty"`
	Filter     []string `json:"filter,omitempty"`
}

type BoundingBox

type BoundingBox struct {
	Top    float64
	Left   float64
	Bottom float64
	Right  float64
}

func NewBoundingBox

func NewBoundingBox(top, right, bottom, left float64) BoundingBox

type Datastore

type Datastore struct {
	Ctx context.Context

	IndexDefinition IndexDefinition // in elasticsearch
	// contains filtered or unexported fields
}

Datstore is an instance to communicate easy with elasticsearch It is meant to be used for one struct and helps storing and retrieving it in/from elasticsearch It leverages the great elastic package from olivere

func NewDatastore

func NewDatastore(esc *elastic.Client, opts ...DatastoreOptFunc) (*Datastore, error)

NewDatastore returns a fresh instance of an elasticorm datastore

func NewDatastoreForURL

func NewDatastoreForURL(URL string, opts ...DatastoreOptFunc) (*Datastore, error)

func (*Datastore) CleanUp

func (ds *Datastore) CleanUp() error

func (*Datastore) CountFiltered

func (ds *Datastore) CountFiltered(filters map[string]interface{}) (uint32, error)

func (*Datastore) Create

func (ds *Datastore) Create(o interface{}, opts ...IndexOptFunc) error

func (*Datastore) DecodeElasticResponse

func (ds *Datastore) DecodeElasticResponse(source *json.RawMessage, ID string, o interface{}) error

func (*Datastore) DecodeElasticResponses

func (ds *Datastore) DecodeElasticResponses(qrs []QueryResult, results interface{}) error

func (*Datastore) DoSearch

func (ds *Datastore) DoSearch(query elastic.Query, results interface{}, opts ...QueryOptFunc) error

func (*Datastore) EnsureIndexDoesntExist

func (ds *Datastore) EnsureIndexDoesntExist() error

func (*Datastore) EnsureIndexExists

func (ds *Datastore) EnsureIndexExists() error

EnsureIndexExists checks wether the needed index for this datastore exists. It it doesn't it gets created the name of the datastore is determined by the structs name (+ plural s)

func (*Datastore) FilterByField

func (ds *Datastore) FilterByField(fieldName string, value interface{}) QueryOptFunc

func (*Datastore) Find

func (ds *Datastore) Find(ID string, result interface{}) error

func (*Datastore) FindAll

func (ds *Datastore) FindAll(results interface{}, opts ...QueryOptFunc) error

func (*Datastore) FindByGeoBoundingBox

func (ds *Datastore) FindByGeoBoundingBox(fieldName string, box BoundingBox, results interface{}, opts ...QueryOptFunc) error

func (*Datastore) FindByGeoDistance

func (ds *Datastore) FindByGeoDistance(fieldName string, lat float64, lon float64, distance string, results interface{}) error

func (*Datastore) FindByIDs

func (ds *Datastore) FindByIDs(IDs []string, result interface{}) error

func (*Datastore) FindFiltered

func (ds *Datastore) FindFiltered(results interface{}, mustFilters map[string]interface{}, opts ...QueryOptFunc) error

func (*Datastore) FindNestedFiltered

func (ds *Datastore) FindNestedFiltered(results interface{}, path string, mustFilters map[string]string, opts ...QueryOptFunc) error

func (*Datastore) FindNestedQuery

func (ds *Datastore) FindNestedQuery(results interface{}, path string, nested elastic.Query, opts ...QueryOptFunc) error

func (*Datastore) FindOneBy

func (ds *Datastore) FindOneBy(fieldName string, value interface{}, result interface{}, opts ...QueryOptFunc) error

func (*Datastore) FindQuery

func (ds *Datastore) FindQuery(results interface{}, q elastic.Query, opts ...QueryOptFunc) error

func (*Datastore) Limit

func (ds *Datastore) Limit(limit int) QueryOptFunc

func (*Datastore) Offset

func (ds *Datastore) Offset(offset int) QueryOptFunc

func (*Datastore) Refresh

func (ds *Datastore) Refresh() error

func (*Datastore) ScriptUpdate

func (ds *Datastore) ScriptUpdate(script string, params map[string]interface{}, filter map[string]string) error

func (*Datastore) SetSorting

func (ds *Datastore) SetSorting(fieldName string, order string) QueryOptFunc

func (*Datastore) Update

func (ds *Datastore) Update(o interface{}) error

func (*Datastore) UpdateByQueryService

func (ds *Datastore) UpdateByQueryService() *elastic.UpdateByQueryService

type DatastoreOptFunc

type DatastoreOptFunc func(*Datastore) error

DatastoreOptFunc is used as a parameter to NewDatastore and provides a way of configuration

func ForStruct

func ForStruct(i interface{}) DatastoreOptFunc

ForStruct generates a DatastoreOptFunc It is used to generate a default mapping, type name and index name by analysing a provided struct

func WithIDField

func WithIDField(name string) DatastoreOptFunc

type IndexAnalysis

type IndexAnalysis struct {
	Analyzer  map[string]Analyzer  `json:"analyzer,omitempty"`
	Tokenizer map[string]Tokenizer `json:"tokenizer,omitempty"`
}

type IndexDefinition

type IndexDefinition struct {
	Settings IndexSettings            `json:"settings,omitempty"`
	Mappings map[string]MappingConfig `json:"mappings,omitempty"`
}

IndexDefinition is a struct which marshals to a valid JSON configuration for creating a new elasticsearch index

func NewIndexDefinition

func NewIndexDefinition(options ...IndexDefinitionFunc) (IndexDefinition, error)

NewIndexDefinition returns a new IndexDefinition which is configurable via IndexDefinitionFuncs like SetNumberOfShards

func (*IndexDefinition) AddAnalyzer

func (d *IndexDefinition) AddAnalyzer(name string, a Analyzer) error

func (*IndexDefinition) AddTokenizer

func (d *IndexDefinition) AddTokenizer(name string, t Tokenizer) error

type IndexDefinitionFunc

type IndexDefinitionFunc func(*IndexDefinition) error

IndexDefinitionFunc is used as a parameter to set options on a new index definition (in NewIndexDefinition)

func AddMappingFromStruct

func AddMappingFromStruct(name string, i interface{}) IndexDefinitionFunc

AddMappingFromStruct is a IndexDefinitionFunc which can be passed to NewIndexDefinition and sets the mapping for the new index by analysing the passed in struct. The mapping should be provide the functionality to save and retrieve structs of the same type (as passed in). The mapping definition is configurable via tags. See MappingFromStruct

func SetNumberOfReplicas

func SetNumberOfReplicas(number int) IndexDefinitionFunc

SetNumberOfReplicas is a IndexDefinitionFunc which can be passed to NewIndexDefinition and sets the number_of_recplicas setting

func SetNumberOfShards

func SetNumberOfShards(number int) IndexDefinitionFunc

SetNumberOfShards is a IndexDefinitionFunc which can be passed to NewIndexDefinition and sets the number_of_shards setting

type IndexOptFunc

type IndexOptFunc func(*elastic.IndexService) error

IndexOptFunc accepts an elastic.IndexService to apply options on it

type IndexSettings

type IndexSettings struct {
	NumberOfShards   int            `json:"number_of_shards,omitempty"`
	NumberOfReplicas int            `json:"number_of_replicas,omitempty"`
	Analysis         *IndexAnalysis `json:"analysis,omitempty"`
}

type MappingConfig

type MappingConfig struct {
	Properties map[string]MappingFieldConfig `json:"properties,omitempty"`
}

MappingConfig is a struct which marshals to a valid elasticsearch mapping configuration

func MappingFromStruct

func MappingFromStruct(i interface{}) (MappingConfig, error)

MappingFromStruct returns the MappingConfig for a passed in struct (pointer). The mapping is configurable via json tags, which can change the name of the field, and elasticorm tags. The elasticorm tags can include

func (*MappingConfig) AddField

func (m *MappingConfig) AddField(name string, cfg MappingFieldConfig)

AddField adds a new field to the mapping

func (*MappingConfig) Analyzers

func (m *MappingConfig) Analyzers() []string

type MappingFieldConfig

type MappingFieldConfig struct {
	Type     string `json:"type"`
	Analyzer string `json:"analyzer,omitempty"`

	Properties map[string]MappingFieldConfig `json:"properties,omitempty"`
	Fields     map[string]MappingFieldConfig `json:"fields,omitempty"`
	Similarity string                        `json:"similarity,omitempty"`
	// contains filtered or unexported fields
}

MappingFieldConfig is a struct which represents the elasticsearch mapping configuration of one field. It is used in the MappingConfig.

type Query

type Query struct{}

func (Query) QueryContext

func (q Query) QueryContext() bool

QueryContext returns a boolean indicating wether the resulting query should be for query context (with a calculated score) - true - or for filtering context (hard matches) - false -

func (Query) String

func (q Query) String() string

type QueryOptFunc

type QueryOptFunc func(*elastic.SearchService) error

QueryOptFunc is used as a parameter

type QueryResult

type QueryResult interface {
	ID() string
	Source() *json.RawMessage
}

type TestSuite

type TestSuite struct {
	Existing  []interface{}
	Query     Query
	Expecting []interface{}
	// contains filtered or unexported fields
}

func NewTestSuite

func NewTestSuite(url string) *TestSuite

type Tokenizer

type Tokenizer struct {
	Type       string   `json:"type"`
	TokenChars []string `json:"token_chars,omitempty"`
	MinGram    int      `json:"min_gram,omitempty"`
	MaxGram    int      `json:"max_gram,omitempty"`
}

Jump to

Keyboard shortcuts

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