登录
首页 >  Golang >  Go问答

从 Golang 服务器到 ES 的查询出错,但Postman直接向ES请求返回了预期的结果

来源:stackoverflow

时间:2024-02-29 14:27:24 289浏览 收藏

大家好,今天本人给大家带来文章《从 Golang 服务器到 ES 的查询出错,但Postman直接向ES请求返回了预期的结果》,文中内容主要涉及到,如果你对Golang方面的知识点感兴趣,那就请各位朋友继续看下去吧~希望能真正帮到你们,谢谢!

问题内容

这是我使用 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
                }
            }
        }]
    }
}
}

到此网址 localhost:9201/rensedbda029ce2b/_search

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

但是当我从服务器向 es 发出相同的请求时,我收到一条错误消息“elastic: error 400 (bad request): expected [start_object] but find [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 允许用户手动设置请求正文,而无需使用 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

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《从 Golang 服务器到 ES 的查询出错,但Postman直接向ES请求返回了预期的结果》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>