elasticsearch 从Golang查询ElasticSearch,elasticsearch,go,client,elasticsearch,Go,Client" /> elasticsearch 从Golang查询ElasticSearch,elasticsearch,go,client,elasticsearch,Go,Client" />

elasticsearch 从Golang查询ElasticSearch

elasticsearch 从Golang查询ElasticSearch,elasticsearch,go,client,elasticsearch,Go,Client,在中给定一个查询字符串,如下所示 如何从ElasticSearch获得响应。这个查询在某种意义上是有效的。 这就是我试图从ElasticSearch获取响应的方法:编码字符串并调用ElasticSearch package main import ( "fmt" "io/ioutil" "net/http" "net/url" ) func main() { query :

在中给定一个查询字符串,如下所示

如何从ElasticSearch获得响应。这个查询在某种意义上是有效的。 这就是我试图从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
{“error”:{“root\u cause”:[{“type”:“query\u parsing\u exception”,“reason”:”
无法分析查询[{\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{“匹配\”:{“名称\”:“Massadra\”}\n\t\t\t\t]\n\t\t\t}\n\t\t}\n\t}\n\t\t}],
“索引”:“员工”}],“类型”:“搜索\阶段\执行\异常”,
“原因”:“所有碎片失败”,“阶段”:“查询”,“分组”:真,
“失败的_碎片”:[{“碎片”:0,“索引”:“员工”,“节点”:“EbSLFZXfRCGoqnPcGsoNAg”,
“原因”:{“类型”:“查询\解析\异常”,“原因”:“未能解析查询”
[{\n\t\t\'query\':{\n\t\t\t\'bool\':{\n\t\t\t\t\'should\':
[\n\t\t\t\t{“匹配\”:{“名称\”:“Rodri\”}}\n\t\t\t{“匹配\”:
{\'name\':\'Massadra\'}\n\t\t\t\t]\n\t\t\t}\n\t\t}\n\t\t}],“索引”:“员工”,
“原因”:{“类型”:“解析异常”,“原因”:
“无法分析“{\n\t\t\”查询\“:{\n\t\t\t\”布尔\“:{\n\t\t\t\”应\“:[\n\t\t\t{”匹配\”:
{\'name\':\'Rodri\'}\n\t\t\t{\'match\':{\'name\':\'Massadra\'}\n\t\t\t\t\t]\n\t\t\t}\n\t\t}
遇到\“\”[\\n\\t\\t\\t\\t{\\\“匹配\\”:\“\”
在第1行第41列。\n应为:\n\“]\”..\n\“}\”..\n
“,”原因“:{”类型“:”解析异常“,”原因“:”遇到\”
\“[\\n\\t\\t\\t{\\\\”匹配\\\\”:\“\”在第1行第41列。\n应为一个
我也尝试过这个方法,但我没有找到一种从字符串查询中使用它的方法


谢谢

一般来说,在发送有效负载时,应该使用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))
}

一般来说,在发送有效负载时,应该使用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))
}

有没有办法使主体漂亮打印?我添加了&pretty as…}&pretty`)进行查询,但它不会改变。是的,您可以使用类似这样的方法:有没有办法使主体漂亮打印?我添加了&pretty as…}&pretty`)进行查询,但它不会改变。是的,您可以使用这样的方法: