i'm relatively new go , i'm trying search records in elastic search, below basic code have created, far i'm able perform simple request "http://10.132.0.13:9200/" , results returned expected. however, once try run more complex request fails.
below code have created far.
package main import ( "fmt" "io/ioutil" "net/http" ) func main() { //request, err := http.get(`http://10.132.0.13:9200/`) // returns test page request, err := http.get(`http://10.132.0.13:9200/database-*/_search?pretty -d { "query": { "match": {"_all": "searchterm"}}}`) if err != nil { //error } defer request.body.close() body, err := ioutil.readall(request.body) fmt.println(string(body)) }
i can query index directly use of curl -xget , returns expected results
is there way implement similar curl -xget in golang?
you can try this. submitting json data (or form data), means should using post or put. xget overrides default behavior of request. using curl switch on post , see if query still works.
package main import ( "bytes" "fmt" "io/ioutil" "net/http" ) func main() { query := []byte(`{"query": { "match": {"_all": "searchterm"}}}`) req, err := http.newrequest("post", "http://10.132.0.13:9200/database-*/_search?pretty", bytes.newbuffer(query)) if err != nil { panic(err) } client := &http.client{} resp, err := client.do(req) if err != nil { panic(err) } defer resp.body.close() body, err := ioutil.readall(resp.body) if err != nil { panic(err) } fmt.println(string(body)) }
Comments
Post a Comment