Skip to content

Commit

Permalink
implemented count, update, flush and status (mostly)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbaird committed Oct 13, 2012
1 parent fa2b0e0 commit 2e0d422
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 23 deletions.
1 change: 1 addition & 0 deletions api/baseRequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
)

func DoCommand(method string, url string, data interface{}) (string, error) {
log.Printf("request is %s", url)
var response map[string]interface{}
var body string
req, err := ElasticSearchRequest(method, url)
Expand Down
13 changes: 9 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@ package main

import (
"github.com/mattbaird/elastigo/core"
"github.com/mattbaird/elastigo/indices"
"log"
)

// for testing
func main() {
// core.RunSearch(true, "questionscraper", "user:kimchy")
log.Println("Index:")
response, _ := core.Index(true, "twitter", "tweet", "1", NewTweet("kimchy", "Search is cool"))
log.Printf("Get: %s", response.Exists)
log.Printf("Index OK: %b", response.Ok)
response, _ = core.Get(true, "twitter", "tweet", "1")
log.Println("Exists:")
log.Printf("Get: %s", response.Exists)
response, _ = core.Exists(true, "twitter", "tweet", "1")
log.Println("Delete:")
log.Printf("Exists: %s", response.Exists)
indices.Flush()
countResponse, _ := core.Count(true, "twitter", "tweet")
log.Printf("Count: %s", countResponse.Count)
response, _ = core.Delete(true, "twitter", "tweet", "1", -1, "")
log.Printf("Delete OK: %b", response.Ok)
response, _ = core.Get(true, "twitter", "tweet", "1")
log.Printf("Get: %s", response.Exists)

}
43 changes: 43 additions & 0 deletions core/count.go
Original file line number Diff line number Diff line change
@@ -1 +1,44 @@
package core

import (
"encoding/json"
"fmt"
"github.com/mattbaird/elastigo/api"
)

type CountResponse struct {
Count int `json:"count"`
Shard Shards `json:"_shards"`
}

type Shards struct {
Total int `json:"total"`
Successful int `json:"successful"`
Failed int `json:"failed"`
}

// The count API allows to easily execute a query and get the number of matches for that query.
// It can be executed across one or more indices and across one or more types.
// The query can either be provided using a simple query string as a parameter,
//or using the Query DSL defined within the request body.
// http://www.elasticsearch.org/guide/reference/api/count.html
// TODO: take parameters.
// currently not working against 0.19.10
func Count(pretty bool, index string, _type string) (CountResponse, error) {
var url string
var retval CountResponse
url = fmt.Sprintf("/%s/%s/_count?%s", index, _type, api.Pretty(pretty))
body, err := api.DoCommand("GET", url, nil)
if err != nil {
return retval, err
}
if err == nil {
// marshall into json
jsonErr := json.Unmarshal([]byte(body), &retval)
if jsonErr != nil {
return retval, jsonErr
}
}
fmt.Println(body)
return retval, err
}
37 changes: 36 additions & 1 deletion core/update.go
Original file line number Diff line number Diff line change
@@ -1 +1,36 @@
package core
package core

import (
"encoding/json"
"fmt"
"github.com/mattbaird/elastigo/api"
)

// The update API allows to update a document based on a script provided. The operation gets the document
// (collocated with the shard) from the index, runs the script (with optional script language and parameters),
// and index back the result (also allows to delete, or ignore the operation). It uses versioning to make sure
// no updates have happened during the “get” and “reindex”. (available from 0.19 onwards).
// Note, this operation still means full reindex of the document, it just removes some network roundtrips
// and reduces chances of version conflicts between the get and the index. The _source field need to be enabled
// for this feature to work.
//
// http://www.elasticsearch.org/guide/reference/api/update.html
// TODO: finish this, it's fairly complex
func Update(pretty bool, index string, _type string, id string) (api.BaseResponse, error) {
var url string
var retval api.BaseResponse
url = fmt.Sprintf("/%s/%s/%s/_update?%s", index, _type, id, api.Pretty(pretty))
body, err := api.DoCommand("POST", url, nil)
if err != nil {
return retval, err
}
if err == nil {
// marshall into json
jsonErr := json.Unmarshal([]byte(body), &retval)
if jsonErr != nil {
return retval, jsonErr
}
}
fmt.Println(body)
return retval, err
}
37 changes: 36 additions & 1 deletion indices/flush.go
Original file line number Diff line number Diff line change
@@ -1 +1,36 @@
package indices
package indices

import (
"encoding/json"
"fmt"
"github.com/mattbaird/elastigo/api"
)

// The flush API allows to flush one or more indices through an API. The flush process of an index basically
// frees memory from the index by flushing data to the index storage and clearing the internal transaction
// log. By default, ElasticSearch uses memory heuristics in order to automatically trigger flush operations
// as required in order to clear memory.
// http://www.elasticsearch.org/guide/reference/api/admin-indices-flush.html
// TODO: add Shards to response
func Flush(index ...string) (api.BaseResponse, error) {
var url string
var retval api.BaseResponse
if len(index) > 0 {
url = fmt.Sprintf("/%s/_flush", index)
} else {
url = "/_flush"
}
body, err := api.DoCommand("POST", url, nil)
if err != nil {
return retval, err
}
if err == nil {
// marshall into json
jsonErr := json.Unmarshal([]byte(body), &retval)
if jsonErr != nil {
return retval, jsonErr
}
}
fmt.Println(body)
return retval, err
}
38 changes: 21 additions & 17 deletions indices/status.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
package indices

import (
"encoding/json"
"fmt"
"github.com/mattbaird/elastigo/api"
"log"
)

// Lists status details of all indices or the specified index.
// http://www.elasticsearch.org/guide/reference/api/admin-indices-status.html
func RunStatus(pretty bool, indices ...string) {
index := ""
if len(indices) >= 1 {
index = indices[0]
}

var response map[string]interface{}

func Status(pretty bool, indices ...string) (api.BaseResponse, error) {
var retval api.BaseResponse
var body string
if len(index) > 0 {
body = api.ElasticSearchRequest("GET", "/"+index+"/_status?pretty=1").Do(&response)
var url string
if len(indices) > 0 {
//TODO, emit the indices csv style

url = "/" + indices[0] + "/_status?pretty=1"
} else {
body = api.ElasticSearchRequest("GET", "/_status?pretty=1").Do(&response)
url = "/_status?pretty=1"
}

if error, ok := response["error"]; ok {
status, _ := response["status"]
log.Fatalf("Error: %v (%v)\n", error, status)
body, err := api.DoCommand("GET", url, nil)
if err != nil {
return retval, err
}
if err == nil {
// marshall into json
jsonErr := json.Unmarshal([]byte(body), &retval)
if jsonErr != nil {
return retval, jsonErr
}
}
fmt.Print(body)
fmt.Println(body)
return retval, err
}

0 comments on commit 2e0d422

Please sign in to comment.