cone / golastic

Go library to consume the elastic search (now just elastic) API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

golastic

Build Status views

Go library to consume the elastic search API

##Getting started

###Installing the project

Simply execute the following command

go get github.com/cone/golastic.git

Then you can import the project in your code

import "github.com/cone/golastic"

###Dependencies

Golastic uses go-uuid to generate unique ids fo the indexed documents if none was specified, so you must get the go-uuid project with the following command

go get code.google.com/p/go-uuid/uuid

###Use

First you must specify the url of the server you want to connect to. Then you can start querying for or indexing documents

server, _ := New("http://localhost:9200")

resultItem, err := golastic.From("test", "product").Find("1")

##Operations

###From

It allows you to specify the Index and Type in a single step

###Index

It allows you to specify the Index. For example, you can use it to delete an index

  _, err = golastic.Index("test").DeleteDoc("")

###Type

It allows you to specify the Type after you sepcifed an index

_, err = golastic.Index("test").Type("product").Find("1")

###Find

It allows you to find a document by its id

resultItem, err := golastic.From("test", "product").Find("1")

###Exec

It allows you to find multiple results using a query. You can specify a method like "post" (default if left blank) or "delete". The last in case you want to delete the matching documents

result, err := golastic.From("test", "product").Exec("post", Query("match_all"))

###IndexDoc

It allows you to index a document. You can specify a custom id or leave it blank to let Golastic generate one for you

_, err = golastic.From("test", "words").IndexDoc("1", TestProduct{1, "Potatoe"})

###UpdateDoc

It allows you to update a document sereahcing for it using its id

  _, err = golastic.From("test", "words").UpdateDoc("1", TestProduct{2, "Apple"})

###DeleteDoc

It allows you to delete a document with the specified id

  _, err = golastic.From("test", "words").DeleteDoc("1")

###Bulk

It allows you to do bulk operations

b := Bulk().
    Index("1", TestProduct{1, "Prod1"}).
    Index("2", TestProduct{2, "Prod2"}).
    Update("3", TestProduct{3, "Prod3"})


errs := golastic.From("test", "words").Bulk(b)

###Scan

It lets you map the resulting document to a struct. It can only be used on results

  resultItem, _ := golastic.From("test", "product").Find("1")

product := TestProduct{}

resultItem.Scan(&product)

You can also pass a slice for query operations

  result, err := golastic.From("test", "product").Exec("", Query("match_all"))

products := []TestProduct{}
//or also []*TestProduct{}

result.Scan(&products)

##Queries

The queries are executed using the Exec function. Aside the method (e.g. "post" or "delete" or "" to default to "post") you should specify the query as a map or using the Item helper

//passing a map
q = Query("match").Set(
	  map[string]map[string]string{
    "message": map[string]string{
      "query": "this is a test",
    },
  },
  )

//Results in:
/*
  {  
    "query":{  
      "match":{  
        "message":{  
          "query":"this is a test"
        }
      }
    }
  }
*/

Or you can use the Item helper to achieve the same result as above

q = Query("match").Set(
	  Item().Put("message", Item().Put("query", "this is a test")),
  )

More helpers for the queries will be added in the future.

##Filters

Filters are build in a similar way to Queries. Queries can contain filters

f := Filter("and").Set([]interface{}{
	Item().Put(
		"range",
		Item().Put(
			"postDate",
			Item().
				Put("from", "2010-03-01").
				Put("to", "2010-04-01"),
		),
	),
})

Contributing

  1. Create your feature branch (git checkout -b feature/my-new-feature)
  2. Commit your changes (git commit -am 'Add some feature')
  3. Push to the branch (git push origin feature/my-new-feature)
  4. Create a new Pull Request

About the Author

Carlos Gutierrez (cone) is a sofware developer based in Colima, Mexico. He is currently working for an awsome company called Crowd Interactive a company specialized in building and growing online retail stores.

About

Go library to consume the elastic search (now just elastic) API

License:MIT License


Languages

Language:Go 100.0%