gpt4 book ai didi

elasticsearch - 从 Golang 查询 ElasticSearch

转载 作者:IT王子 更新时间:2023-10-29 02:33:09 26 4
gpt4 key购买 nike

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但我没有找到从字符串查询中使用它的方法。

谢谢

最佳答案

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

关于elasticsearch - 从 Golang 查询 ElasticSearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39309862/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com