passing json data in elasticsearch get request using rest-client ruby gem

核能气质少年 提交于 2019-12-06 00:13:10

RestClient can't send request bodies with GET. You've got two options:

Pass your query as the source URL parameter:

require 'rest_client'
require 'json'

# RestClient.log=STDOUT # Optionally turn on logging

q = '{
    "query" : { "term" : { "user" : "kimchy" } }
}
'
r = JSON.parse \
      RestClient.get( 'http://localhost:9200/twitter/tweet/_search',
                      params: { source: q } )

puts r

...or just use POST.


UPDATE: Fixed incorrect passing of the URL parameter, notice the params Hash.

In case anyone else finds this. It IS possible, although not recommended, to send request bodies with GET by using the internal Request method that the main API uses to create it's calls.

RestClient::Request.execute( method: :get, 
                             url: 'http://localhost:9200/twitter/tweet/_search',
                             payload: {source: q} )

See here for more details.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!