gpt4 book ai didi

elasticsearch - 从 golang 服务器到 ES 的 ES 查询返回错误,而 postman 直接向 ES 请求返回预期结果

转载 作者:IT王子 更新时间:2023-10-29 02:06:43 31 4
gpt4 key购买 nike

这是我为此端点使用 Postman localhost:9201/response_v2_862875ee3a88a6d09c95bdbda029ce2b/_search

的请求正文
{
"_source": ["_id"],
"from": 1,
"size": 10,
: {
"should": {
"match": {

}
}, {
"range": {
"_updated_at": {
"from": "36163",
"include_lower": true,
"include_upper": true,
"to": null
}
}
}]
}
}
}

到这个 url localhost:9201/rensedbda029ce2b/_search

我得到了结果 https://gist.gith

但是当我从我的服务器向 ES 发出相同的请求时,我收到一条错误消息“elastic:错误 400(错误请求):预期 [START_OBJECT] 但发现 [START_ARRAY] [type=parsing_exception]”

这些是我的代码片段。我从另一个 util 函数获取查询并在调用 ES 时使用它。

这是对 ES res, err = r.esConn.Search(indexName).e(requestBody.ResponsePageLength).Do(ctx)

的调用

查询构建器函数是这样的,它将从请求正文中提取的参数发送到我的服务器,并基于该参数构建查询。

func CreateMonitoringPipeline(maxResponseTime string, responseQueries []ResponseQuery, baselineFormId string) *elastic.BoolQuery {
finalQuery := elastic.NewBoolQuery()

dateRangeMatchQuery := elastic.NewRangeQuery("_updated_at").
Gte(maxResponseTime)

finalQuery.Filter(dateRangeMatchQuery)
}

return finalQuery
}

我不明白为什么会这样?我的 ES 使用 ES 二进制文件运行,我的服务器在 docker 容器中运行。

对 ES 和 golang 完全陌生,所以请帮忙。

更新:

这是我使用 SetTraceLog 记录请求时得到的结果

| ELASTICPOST /resp8ecf8427e/_search HTTP/1.1
| Host: 172.17.0.1:9201
| User-Agent: elastic/5.0.81 (linux-amd64)
| Transfer-Encoding: chunked
| Accept: application/json
| Content-Type: application/json
| Accept-Encoding: gzip
|
| 7
| ["_id"]
| 0

我不明白 7 和 ["_id"] 是什么意思。这是ES收到的我的请求体吗?

最佳答案

感谢您上传日志,您是对的,["_id"] 是正在发送的请求。问题出在请求行中,因为 Source([]string{"_id"}) 没有按照您的预期将源字段设置为 ["_id"] 而是:

Source allows the user to set the request body manually without using any of the structs and interfaces in Elastic.

https://godoc.org/github.com/olivere/elastic#SearchService.Source

您想改用 FetchSourceContext:

res, err = r.esConn.Search(indexName).From(requestBody.MaxResponseTimestampCount).FetchSourceContext(elastic.NewFetchSourceContect(true). Include("_id")).Query(query).Size(requestBody.ResponsePageLength).Do(ctx)

https://godoc.org/github.com/olivere/elastic#SearchService.FetchSourceContext

关于elasticsearch - 从 golang 服务器到 ES 的 ES 查询返回错误,而 postman 直接向 ES 请求返回预期结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56611518/

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