goes

package module
Version: v0.0.0-...-e54d722 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2015 License: BSD-3-Clause Imports: 10 Imported by: 223

README

There is a new maintener for this library.

Please go here : https://github.com/OwnLocal/goes

!!!!! By using this repo you are running on thin ice !!!!!!

https://github.com/belogik/goes/issues/40 might be of interest for you.

Documentation

Overview

Package goes provides an API to access Elasticsearch.

Index

Examples

Constants

View Source
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

func (Bucket) DocCount

func (b Bucket) DocCount() uint64

DocCount returns count of documents in this bucket

func (Bucket) Key

func (b Bucket) Key() interface{}

Key returns key for aggregation 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)
}
Output:

func (*Connection) Delete

func (c *Connection) Delete(d Document, extraArgs url.Values) (*Response, error)

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)
}
Output:

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)
}
Output:

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

func (c *Connection) Index(d Document, extraArgs url.Values) (*Response, error)

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)
}
Output:

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

func (c *Connection) Optimize(indexList []string, extraArgs url.Values) (*Response, error)

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)
}
Output:

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)
}
Output:

func (*Connection) Stats

func (c *Connection) Stats(indexList []string, extraArgs url.Values) (*Response, error)

Stats fetches statistics (_stats) for the current elasticsearch server

func (*Connection) Update

func (c *Connection) Update(d Document, query interface{}, extraArgs url.Values) (*Response, error)

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

func (*Request) Run

func (req *Request) Run() (*Response, error)

Run executes an elasticsearch Request. It converts data to Json, sends the request and returns the Response obtained

func (*Request) Url

func (r *Request) Url() string

Url builds a Request for a URL

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

type SearchError struct {
	Msg        string
	StatusCode uint64
}

func (*SearchError) Error

func (err *SearchError) Error() string

type Shard

type Shard struct {
	Total      uint64
	Successful uint64
	Failed     uint64
}

Represents the "shard" struct as returned by elasticsearch

type StatIndex

type StatIndex struct {
	Primaries map[string]StatPrimary `json:"primaries"`
}

type StatPrimary

type StatPrimary struct {
	// primary/docs:
	Count   int
	Deleted int
}

Source Files

Jump to

Keyboard shortcuts

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