Given a query string in Go like below in Sense
GET employee/info/_search
{
"query":{
"bool": {
"should": [
{"match": {"name": "Rodri"}},
{"match": {"name": "Massadra"}}
]
}
}
}
how to get the response from ElasticSearch. This query works in Sense. This is how I try to do get a response from ElasticSearch: encoding the string and call ElasticSearch
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
func main() {
query := `{
"query":{
"bool": {
"should": [
{"match": {"name": "Rodri"}},
{"match": {"name": "Massadra"}}
]
}
}
}`
query = url.QueryEscape(query)
resp, err := http.Get("http://localhost:9200/employee/info/_search?q=" + query)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Printf("
%s", body)
}
and this is the error i got:
{"error":{"root_cause":[{"type":"query_parsing_exception","reason":"
Failed to parse query [{
\t\t\"query\":{
\t\t\t\"bool\":
{
\t\t\t\t\"should\": [
\t\t\t\t{\"match\": {\"name\": \"Rodri\"}},
\t\t\t\t{\"match\": {\"name\": \"Massadra\"}}
\t\t\t\t]
\t\t\t}
\t\t}
\t\t}]",
"index":"employee"}],"type":"search_phase_execution_exception",
"reason":"all shards failed","phase":"query","grouped":true,
"failed_shards":[{"shard":0,"index":"employee","node":"EbSLFZXfRCGoqnPcGsoNAg",
"reason":{"type":"query_parsing_exception","reason":"Failed to parse query
[{
\t\t\"query\":{
\t\t\t\"bool\": {
\t\t\t\t\"should\":
[
\t\t\t\t{\"match\": {\"name\": \"Rodri\"}},
\t\t\t\t{\"match\":
{\"name\": \"Massadra\"}}
\t\t\t\t]
\t\t\t}
\t\t}
\t\t}]","index":"employee",
"caused_by":{"type":"parse_exception","reason":
"Cannot parse '{
\t\t\"query\":{
\t\t\t\"bool\": {
\t\t\t\t\"should\": [
\t\t\t\t{\"match\":
{\"name\": \"Rodri\"}},
\t\t\t\t{\"match\": {\"name\": \"Massadra\"}}
\t\t\t\t]
\t\t\t}
\t\t}
\t\t}':
Encountered \" <RANGE_GOOP> \"[\
\\t\\t\\t\\t{\\\"match\\\": \"\"
at line 1, column 41.
Was expecting one of:
\"]\" ...
\"}\" ...
","caused_by":{"type":"parse_exception","reason":"Encountered \" <RANGE_GOOP>
\"[\
\\t\\t\\t\\t{\\\"match\\\": \"\" at line 1, column 41.
Was expecting one
I also try this client but I didn't find a way to use it from string query.
Thank you