goes

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

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

Go to latest
Published: Jun 23, 2013 License: BSD-3-Clause Imports: 8 Imported by: 0

README

Goes : a library to interact with ElasticSearch
===============================================

Supported operations
--------------------

- index creation
- index removal
- simple indexing (document)
- bulk indexing
- search
- get

Example
-------

You will find examples in example_test.go

License
-------

Copyright (c) 2013 Belogik. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

   * Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
   * Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
   * Neither the name of Belogik nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

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 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 Connection

type Connection struct {
	// The host to connect to
	Host string

	// The port to use
	Port string
}

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) BulkSend

func (c *Connection) BulkSend(index string, documents []Document) (Response, error)

Bulk adds multiple documents in bulk mode to the index for a given type

func (*Connection) CreateIndex

func (c *Connection) CreateIndex(name string, mapping map[string]interface{}) (Response, error)

CreateIndex creates a new index represented by a name and a mapping

Example
package main

import (
	"fmt"
	"goes"
)

func main() {
	conn := goes.NewConnection("localhost", "9200")

	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"
	"goes"
	"net/url"
)

func main() {
	conn := goes.NewConnection("localhost", "9200")

	//[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"
	"goes"
)

func main() {
	conn := goes.NewConnection("localhost", "9200")
	resp, err := conn.DeleteIndex("yourinde")

	if err != nil {
		panic(err)
	}

	fmt.Printf("%s", resp)
}
Output:

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) 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"
	"goes"
	"net/url"
)

func main() {
	conn := goes.NewConnection("localhost", "9200")

	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) RefreshIndex

func (c *Connection) RefreshIndex(name string) (Response, error)

RefreshIndex refreshes an index represented by a name

Example
package main

import (
	"fmt"
	"goes"
)

func main() {
	conn := goes.NewConnection("localhost", "9200")
	resp, err := conn.RefreshIndex("yourindex")

	if err != nil {
		panic(err)
	}

	fmt.Printf("%s", resp)
}
Output:

func (*Connection) Search

func (c *Connection) Search(query map[string]interface{}, indexList []string, typeList []string) (Response, error)

Search executes a search query against an index

Example
package main

import (
	"fmt"
	"goes"
)

func main() {
	conn := goes.NewConnection("localhost", "9200")

	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,
				},
			},
		},
	}

	searchResults, err := conn.Search(query, []string{"someindex"}, []string{""})

	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

type Document

type Document struct {
	// XXX : interface as we can support nil values
	Index       interface{}
	Type        string
	Id          interface{}
	BulkCommand string
	Fields      map[string]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"`
	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 {
	Ok      bool   `json:"ok"`
	Type    string `json:"_type"`
	Id      string `json:"_id"`
	Index   string `json:"_index"`
	Version int    `json:"_version"`
}

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

	// 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 return the Response obtained

func (*Request) Url

func (r *Request) Url() string

Url builds a Request for a URL

type Response

type Response struct {
	Ok           bool
	Acknowledged bool
	Error        string
	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

	// 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
	Exists bool
	Source map[string]interface{} `json:"_source"`
	Fields map[string]interface{} `json:"fields"`

	// Used by the _status API
	Indices map[string]IndexStatus
}

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
}

Jump to

Keyboard shortcuts

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