Documentation
¶
Overview ¶
Package goes provides an API to access Elasticsearch.
Index ¶
- Constants
- type Aggregation
- type All
- type Bucket
- type Connection
- func (c *Connection) AddAlias(alias string, indexes []string) (*Response, error)
- func (c *Connection) AliasExists(alias string) (bool, error)
- func (c *Connection) BulkSend(documents []Document) (*Response, error)
- func (c *Connection) Count(query interface{}, indexList []string, typeList []string, extraArgs url.Values) (*Response, error)
- func (c *Connection) CreateIndex(name string, mapping interface{}) (*Response, error)
- func (c *Connection) Delete(d Document, extraArgs url.Values) (*Response, error)
- func (c *Connection) DeleteIndex(name string) (*Response, error)
- func (c *Connection) DeleteMapping(typeName string, indexes []string) (*Response, error)
- func (c *Connection) Get(index string, documentType string, id string, extraArgs url.Values) (*Response, error)
- func (c *Connection) GetMapping(types []string, indexes []string) (*Response, error)
- func (c *Connection) Index(d Document, extraArgs url.Values) (*Response, error)
- func (c *Connection) IndexStatus(indexList []string) (*Response, error)
- func (c *Connection) IndicesExist(indexes []string) (bool, error)
- func (c *Connection) Optimize(indexList []string, extraArgs url.Values) (*Response, error)
- func (c *Connection) PutMapping(typeName string, mapping interface{}, indexes []string) (*Response, error)
- func (c *Connection) Query(query interface{}, indexList []string, typeList []string, httpMethod string, ...) (*Response, error)
- func (c *Connection) RefreshIndex(name string) (*Response, error)
- func (c *Connection) RemoveAlias(alias string, indexes []string) (*Response, error)
- func (c *Connection) Scan(query interface{}, indexList []string, typeList []string, timeout string, ...) (*Response, error)
- func (c *Connection) Scroll(scrollId string, timeout string) (*Response, error)
- func (c *Connection) Search(query interface{}, indexList []string, typeList []string, extraArgs url.Values) (*Response, error)
- func (c *Connection) Stats(indexList []string, extraArgs url.Values) (*Response, error)
- func (c *Connection) Update(d Document, query interface{}, extraArgs url.Values) (*Response, error)
- func (c *Connection) UpdateIndexSettings(name string, settings interface{}) (*Response, error)
- func (c *Connection) WithClient(cl *http.Client) *Connection
- type Document
- type Hit
- type Hits
- type IndexStatus
- type Item
- type Request
- type Response
- type SearchError
- type Shard
- type StatIndex
- type StatPrimary
Examples ¶
Constants ¶
const ( BULK_COMMAND_INDEX = "index" BULK_COMMAND_DELETE = "delete" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Aggregation ¶
type Aggregation map[string]interface{}
Represents an aggregation from response
func (Aggregation) Buckets ¶
func (a Aggregation) Buckets() []Bucket
Buckets returns list of buckets in aggregation
type All ¶
type All struct {
Indices map[string]StatIndex `json:"indices"`
Primaries map[string]StatPrimary `json:"primaries"`
}
Represents the "_all" field when calling the _stats API This is minimal but this is what I only need
type Bucket ¶
type Bucket map[string]interface{}
Represents a bucket for aggregation
func (Bucket) Aggregation ¶
func (b Bucket) Aggregation(name string) Aggregation
Aggregation returns aggregation by name from bucket
type Connection ¶
type Connection struct {
// The host to connect to
Host string
// The port to use
Port string
// Client is the http client used to make requests, allowing settings things
// such as timeouts etc
Client *http.Client
}
Represents a Connection object to elasticsearch
func NewConnection ¶
func NewConnection(host string, port string) *Connection
NewConnection initiates a new Connection to an elasticsearch server
This function is pretty useless for now but might be useful in a near future if wee need more features like connection pooling or load balancing.
func (*Connection) AddAlias ¶
func (c *Connection) AddAlias(alias string, indexes []string) (*Response, error)
AddAlias creates an alias to one or more indexes
func (*Connection) AliasExists ¶
func (c *Connection) AliasExists(alias string) (bool, error)
AliasExists checks whether alias is defined on the server
func (*Connection) BulkSend ¶
func (c *Connection) BulkSend(documents []Document) (*Response, error)
Bulk adds multiple documents in bulk mode
func (*Connection) Count ¶
func (c *Connection) Count(query interface{}, indexList []string, typeList []string, extraArgs url.Values) (*Response, error)
Count executes a count query against an index, use the Count field in the response for the result
func (*Connection) CreateIndex ¶
func (c *Connection) CreateIndex(name string, mapping interface{}) (*Response, error)
CreateIndex creates a new index represented by a name and a mapping
Example ¶
package main
import (
"fmt"
"os"
"github.com/belogik/goes"
)
var (
ES_HOST = "localhost"
ES_PORT = "9200"
)
func getConnection() (conn *goes.Connection) {
h := os.Getenv("TEST_ELASTICSEARCH_HOST")
if h == "" {
h = ES_HOST
}
p := os.Getenv("TEST_ELASTICSEARCH_PORT")
if p == "" {
p = ES_PORT
}
conn = goes.NewConnection(h, p)
return
}
func main() {
conn := getConnection()
mapping := map[string]interface{}{
"settings": map[string]interface{}{
"index.number_of_shards": 1,
"index.number_of_replicas": 0,
},
"mappings": map[string]interface{}{
"_default_": map[string]interface{}{
"_source": map[string]interface{}{
"enabled": true,
},
"_all": map[string]interface{}{
"enabled": false,
},
},
},
}
resp, err := conn.CreateIndex("test", mapping)
if err != nil {
panic(err)
}
fmt.Printf("%s", resp)
}
func (*Connection) Delete ¶
Delete deletes a Document d The extraArgs is a list of url.Values that you can send to elasticsearch as URL arguments, for example, to control routing.
Example ¶
package main
import (
"fmt"
"net/url"
"os"
"github.com/belogik/goes"
)
var (
ES_HOST = "localhost"
ES_PORT = "9200"
)
func getConnection() (conn *goes.Connection) {
h := os.Getenv("TEST_ELASTICSEARCH_HOST")
if h == "" {
h = ES_HOST
}
p := os.Getenv("TEST_ELASTICSEARCH_PORT")
if p == "" {
p = ES_PORT
}
conn = goes.NewConnection(h, p)
return
}
func main() {
conn := getConnection()
//[create index, index document ...]
d := goes.Document{
Index: "twitter",
Type: "tweet",
Id: "1",
Fields: map[string]interface{}{
"user": "foo",
},
}
response, err := conn.Delete(d, url.Values{})
if err != nil {
panic(err)
}
fmt.Printf("%s", response)
}
func (*Connection) DeleteIndex ¶
func (c *Connection) DeleteIndex(name string) (*Response, error)
DeleteIndex deletes an index represented by a name
Example ¶
package main
import (
"fmt"
"os"
"github.com/belogik/goes"
)
var (
ES_HOST = "localhost"
ES_PORT = "9200"
)
func getConnection() (conn *goes.Connection) {
h := os.Getenv("TEST_ELASTICSEARCH_HOST")
if h == "" {
h = ES_HOST
}
p := os.Getenv("TEST_ELASTICSEARCH_PORT")
if p == "" {
p = ES_PORT
}
conn = goes.NewConnection(h, p)
return
}
func main() {
conn := getConnection()
resp, err := conn.DeleteIndex("yourinde")
if err != nil {
panic(err)
}
fmt.Printf("%s", resp)
}
func (*Connection) DeleteMapping ¶
func (c *Connection) DeleteMapping(typeName string, indexes []string) (*Response, error)
DeleteMapping deletes a mapping along with all data in the type
func (*Connection) Get ¶
func (c *Connection) Get(index string, documentType string, id string, extraArgs url.Values) (*Response, error)
Get a typed document by its id
func (*Connection) GetMapping ¶
func (c *Connection) GetMapping(types []string, indexes []string) (*Response, error)
func (*Connection) Index ¶
Index indexes a Document The extraArgs is a list of url.Values that you can send to elasticsearch as URL arguments, for example, to control routing, ttl, version, op_type, etc.
Example ¶
package main
import (
"fmt"
"net/url"
"os"
"github.com/belogik/goes"
)
var (
ES_HOST = "localhost"
ES_PORT = "9200"
)
func getConnection() (conn *goes.Connection) {
h := os.Getenv("TEST_ELASTICSEARCH_HOST")
if h == "" {
h = ES_HOST
}
p := os.Getenv("TEST_ELASTICSEARCH_PORT")
if p == "" {
p = ES_PORT
}
conn = goes.NewConnection(h, p)
return
}
func main() {
conn := getConnection()
d := goes.Document{
Index: "twitter",
Type: "tweet",
Fields: map[string]interface{}{
"user": "foo",
"message": "bar",
},
}
extraArgs := make(url.Values, 1)
extraArgs.Set("ttl", "86400000")
response, err := conn.Index(d, extraArgs)
if err != nil {
panic(err)
}
fmt.Printf("%s", response)
}
func (*Connection) IndexStatus ¶
func (c *Connection) IndexStatus(indexList []string) (*Response, error)
IndexStatus fetches the status (_status) for the indices defined in indexList. Use _all in indexList to get stats for all indices
func (*Connection) IndicesExist ¶
func (c *Connection) IndicesExist(indexes []string) (bool, error)
IndicesExist checks whether index (or indices) exist on the server
func (*Connection) Optimize ¶
Optimize an index represented by a name, extra args are also allowed please check: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-optimize.html#indices-optimize
func (*Connection) PutMapping ¶
func (c *Connection) PutMapping(typeName string, mapping interface{}, indexes []string) (*Response, error)
PutMapping registers a specific mapping for one or more types in one or more indexes
func (*Connection) Query ¶
func (c *Connection) Query(query interface{}, indexList []string, typeList []string, httpMethod string, extraArgs url.Values) (*Response, error)
Query runs a query against an index using the provided http method. This method can be used to execute a delete by query, just pass in "DELETE" for the HTTP method.
func (*Connection) RefreshIndex ¶
func (c *Connection) RefreshIndex(name string) (*Response, error)
RefreshIndex refreshes an index represented by a name
Example ¶
package main
import (
"fmt"
"os"
"github.com/belogik/goes"
)
var (
ES_HOST = "localhost"
ES_PORT = "9200"
)
func getConnection() (conn *goes.Connection) {
h := os.Getenv("TEST_ELASTICSEARCH_HOST")
if h == "" {
h = ES_HOST
}
p := os.Getenv("TEST_ELASTICSEARCH_PORT")
if p == "" {
p = ES_PORT
}
conn = goes.NewConnection(h, p)
return
}
func main() {
conn := getConnection()
resp, err := conn.RefreshIndex("yourindex")
if err != nil {
panic(err)
}
fmt.Printf("%s", resp)
}
func (*Connection) RemoveAlias ¶
func (c *Connection) RemoveAlias(alias string, indexes []string) (*Response, error)
RemoveAlias removes an alias to one or more indexes
func (*Connection) Scan ¶
func (c *Connection) Scan(query interface{}, indexList []string, typeList []string, timeout string, size int) (*Response, error)
Scan starts scroll over an index
func (*Connection) Scroll ¶
func (c *Connection) Scroll(scrollId string, timeout string) (*Response, error)
Scroll fetches data by scroll id
func (*Connection) Search ¶
func (c *Connection) Search(query interface{}, indexList []string, typeList []string, extraArgs url.Values) (*Response, error)
Search executes a search query against an index
Example ¶
package main
import (
"fmt"
"net/url"
"os"
"github.com/belogik/goes"
)
var (
ES_HOST = "localhost"
ES_PORT = "9200"
)
func getConnection() (conn *goes.Connection) {
h := os.Getenv("TEST_ELASTICSEARCH_HOST")
if h == "" {
h = ES_HOST
}
p := os.Getenv("TEST_ELASTICSEARCH_PORT")
if p == "" {
p = ES_PORT
}
conn = goes.NewConnection(h, p)
return
}
func main() {
conn := getConnection()
var query = map[string]interface{}{
"query": map[string]interface{}{
"bool": map[string]interface{}{
"must": map[string]interface{}{
"match_all": map[string]interface{}{},
},
},
},
"from": 0,
"size": 100,
"fields": []string{"onefield"},
"filter": map[string]interface{}{
"range": map[string]interface{}{
"somefield": map[string]interface{}{
"from": "some date",
"to": "some date",
"include_lower": false,
"include_upper": false,
},
},
},
}
extraArgs := make(url.Values, 1)
searchResults, err := conn.Search(query, []string{"someindex"}, []string{""}, extraArgs)
if err != nil {
panic(err)
}
fmt.Printf("%s", searchResults)
}
func (*Connection) UpdateIndexSettings ¶
func (c *Connection) UpdateIndexSettings(name string, settings interface{}) (*Response, error)
UpdateIndexSettings updates settings for existing index represented by a name and a settings as described here: https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-update-settings.html
func (*Connection) WithClient ¶
func (c *Connection) WithClient(cl *http.Client) *Connection
type Document ¶
type Document struct {
// XXX : interface as we can support nil values
Index interface{}
Type string
Id interface{}
BulkCommand string
Fields interface{}
}
Represents a document to send to elasticsearch
type Hit ¶
type Hit struct {
Index string `json:"_index"`
Type string `json:"_type"`
Id string `json:"_id"`
Score float64 `json:"_score"`
Source map[string]interface{} `json:"_source"`
Highlight map[string]interface{} `json:"highlight"`
Fields map[string]interface{} `json:"fields"`
}
Represent a hit returned by a search
type Hits ¶
type Hits struct {
Total uint64
// max_score may contain the "null" value
MaxScore interface{} `json:"max_score"`
Hits []Hit
}
Represent the hits structure as returned by elasticsearch
type IndexStatus ¶
type IndexStatus struct {
// XXX : problem, int will be marshaled to a float64 which seems logical
// XXX : is it better to use strings even for int values or to keep
// XXX : interfaces and deal with float64 ?
Index map[string]interface{}
Translog map[string]uint64
Docs map[string]uint64
Merges map[string]interface{}
Refresh map[string]interface{}
Flush map[string]interface{}
}
Represent the status for a given index for the _status command
type Item ¶
type Item struct {
Type string `json:"_type"`
Id string `json:"_id"`
Index string `json:"_index"`
Version int `json:"_version"`
Error string `json:"error"`
Status uint64 `json:"status"`
}
Represents the "items" field in a _bulk response
type Request ¶
type Request struct {
// Which connection will be used
Conn *Connection
// A search query
Query interface{}
// Which index to search into
IndexList []string
// Which type to search into
TypeList []string
// Request body
Body []byte
// A list of extra URL arguments
ExtraArgs url.Values
// contains filtered or unexported fields
}
Represents a Request to elasticsearch
type Response ¶
type Response struct {
Acknowledged bool
Error string
Errors bool
Status uint64
Took uint64
TimedOut bool `json:"timed_out"`
Shards Shard `json:"_shards"`
Hits Hits
Index string `json:"_index"`
Id string `json:"_id"`
Type string `json:"_type"`
Version int `json:"_version"`
Found bool
Count int
// Used by the _stats API
All All `json:"_all"`
// Used by the _bulk API
Items []map[string]Item `json:"items,omitempty"`
// Used by the GET API
Source map[string]interface{} `json:"_source"`
Fields map[string]interface{} `json:"fields"`
// Used by the _status API
Indices map[string]IndexStatus
// Scroll id for iteration
ScrollId string `json:"_scroll_id"`
Aggregations map[string]Aggregation `json:"aggregations,omitempty"`
Raw map[string]interface{}
}
Represents a Response from elasticsearch
type SearchError ¶
func (*SearchError) Error ¶
func (err *SearchError) Error() string
type StatIndex ¶
type StatIndex struct {
Primaries map[string]StatPrimary `json:"primaries"`
}