问题描述
如何执行以下查询(在 doc )使用其余客户端。
How do I execute the below query(given in doc) using rest client.
curl -XGET 'http://localhost:9200/twitter/tweet/_search' -d '{
"query" : {
"term" : { "user" : "kimchy" }
}
}
'
我尝试这样做:
q = '{
"query" : {
"term" : { "user" : "kimchy" }
}
}
'
r = JSON.parse(RestClient.get('http://localhost:9200/twitter/tweet/_search', q))
这引发了错误:
in `process_url_params': undefined method `delete_if' for #<String:0x8b12e18> (NoMethodError)
from /home/socialapps/.rvm/gems/ruby-1.9.3-p194/gems/rest-client-1.6.7/lib/restclient/request.rb:40:in `initialize'
from /home/socialapps/.rvm/gems/ruby-1.9.3-p194/gems/rest-client-1.6.7/lib/restclient/request.rb:33:in `new'
from /home/socialapps/.rvm/gems/ruby-1.9.3-p194/gems/rest-client-1.6.7/lib/restclient/request.rb:33:in `execute'
from /home/socialapps/.rvm/gems/ruby-1.9.3-p194/gems/rest-client-1.6.7/lib/restclient.rb:68:in `get'
from get_check2.rb:12:in `<main>'
中,当我使用 RestClient进行相同操作时。发布
,它给了我正确的结果!但是elasticsearch doc在curl命令中使用 XGET
进行搜索查询,而不使用 XPOST
。如何使 RestClient.get
方法起作用?
When I do the same using RestClient.post
, it gives me the right results!. But the elasticsearch doc uses XGET
in curl command for the search query and not XPOST
. How do I get the RestClient.get
method to work?
如果有其他/更好的方法来执行此操作,请提出建议。
If there are alternate/better ways of doing this action, please suggest.
推荐答案
RestClient无法使用 GET
发送请求正文。您有两种选择:
RestClient can't send request bodies with GET
. You've got two options:
将查询作为来源
URL参数传递:
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
...或只使用 POST
。
UPDATE:修复了URL参数的错误传递,请注意参数
哈希。
UPDATE: Fixed incorrect passing of the URL parameter, notice the params
Hash.
这篇关于使用rest-client ruby gem在elasticsearch中传递json数据获取请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!