douliaodun9153 2016-09-03 18:00
浏览 263

从Golang查询ElasticSearch

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

  • 写回答

1条回答 默认 最新

  • dopnpoh056622 2016-09-04 05:48
    关注

    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("
    %s", string(body))
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集