从Golang查询ElasticSearch

时间:2016-09-03 18:00:37

标签: elasticsearch go client

Sense

中给出Go中的查询字符串
GET employee/info/_search
{
    "query":{
        "bool": {
            "should": [
               {"match": {"name": "Rodri"}},
                {"match": {"name": "Massadra"}}
            ]
        }
    }
}

如何从ElasticSearch获取响应。此查询在Sense中有效。 这就是我尝试从ElasticSearch获取响应的方法:编码字符串并调用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)
}

这是我得到的错误:

{"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

我也试试这个client,但我找不到从字符串查询中使用它的方法。

谢谢

1 个答案:

答案 0 :(得分:3)

一般来说,在发送有效负载时,您应该使用POST而不是GET(即使ES接受GET请求中的有效负载)。

请尝试使用此代码:

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))
}