elasticsearch

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2024 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrNoTimestampRange = "no timestamp range found"
	MaxSize             = 10000
	SearchPage          = "_search"
)
View Source
const ErrFailedQuery = "elasticsearch query failed"
View Source
const (
	MaxEncodedDetailsLength = 16 * 1024
)

Variables

This section is empty.

Functions

func WantsField

func WantsField(desired, flag Fields) bool

WantsField takes the output of Query.DesiredFields() and sees if the given field from amongst our Fields* flags is one of the desired fields.

If desired is 0 (the Query had no Source items set), this always returns true.

Types

type Aggregations

type Aggregations struct {
	Stats *Buckets `json:"stats,omitempty"`
}

func (Aggregations) MarshalEasyJSON

func (v Aggregations) MarshalEasyJSON(w *jwriter.Writer)

MarshalEasyJSON supports easyjson.Marshaler interface

func (*Aggregations) UnmarshalEasyJSON

func (v *Aggregations) UnmarshalEasyJSON(l *jlexer.Lexer)

UnmarshalEasyJSON supports easyjson.Unmarshaler interface

func (*Aggregations) UnmarshalJSON

func (v *Aggregations) UnmarshalJSON(data []byte) error

UnmarshalJSON supports json.Unmarshaler interface

type Aggs

type Aggs struct {
	Stats interface{} `json:"stats"`
}

Aggs is used to specify an aggregation query.

type AggsField

type AggsField struct {
	Sum            *Field          `json:"sum,omitempty"`
	ScriptedMetric *ScriptedMetric `json:"scripted_metric,omitempty"`
}

type AggsStats

type AggsStats struct {
	MultiTerms *MultiTerms          `json:"multi_terms,omitempty"`
	Terms      *Field               `json:"terms,omitempty"`
	Aggs       map[string]AggsField `json:"aggs,omitempty"`
}

type Buckets

type Buckets struct {
	Buckets []interface{} `json:"buckets,omitempty"`
}

func (Buckets) MarshalEasyJSON

func (v Buckets) MarshalEasyJSON(w *jwriter.Writer)

MarshalEasyJSON supports easyjson.Marshaler interface

func (*Buckets) UnmarshalEasyJSON

func (v *Buckets) UnmarshalEasyJSON(l *jlexer.Lexer)

UnmarshalEasyJSON supports easyjson.Unmarshaler interface

func (*Buckets) UnmarshalJSON

func (v *Buckets) UnmarshalJSON(data []byte) error

UnmarshalJSON supports json.Unmarshaler interface

type Client

type Client struct {
	Error error
	// contains filtered or unexported fields
}

Client is used to interact with an Elastic Search server.

func NewClient

func NewClient(config Config) (*Client, error)

NewClient returns a Client that can talk to the configured Elastic Search server and will use the configured index for queries.

func (*Client) Info

func (c *Client) Info() (*ElasticInfo, error)

Info tells you the version number info of the server.

func (*Client) Scroll

func (c *Client) Scroll(query *Query, cb HitsCallBack) (*Result, error)

Scroll uses our index and the given query to get back your desired search results. It auto-scrolls and returns all your hits via the given callback, and everything else in the returned Result.

func (*Client) Search

func (c *Client) Search(query *Query) (*Result, error)

Search uses our index and the given query to get back your desired search results. If there are more than 10,000 hits, you won't get them (use Scroll instead).

type Config

type Config struct {
	Host     string
	Username string
	Password string
	Scheme   string
	Port     int
	Index    string
	// contains filtered or unexported fields
}

Config allows you to specify your Elastic Search server details. Currently only basic auth is supported, for an internal network server with "public" access.

type Details

type Details struct {
	ID                string  `json:"_id"`
	AccountingName    string  `json:"ACCOUNTING_NAME"`
	AvailCPUTimeSec   int64   `json:"AVAIL_CPU_TIME_SEC"`
	BOM               string  `json:"BOM"`
	Command           string  `json:"Command"`
	JobName           string  `json:"JOB_NAME"`
	Job               string  `json:"Job"`
	MemRequestedMB    int64   `json:"MEM_REQUESTED_MB"`
	MemRequestedMBSec int64   `json:"MEM_REQUESTED_MB_SEC"`
	NumExecProcs      int64   `json:"NUM_EXEC_PROCS"`
	PendingTimeSec    int64   `json:"PENDING_TIME_SEC"`
	QueueName         string  `json:"QUEUE_NAME"`
	RunTimeSec        int64   `json:"RUN_TIME_SEC"`
	Timestamp         int64   `json:"timestamp"`
	UserName          string  `json:"USER_NAME"`
	WastedCPUSeconds  float64 `json:"WASTED_CPU_SECONDS"`
	WastedMBSeconds   float64 `json:"WASTED_MB_SECONDS"`
}

Details holds the document information of a Hit.

func DeserializeDetails

func DeserializeDetails(encoded []byte, desired Fields) (*Details, error)

DeserializeDetails takes the output of Details.Serialize and converts it back in to a Details. Provide a non-zero Fields (from Query.DesiredFields()) to skip the unmarshalling of undesired fields, for a speed boost.

func (Details) MarshalEasyJSON

func (v Details) MarshalEasyJSON(w *jwriter.Writer, desired Fields)

MarshalEasyJSON supports easyjson.Marshaler interface

func (*Details) Serialize

func (d *Details) Serialize() ([]byte, error)

Serialize converts a Details to a byte slice representation suitable for storing on disk.

func (*Details) UnmarshalEasyJSON

func (v *Details) UnmarshalEasyJSON(l *jlexer.Lexer)

UnmarshalEasyJSON supports easyjson.Unmarshaler interface

func (*Details) UnmarshalJSON

func (v *Details) UnmarshalJSON(data []byte) error

UnmarshalJSON supports json.Unmarshaler interface

type ElasticInfo

type ElasticInfo struct {
	Version struct {
		Number string
	}
}

ElasticInfo is the type returned by an Info() request. It just tells you the version number of the server.

type Error

type Error struct {
	Msg string
	// contains filtered or unexported fields
}

Error is an error type that has a Msg with one of our const Err* messages.

func (Error) Error

func (e Error) Error() string

Error returns a string representation of the error.

type Field

type Field struct {
	Field string `json:"field"`
	Size  int    `json:"size,omitempty"`
}

type Fields

type Fields uint16
const (
	FieldAccountingName Fields = 1 << iota
	FieldAvailCPUTimeSec
	FieldBOM
	FieldCommand
	FieldJobName
	FieldJob
	FieldMemRequestedMB
	FieldMemRequestedMBSec
	FieldNumExecProcs
	FieldPendingTimeSec
	FieldQueueName
	FieldRunTimeSec
	FieldTimestamp
	FieldUserName
	FieldWastedCPUSeconds
	FieldWastedMBSeconds
)

type Filter

type Filter []map[string]MapStringStringOrMap

type Hit

type Hit struct {
	ID      string   `json:"_id,omitempty"`
	Details *Details `json:"_source"`
}

func (Hit) MarshalEasyJSON

func (v Hit) MarshalEasyJSON(w *jwriter.Writer, desired Fields)

MarshalEasyJSON supports easyjson.Marshaler interface

func (*Hit) UnmarshalEasyJSON

func (v *Hit) UnmarshalEasyJSON(l *jlexer.Lexer)

UnmarshalEasyJSON supports easyjson.Unmarshaler interface

func (*Hit) UnmarshalJSON

func (v *Hit) UnmarshalJSON(data []byte) error

UnmarshalJSON supports json.Unmarshaler interface

type HitSet

type HitSet struct {
	Total HitSetTotal `json:"total"`
	Hits  []Hit       `json:"hits"`
}

HitSet is the container of all Hits, plus a Total.Value which may tell you the total number of matching documents.

func (*HitSet) FromJSON

func (v *HitSet) FromJSON(l *jlexer.Lexer, cb HitsCallBack) int

FromJSON is like UnmarshalJSON, but instead of storing all hits on the Result which might need too much memory, it passes each Hit to the given callback and only updates the Total count, but leaves Hits empty.

func (*HitSet) MarshalFields

func (v *HitSet) MarshalFields(w *jwriter.Writer, desired Fields)

MarshalFields converts to JSON, but the JSON will only include the given fields of the hit details, even if they're zero value. If the desired map is empty, all fields are included.

func (*HitSet) UnmarshalEasyJSON

func (v *HitSet) UnmarshalEasyJSON(l *jlexer.Lexer)

UnmarshalEasyJSON supports easyjson.Unmarshaler interface

func (*HitSet) UnmarshalJSON

func (v *HitSet) UnmarshalJSON(data []byte) error

UnmarshalJSON supports json.Unmarshaler interface

type HitSetTotal

type HitSetTotal struct {
	Value int `json:"value"`
}

func (HitSetTotal) MarshalEasyJSON

func (v HitSetTotal) MarshalEasyJSON(w *jwriter.Writer)

MarshalEasyJSON supports easyjson.Marshaler interface

func (*HitSetTotal) UnmarshalEasyJSON

func (v *HitSetTotal) UnmarshalEasyJSON(l *jlexer.Lexer)

UnmarshalEasyJSON supports easyjson.Unmarshaler interface

func (*HitSetTotal) UnmarshalJSON

func (v *HitSetTotal) UnmarshalJSON(data []byte) error

UnmarshalJSON supports json.Unmarshaler interface

type HitsCallBack

type HitsCallBack func(*Hit)

type MapStringStringOrMap

type MapStringStringOrMap map[string]interface{}

func (MapStringStringOrMap) GetMapString

func (m MapStringStringOrMap) GetMapString(key, subKey string) string

GetMapString returns the string value of the given subKey of the map with the given key in this map. Returns blank string if the keys didn't exist, or the value wasn't a string.

type Mock

type Mock struct {
	*Client
	// contains filtered or unexported fields
}

Mock helps you test elasticsearch queries without a real elasticsearch server.

func NewMock

func NewMock(index string) *Mock

NewMock returns a Mock that you can get a mock Client and queries from.

func (*Mock) AggQuery

func (m *Mock) AggQuery() *http.Request

AggQuery returns a http.Request that is requesting an aggregation search.

func (*Mock) Done

func (m *Mock) Done(int) bool

func (*Mock) ScrollQuery

func (m *Mock) ScrollQuery(args string) (*http.Request, int)

ScrollQuery returns a http.Request that is requesting a scroll search. Any args are appended to the search url. Also returns the number of matching documents (which will match the number of hits you get back if your args include the scroll parameter).

func (*Mock) Usernames

func (m *Mock) Usernames(query *Query) ([]string, error)

type MultiTerms

type MultiTerms struct {
	Terms []Field `json:"terms"`
	Size  int     `json:"size"`
}

type QFBool

type QFBool struct {
	Filter Filter `json:"filter"`
}

type Query

type Query struct {
	Size           int          `json:"size"`
	Aggs           *Aggs        `json:"aggs,omitempty"`
	Query          *QueryFilter `json:"query,omitempty"`
	Sort           []string     `json:"sort,omitempty"`
	Source         []string     `json:"_source,omitempty"`
	ScrollParamSet bool         `json:"_scroll,omitempty"`
}

Query describes the search query you wish to run against Elastic Search.

func NewQuery

func NewQuery(req *http.Request) (*Query, bool)

NewQuery looks at the given Request method, path, body and parameters to see if it's a search request, and converts it to a Query if so. The booleon will be false if not.

func (*Query) DateRange

func (q *Query) DateRange() (lt, lte, gte time.Time, err error)

DateRange looks at the query's range->timestamp and returns the lt, lte and gte values. Returns an error if none were found.

func (*Query) DesiredFields

func (q *Query) DesiredFields() Fields

DesiredFields returns a Fields bitmask value with all our Source values set. Call eg. WantsField(value, FieldAccountingName) to see if the returned value and thus this Query had a Source entry "ACCOUNTING_NAME".

If no Source values are set, this returns a 0 value which will be treated by WantsField() as wanting all fields.

func (*Query) Filters

func (q *Query) Filters() map[string]string

Filters returns a combination of MatchFilters() and PrefixFilters().

func (*Query) IsScroll

func (q *Query) IsScroll() bool

IsScroll returns true if the http.Request this Query was made from had a scroll parameter.

func (*Query) Key

func (q *Query) Key() string

Key returns a string that is unique to this Query.

func (*Query) MatchFilters added in v1.1.0

func (q *Query) MatchFilters() map[string]string

MatchFilters returns the match_phrase key value pairs found in the query's filter. Returns an empty map if none found.

func (*Query) PrefixFilters added in v1.1.0

func (q *Query) PrefixFilters() map[string]string

PrefixFilters returns the prefix key value pairs found in the query's filter. Returns an empty map if none found.

type QueryFilter

type QueryFilter struct {
	Bool QFBool `json:"bool"`
}

QueryFilter is used to filter the documents you're interested in.

type Result

type Result struct {
	ScrollID     string        `json:"_scroll_id,omitempty"`
	Took         int           `json:"took"`
	TimedOut     bool          `json:"timed_out"`
	HitSet       *HitSet       `json:"hits"`
	Aggregations *Aggregations `json:"aggregations,omitempty"`
	PoolKey      int           `json:"-"`
}

Result holds the results of a search query.

func NewResult

func NewResult() *Result

NewResult returns a Result with an empty HitSet in it, suitable for adding hits and errors to.

func (*Result) FromJSON

func (v *Result) FromJSON(data []byte, cb HitsCallBack) (int, error)

FromJSON is like UnmarshalJSON, but instead of storing all hits on the Result which might need too much memory, it passes each Hit to the given callback and only updates the Total count, but leaves Hits empty. Returns the number of Hits passed to the callback.

func (*Result) MarshalFields

func (v *Result) MarshalFields(desired Fields) ([]byte, error)

MarshalFields converts to JSON, but the JSON will only include the given fields of the hit details, even if they're zero value. If the desired map is empty, all fields are included.

func (*Result) UnmarshalEasyJSON

func (v *Result) UnmarshalEasyJSON(l *jlexer.Lexer)

UnmarshalEasyJSON supports easyjson.Unmarshaler interface

func (*Result) UnmarshalJSON

func (v *Result) UnmarshalJSON(data []byte) error

UnmarshalJSON supports json.Unmarshaler interface

type ScriptedMetric

type ScriptedMetric struct {
	InitScript    string      `json:"init_script"`
	MapScript     string      `json:"map_script"`
	CombineScript string      `json:"combine_script"`
	ReduceScript  string      `json:"reduce_script"`
	Params        interface{} `json:"params"`
}

Jump to

Keyboard shortcuts

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