使用Ruby传递HTTP GET请求

时间:2017-10-02 07:20:09

标签: json ruby http elasticsearch get

我正在尝试使用Ruby脚本在elasticsearch中获取大量文档。要做到这一点,我需要传递一个HTTP GET请求与身体,我似乎被困在这。我正在使用rest-client gem,因为它似乎是最简单的。

以下是我在整个Elasticsearch数据库中获取文档数量的代码:

url = 'http://localhost:9200/bookings/_count'
response = RestClient.get(url)
puts response

现在我需要传递相同的请求,但使用此JSON,作为正文:

query: {
 filtered: {
  query: {
    term: { status: "book_confirmed" }
    },
     filter: {
      bool: {
       must: {
        range: { date_booked: { gte: "now-7d/d", lte: "now-7d", time_zone: "+2:00" } } },
       should: {
        term: {contract: "xd.hotels"} }
      }
     }
  }

}

我该怎么做?

编辑:

我已经设法通过POST实现了这一目标。当我发布我不知道的问题时,Elasticsearch接受Count API中的POST请求。

以下是代码:

urlago = 'http://localhost:9200/bookings/_count'
responseago = RestClient.post(urlago, {query: { filtered: { query: { term: { status: "book_confirmed" } }, filter: { bool: { must: { range: { date_booked: { gte: "now-7d/d", lte: "now-7d", time_zone: "+2:00" } } }, should: { term: {contract: "xd.hotels"} } } } } } }.to_json, {content_type: :json, accept: :json})
puts 'Week ago:' 
puts responseago

0 个答案:

没有答案
相关问题