dtt83024 2019-06-15 15:01
浏览 210
已采纳

从golang服务器到ES的ES查询返回错误,而直接向ES的邮递员请求则返回预期结果

This is the request body that I for this endpoint using 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
                }
            }
        }]
    }
}
}

To this url localhost:9201/rensedbda029ce2b/_search

And I get the results https://gist.gith

But when I make the same request from my server to ES I get an error saying "elastic: Error 400 (Bad Request): Expected [START_OBJECT] but found [START_ARRAY] [type=parsing_exception]"

These are some snippets of my code. I get the query from another util function and use that while making the call to ES.

This is the call to ES res, err = r.esConn.Search(indexName).e(requestBody.ResponsePageLength).Do(ctx)

and the query builder function is this, it takes arguments which are extracted from the body of the request to my server and builds a query based on that.

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
}

I can't figure out why is this happening? my ES is running using the ES binary and my server runs in a docker container.

Completely new to ES and golang so please help.

UPDATE:

This is what I got when I logged my request using 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

I can't understand what do the 7 and ["_id"] mean. Is this my request body that ES received?

  • 写回答

1条回答 默认 最新

  • doutuosai3504 2019-06-16 17:05
    关注

    Thanks for uploading the logs, you are right that the ["_id"] is the request being sent. The problem is in the request line as Source([]string{"_id"}) does not set the source field to ["_id"] as you intended but instead:

    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

    You want to use FetchSourceContext instead:

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!