1

I would like to know why this code runs but doesn't filter the data as it should. The same request in postman works but in Kotlin it doesn't what is happening? The goal is to filter the data by timestamp value.

val getFiltered = restTemplate.exchange(
    "https://X.X.X.X:6200/ble_flow-$da/_search/?size=50&pretty=1",
    HttpMethod.GET, HttpEntity("{\\r\\n\\\"query\\\": { \\r\\n    \\\"bool\\\": { \\r\\n      \\\"filter\\\": [ \\r\\n        { \\\"range\\\": { \\\"timestamp\\\": { \\\"gte\\\": \\\"2019-08-12T06:00:00\\\",\\\"lt\\\":\\\"2019-08-12T011:00:00\\\"}}} \\r\\n      ]\\r\\n    }\\r\\n  }\\r\\n}", headers), 
    ResultsFlow::class.java)
println(getFiltered)

It would solve the problem if I could transform the body:

{
"query": { 
    "bool": { 
      "filter": [ 
        { "range": { "timestamp": { "gte": "2019-08-12T06:00:00","lt":"2019-08-12T07:00:00"}}} 
      ]
    }
  }
}

into url query. But I don' really know how to do this. Thanks.

Simulant
  • 19,190
  • 8
  • 63
  • 98
Ivan Rosa
  • 85
  • 8

1 Answers1

0

The Spring RestTemplate does not send your Body in a GET request, as a GET request should not contain a body but use query parameters instead. Read more on it here HTTP GET with request body

Therefore the Elasticsearch API allows also POST to send a query with a body. I would recommend this as your first solution: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html

Both HTTP GET and HTTP POST can be used to execute search with body. Since not all clients support GET with body, POST is allowed as well.

If you really want to use a GET request from Spring RestTemplate to transfer a body you need to replace and extend the RequestFactory. You can find an example for exactly your case in this blog post.

Simulant
  • 19,190
  • 8
  • 63
  • 98