Skip to content

Commit

Permalink
add space after comma on last param (ocd)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbaird committed Jul 21, 2013
1 parent 59a1c45 commit 2192a35
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 35 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,3 @@
[submodule "cookbooks/elasticsearch"]
path = cookbooks/elasticsearch
url = https://github.com/mattbaird/elasticsearch-chef.git
[submodule "cookbooks/golang"]
path = cookbooks/golang
url = [email protected]:michaelklishin/go-language-chef-cookbook.git
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
elastigo
========

This fork fixes the Exists method and implements Scan support for SearchRequest and SearchUri.

Golang based Elasticsearch client, implements core api for Indexing and searching. GoDoc http://godoc.org/github.com/mattbaird/elastigo

To get the Chef based Vagrantfile working, be sure to pull like so:
Expand Down Expand Up @@ -56,11 +58,11 @@ A Direct Search using the api :
"term":map[string]string{"user:"kimchy"},
},
}
core.SearchRequest(true, "github", "Issues", qry, "")
core.SearchRequest(true, "github", "Issues", qry, "", 0)

A Direct Search using the query string Api :

core.SearchUri("github", "Issues", "user:kimchy", "")
core.SearchUri("github", "Issues", "user:kimchy", "", 0)

A Filtered search `Search DSL` :

Expand Down
15 changes: 15 additions & 0 deletions api/baseResponse.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package api

import (
"fmt"
)

type BaseResponse struct {
Ok bool `json:"ok"`
Index string `json:"_index,omitempty"`
Expand Down Expand Up @@ -37,6 +41,17 @@ func Pretty(pretty bool) string {
return prettyString
}


// http://www.elasticsearch.org/guide/reference/api/search/search-type/

func Scan(scan int) string {
scanString := ""
if scan > 0 {
scanString = fmt.Sprintf("&search_type=scan&size=%v",scan)
}
return scanString
}

func Scroll(duration string) string {
scrollString := ""
if duration != "" {
Expand Down
6 changes: 3 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func main() {
response, _ := core.Index(true, "twitter", "tweet", "1", NewTweet("kimchy", "Search is cool"))
indices.Flush()
log.Printf("Index OK: %v", response.Ok)
searchresponse, err := core.SearchRequest(true, "twitter", "tweet", "{\"query\" : {\"term\" : { \"user\" : \"kimchy\" }}}", "")
searchresponse, err := core.SearchRequest(true, "twitter", "tweet", "{\"query\" : {\"term\" : { \"user\" : \"kimchy\" }}}", "", 0)
if err != nil {
log.Println("error during search:" + err.Error())
log.Fatal(err)
Expand All @@ -48,8 +48,8 @@ func main() {
log.Printf("Search Found: %s", t)
response, _ = core.Get(true, "twitter", "tweet", "1")
log.Printf("Get: %v", response.Exists)
response, _ = core.Exists(true, "twitter", "tweet", "1")
log.Printf("Exists: %v", response.Exists)
exists, _ := core.Exists(true, "twitter", "tweet", "1")
log.Printf("Exists: %v", exists)
indices.Flush()
countResponse, _ := core.Count(true, "twitter", "tweet")
log.Printf("Count: %v", countResponse.Count)
Expand Down
38 changes: 22 additions & 16 deletions core/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,33 @@ func Get(pretty bool, index string, _type string, id string) (api.BaseResponse,
}

// The API also allows to check for the existance of a document using HEAD
// This appears to be broken in the current version of elasticsearch 0.19.10, currently
// returning nothing
func Exists(pretty bool, index string, _type string, id string) (api.BaseResponse, error) {

func Exists(pretty bool, index string, _type string, id string) (bool, error) {

var url string
var retval api.BaseResponse

var response map[string]interface{}

if len(_type) > 0 {
url = fmt.Sprintf("/%s/%s/%s?%s", index, _type, id, api.Pretty(pretty))
url = fmt.Sprintf("/%s/%s/%s?fields=_id%s", index, _type, id, api.Pretty(pretty))
} else {
url = fmt.Sprintf("/%s/%s?%s", index, id, api.Pretty(pretty))
url = fmt.Sprintf("/%s/%s?fields=_id%s", index, id, api.Pretty(pretty))
}
body, err := api.DoCommand("HEAD", url, nil)

req, err := api.ElasticSearchRequest("HEAD", url)

if err != nil {
return retval, err
fmt.Println(err)
}
if err == nil {
// marshall into json
jsonErr := json.Unmarshal(body, &retval)
if jsonErr != nil {
return retval, jsonErr
}

httpStatusCode, _, err := req.Do(&response)

if err != nil {
return false, err
}
//fmt.Println(body)
return retval, err
if httpStatusCode == 404 {
return false, err
} else {
return true, err
}
}
17 changes: 9 additions & 8 deletions core/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ var (
// 2) io.Reader that can be set in body (also valid elasticsearch string syntax..)
// 3) other type marshalable to json (also valid elasticsearch json)
//
// out, err := SearchRequest(true, "github","",qryType ,"")
// out, err := SearchRequest(true, "github","",qryType ,"", 0)
//
// http://www.elasticsearch.org/guide/reference/api/search/uri-request.html
func SearchRequest(pretty bool, index string, _type string, query interface{}, scroll string) (SearchResult, error) {
func SearchRequest(pretty bool, index string, _type string, query interface{}, scroll string, scan int) (SearchResult, error) {
var uriVal string
var retval SearchResult
if len(_type) > 0 && _type != "*" {
uriVal = fmt.Sprintf("/%s/%s/_search?%s%s", index, _type, api.Pretty(pretty), api.Scroll(scroll))
uriVal = fmt.Sprintf("/%s/%s/_search?%s%s%s", index, _type, api.Pretty(pretty), api.Scroll(scroll), api.Scan(scan))
} else {
uriVal = fmt.Sprintf("/%s/_search?%s%s", index, api.Pretty(pretty), api.Scroll(scroll))
uriVal = fmt.Sprintf("/%s/_search?%s%s%s", index, api.Pretty(pretty), api.Scroll(scroll), api.Scan(scan))
}
body, err := api.DoCommand("POST", uriVal, query)
if err != nil {
Expand All @@ -54,19 +54,19 @@ func SearchRequest(pretty bool, index string, _type string, query interface{}, s
// @_type: optional ("" if not used) search specific type in this index
// @query: valid string lucene search syntax
//
// out, err := SearchUri("github","",`user:kimchy` ,"")
// out, err := SearchUri("github","",`user:kimchy` ,"", 0)
//
// produces a request like this: host:9200/github/_search?q=user:kimchy"
//
// http://www.elasticsearch.org/guide/reference/api/search/uri-request.html
func SearchUri(index, _type string, query, scroll string) (SearchResult, error) {
func SearchUri(index, _type string, query, scroll string,scan int) (SearchResult, error) {
var uriVal string
var retval SearchResult
query = url.QueryEscape(query)
if len(_type) > 0 && _type != "*" {
uriVal = fmt.Sprintf("/%s/%s/_search?q=%s%s", index, _type, query, api.Scroll(scroll))
uriVal = fmt.Sprintf("/%s/%s/_search?q=%s%s%s", index, _type, query, api.Scroll(scroll), api.Scan(scan))
} else {
uriVal = fmt.Sprintf("/%s/_search?q=%s%s", index, query, api.Scroll(scroll))
uriVal = fmt.Sprintf("/%s/_search?q=%s%s%s", index, query, api.Scroll(scroll), api.Scan(scan))
}
//log.Println(uriVal)
body, err := api.DoCommand("GET", uriVal, nil)
Expand Down Expand Up @@ -132,6 +132,7 @@ type Hit struct {
Id string `json:"_id"`
Score Float32Nullable `json:"_score,omitempty"` // Filters (no query) dont have score, so is null
Source json.RawMessage `json:"_source"` // marshalling left to consumer
Fields json.RawMessage `json:"fields"` // when a field arg is passed to ES, instead of _source it returns fields
}

// Elasticsearch returns some invalid (according to go) json, with floats having...
Expand Down
2 changes: 1 addition & 1 deletion core/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestSearchRequest(t *testing.T) {
"wildcard": map[string]string{"actor": "a*"},
},
}
out, err := SearchRequest(true, "github", "", qry, "")
out, err := SearchRequest(true, "github", "", qry, "",0)
//log.Println(out)
Assert(&out != nil && err == nil, t, "Should get docs")
Assert(out.Hits.Len() == 10, t, "Should have 10 docs but was %v", out.Hits.Len())
Expand Down
4 changes: 2 additions & 2 deletions search/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestSearchRequest(t *testing.T) {
"wildcard": map[string]string{"actor": "a*"},
},
}
out, err := core.SearchRequest(true, "github", "", qry, "")
out, err := core.SearchRequest(true, "github", "", qry, "", 0)
//log.Println(out)
u.Assert(&out != nil && err == nil, t, "Should get docs")
u.Assert(out.Hits.Total == 616 && out.Hits.Len() == 10, t, "Should have 616 hits but was %v", out.Hits.Total)
Expand All @@ -41,7 +41,7 @@ func TestSearchSimple(t *testing.T) {
}

func TestSearchRequestQueryString(t *testing.T) {
out, err := core.SearchUri("github", "", "actor:a*", "")
out, err := core.SearchUri("github", "", "actor:a*", "", 0)
//log.Println(out)
u.Assert(&out != nil && err == nil, t, "Should get docs")
u.Assert(out.Hits.Total == 616, t, "Should have 616 hits but was %v", out.Hits.Total)
Expand Down

0 comments on commit 2192a35

Please sign in to comment.