David
David

Reputation: 331

Querying ElasticSearch from Golang

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("\n%s", body)
}

and this is the error i got:

{"error":{"root_cause":[{"type":"query_parsing_exception","reason":"
Failed to parse query [{\n\t\t\"query\":{\n\t\t\t\"bool\": 
{\n\t\t\t\t\"should\": [\n\t\t\t\t{\"match\": {\"name\": \"Rodri\"}},
\n\t\t\t\t{\"match\": {\"name\": \"Massadra\"}}\n\t\t\t\t]\n\t\t\t}\n\t\t}\n\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
 [{\n\t\t\"query\":{\n\t\t\t\"bool\": {\n\t\t\t\t\"should\":
 [\n\t\t\t\t{\"match\": {\"name\": \"Rodri\"}},\n\t\t\t\t{\"match\": 
{\"name\": \"Massadra\"}}\n\t\t\t\t]\n\t\t\t}\n\t\t}\n\t\t}]","index":"employee",
"caused_by":{"type":"parse_exception","reason":
"Cannot parse '{\n\t\t\"query\":{\n\t\t\t\"bool\": {\n\t\t\t\t\"should\": [\n\t\t\t\t{\"match\":
 {\"name\": \"Rodri\"}},\n\t\t\t\t{\"match\": {\"name\": \"Massadra\"}}\n\t\t\t\t]\n\t\t\t}\n\t\t}\n\t\t}': 
Encountered \" <RANGE_GOOP> \"[\\n\\t\\t\\t\\t{\\\"match\\\": \"\" 
at line 1, column 41.\nWas expecting one of:\n    \"]\" ...\n    \"}\" ...\n   
 ","caused_by":{"type":"parse_exception","reason":"Encountered \" <RANGE_GOOP> 
\"[\\n\\t\\t\\t\\t{\\\"match\\\": \"\" at line 1, column 41.\nWas expecting one

I also try this client but I didn't find a way to use it from string query.

Thank you

Upvotes: 0

Views: 3737

Answers (1)

Val
Val

Reputation: 217514

Generally speaking, you should be using POST instead of GET when sending a payload (even if ES accepts payload in GET requests).

Try this code instead:

package main

import (
    "bytes"
    "fmt"
    "io/ioutil"
    "net/http"
)

func main() {
    url := "http://localhost:9200/employee/info/_search"
    query := []byte(`{
            "query":{
                "bool": {
                    "should": [
                    {"match": {"name": "Rodri"}},
                    {"match": {"name": "Massadra"}}
                    ]
                }
            }
            }`)
    req, err := http.NewRequest("POST", url, bytes.NewBuffer(query))
    req.Header.Set("Content-Type", "application/json")

    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.Printf("\n%s", string(body))
}

Upvotes: 4

Related Questions